<commit_before># coding: utf-8

from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.models import Max
from django.db.models.aggregates import Count
from django.forms.models import inlineformset_factory
from django.http.response import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.admin.views.decorators import staff_member_required

from datetime import datetime, timedelta
import json
from functools import cmp_to_key

from ref.models import Environment, ComponentInstance, EnvironmentType, Convention, Application, LogicalComponent, \
    Project, ConventionCounter
from cpn.tests import TestHelper
from scm.models import InstallableSet, Installation, InstallationMethod, Delivery, LogicalComponentVersion, InstallableItem, ItemDependency, Tag, \
    BackupSet, BackupItem
from scm.install import install_iset_envt, install_ii_single_target_envt
from scm.exceptions import MageScmFailedEnvironmentDependencyCheck
from scm.tests import create_test_is

from forms import DeliveryForm, IDForm, IIForm
from scm.backup import register_backup, register_backup_envt_default_plan
from scm.forms import BackupForm
from ref.conventions import nc_sync_naming_convention
import csv
import importlib
from prm.models import getParam

def envts(request):
    envts = Environment.objects_active.annotate(latest_reconfiguration=Max('component_instances__configurations__created_on')).\
        annotate(configuration_modification_count=Count('component_instances__configurations')).\
        order_by('typology')
    return render(request, 'scm/envts.html', {'envts': envts, })

def all_installs(request, envt_name, limit=15):
    envt = Environment.objects.get(name=envt_name)
    envt.potential_tag = datetime.today().strftime('%Y%M%d') + "_" + envt_name
    dlimit = datetime.now() - timedelta(days=limit)
    installs = Installation.objects.filter(install_date__gt=dlimit).filter(modified_components__component_instance__environments=envt).distinct().order_by('-pk')
    # logical_components = envt.component_instances.all().instanciates.implements;
    logical_components = LogicalComponent.objects.filter(scm_trackable=True, implemented_by__instances__environments=envt)
    
    versions = {}
    for compo in envt.component_instances.filter(instanciates__isnull=False, instanciates__implements__scm_trackable=True):
        lc = logical_components.get(id=compo.instanciates.implements_id)
        versions[lc] = compo.version
        
    return render(request, 'scm/envt_all_installs.html', {'installs': installs, 'envt':envt, 'logical_components':logical_components, 'versions': versions, 'limit': limit })

def delivery_list(request):
    deliveries = Delivery.objects.order_by('set_date').reverse().select_related('set_content__what_is_installed__logical_component')
    lis = LogicalComponent.objects.filter(scm_trackable = True, active = True).order_by('pk').select_related('versions')
    return render(request, 'scm/all_deliveries.html', {'deliveries': deliveries, 'lis': lis})

def delivery(request, iset_id):
    delivery = InstallableSet.objects.get(pk=iset_id)
    return render(request, 'scm/delivery_detail.html', {'delivery': delivery, 'envts': Environment.objects_active.all().order_by('typology__chronological_order', 'name')})

@permission_required('scm.validate_installableset')
def delivery_validate(request, iset_id):
    delivery = Delivery.objects.get(pk=iset_id)
    delivery.status = 1
    delivery.save()
    return redirect('scm:delivery_detail', iset_id=iset_id)

def delivery_test(request, delivery_id, envt_id_or_name):
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
        
    try:
        delivery.check_prerequisites(envt.name)
        return render(request, 'scm/delivery_prereqs.html', {'delivery': delivery, 'envt': envt, 'error': None})
    except MageScmFailedEnvironmentDependencyCheck, e:
        return render(request, 'scm/delivery_prereqs.html', {'delivery': delivery, 'envt': envt, 'error': e})

def delivery_test_script(request, delivery_id, envt_id_or_name):
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
        
    try:
        delivery.check_prerequisites(envt.name)
        return HttpResponse("<html><body>OK</body></html>")
    except MageScmFailedEnvironmentDependencyCheck, e:
        return HttpResponse("<html><body>%s</body></html>" %e, status=424)


@permission_required('scm.install_installableset')
def delivery_apply_envt(request, delivery_id, envt_id_or_name):    
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
    
    install_iset_envt(delivery, envt)
    return redirect('scm:envtinstallhist', envt_name=envt.name)

@permission_required('scm.install_installableset')
def delivery_ii_apply_envt(request, ii_id, instance_id, envt_name, install_id = None):    
    ii = InstallableItem.objects.get(pk=ii_id)
    instance = ComponentInstance.objects.get(pk = instance_id)
    install = Installation.objects.get(pk = install_id) if install_id is not None else None
    envt = Environment.objects.get(name=envt_name)

    install = install_ii_single_target_envt(ii = ii, instance = instance, envt = envt, force_prereqs = True, install = install)
    
    response = HttpResponse(content_type='text/text')
    response.write(install.id)
    return response

def lc_versions_per_environment(request):
    Installation.objects.filter()
    envts = Environment.objects_active.all().order_by('typology__chronological_order', 'name')
    res = {}
    for lc in LogicalComponent.objects.filter(scm_trackable=True):
        lc_list = []
        for envt in envts:
            compo_instances = envt.component_instances.filter(instanciates__implements__id=lc.id)
            versions = [i.version_object_safe for i in compo_instances]
            if len(versions) > 0:
                lc_list.append(max(versions, key=cmp_to_key(LogicalComponentVersion.compare)))
            else:
                lc_list.append(None)
        res[lc] = lc_list
    
    return render(request, 'scm/lc_installs_envt.html', {'res': res, 'envts': envts})


def lc_list(request):
    lcs = LogicalComponent.objects.all()
    return render(request, 'scm/lc_versions.html', {'lcs': lcs})

def lc_versions(request, lc_id):
    lc = LogicalComponent.objects.get(pk = lc_id)
    return render(request, 'scm/lc_versions_detail.html', {'lc': lc})



@login_required
@permission_required('scm.add_delivery')
def delivery_edit(request, iset_id=None):
    ## Out model formset, linked to its parent
    InstallableItemFormSet = inlineformset_factory(Delivery, InstallableItem, form=IIForm, extra=1)
    
    ## Already bound?
    instance = None
    if iset_id is not None:
        instance = InstallableSet.objects.get(pk=iset_id)
        ## A dev can only modify an unvalidated delivery
        if not request.user.has_perm('scm.modify_delivery') and instance.status != 3:
            return redirect(reverse('login') + '?next=%s' % request.path)
    
    ## General parameters
    level = int(getParam('DELIVERY_FORM_DATA_FIELDS'))
    display = { 'd1': False, 'd2': False, 'd3':False, 'd4':False, 'globalfile':False, 'iifile': False }
    if level >= 1:
        display['d1'] = True
    if level >= 2:
        display['d2'] = True
    if level >= 3:
        display['d3'] = True
    if level >= 4:
        display['d4'] = True
    mode = getParam('DELIVERY_FORM_DATAFILE_MODE')
    if mode == 'ONE_FILE_PER_SET':
        display['globalfile']= True
    if mode == 'ONE_FILE_PER_ITEM':
        display['iifile'] = True 
    <commit_msg>Extra content forms tuning<commit_after># coding: utf-8

from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.models import Max
from django.db.models.aggregates import Count
from django.forms.models import inlineformset_factory
from django.http.response import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.admin.views.decorators import staff_member_required

from datetime import datetime, timedelta
import json
from functools import cmp_to_key

from ref.models import Environment, ComponentInstance, EnvironmentType, Convention, Application, LogicalComponent, \
    Project, ConventionCounter
from cpn.tests import TestHelper
from scm.models import InstallableSet, Installation, InstallationMethod, Delivery, LogicalComponentVersion, InstallableItem, ItemDependency, Tag, \
    BackupSet, BackupItem
from scm.install import install_iset_envt, install_ii_single_target_envt
from scm.exceptions import MageScmFailedEnvironmentDependencyCheck
from scm.tests import create_test_is

from forms import DeliveryForm, IDForm, IIForm
from scm.backup import register_backup, register_backup_envt_default_plan
from scm.forms import BackupForm
from ref.conventions import nc_sync_naming_convention
import csv
import importlib
from prm.models import getParam

def envts(request):
    envts = Environment.objects_active.annotate(latest_reconfiguration=Max('component_instances__configurations__created_on')).\
        annotate(configuration_modification_count=Count('component_instances__configurations')).\
        order_by('typology')
    return render(request, 'scm/envts.html', {'envts': envts, })

def all_installs(request, envt_name, limit=15):
    envt = Environment.objects.get(name=envt_name)
    envt.potential_tag = datetime.today().strftime('%Y%M%d') + "_" + envt_name
    dlimit = datetime.now() - timedelta(days=limit)
    installs = Installation.objects.filter(install_date__gt=dlimit).filter(modified_components__component_instance__environments=envt).distinct().order_by('-pk')
    # logical_components = envt.component_instances.all().instanciates.implements;
    logical_components = LogicalComponent.objects.filter(scm_trackable=True, implemented_by__instances__environments=envt)
    
    versions = {}
    for compo in envt.component_instances.filter(instanciates__isnull=False, instanciates__implements__scm_trackable=True):
        lc = logical_components.get(id=compo.instanciates.implements_id)
        versions[lc] = compo.version
        
    return render(request, 'scm/envt_all_installs.html', {'installs': installs, 'envt':envt, 'logical_components':logical_components, 'versions': versions, 'limit': limit })

def delivery_list(request):
    deliveries = Delivery.objects.order_by('set_date').reverse().select_related('set_content__what_is_installed__logical_component')
    lis = LogicalComponent.objects.filter(scm_trackable = True, active = True).order_by('pk').select_related('versions')
    return render(request, 'scm/all_deliveries.html', {'deliveries': deliveries, 'lis': lis})

def delivery(request, iset_id):
    delivery = InstallableSet.objects.get(pk=iset_id)
    return render(request, 'scm/delivery_detail.html', {'delivery': delivery, 'envts': Environment.objects_active.all().order_by('typology__chronological_order', 'name')})

@permission_required('scm.validate_installableset')
def delivery_validate(request, iset_id):
    delivery = Delivery.objects.get(pk=iset_id)
    delivery.status = 1
    delivery.save()
    return redirect('scm:delivery_detail', iset_id=iset_id)

def delivery_test(request, delivery_id, envt_id_or_name):
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
        
    try:
        delivery.check_prerequisites(envt.name)
        return render(request, 'scm/delivery_prereqs.html', {'delivery': delivery, 'envt': envt, 'error': None})
    except MageScmFailedEnvironmentDependencyCheck, e:
        return render(request, 'scm/delivery_prereqs.html', {'delivery': delivery, 'envt': envt, 'error': e})

def delivery_test_script(request, delivery_id, envt_id_or_name):
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
        
    try:
        delivery.check_prerequisites(envt.name)
        return HttpResponse("<html><body>OK</body></html>")
    except MageScmFailedEnvironmentDependencyCheck, e:
        return HttpResponse("<html><body>%s</body></html>" %e, status=424)


@permission_required('scm.install_installableset')
def delivery_apply_envt(request, delivery_id, envt_id_or_name):    
    delivery = InstallableSet.objects.get(pk=delivery_id)
    try:
        envt = Environment.objects.get(name=envt_id_or_name)
    except Environment.DoesNotExist:
        envt = Environment.objects.get(pk=int(envt_id_or_name))
    
    install_iset_envt(delivery, envt)
    return redirect('scm:envtinstallhist', envt_name=envt.name)

@permission_required('scm.install_installableset')
def delivery_ii_apply_envt(request, ii_id, instance_id, envt_name, install_id = None):    
    ii = InstallableItem.objects.get(pk=ii_id)
    instance = ComponentInstance.objects.get(pk = instance_id)
    install = Installation.objects.get(pk = install_id) if install_id is not None else None
    envt = Environment.objects.get(name=envt_name)

    install = install_ii_single_target_envt(ii = ii, instance = instance, envt = envt, force_prereqs = True, install = install)
    
    response = HttpResponse(content_type='text/text')
    response.write(install.id)
    return response

def lc_versions_per_environment(request):
    Installation.objects.filter()
    envts = Environment.objects_active.all().order_by('typology__chronological_order', 'name')
    res = {}
    for lc in LogicalComponent.objects.filter(scm_trackable=True):
        lc_list = []
        for envt in envts:
            compo_instances = envt.component_instances.filter(instanciates__implements__id=lc.id)
            versions = [i.version_object_safe for i in compo_instances]
            if len(versions) > 0:
                lc_list.append(max(versions, key=cmp_to_key(LogicalComponentVersion.compare)))
            else:
                lc_list.append(None)
        res[lc] = lc_list
    
    return render(request, 'scm/lc_installs_envt.html', {'res': res, 'envts': envts})


def lc_list(request):
    lcs = LogicalComponent.objects.all()
    return render(request, 'scm/lc_versions.html', {'lcs': lcs})

def lc_versions(request, lc_id):
    lc = LogicalComponent.objects.get(pk = lc_id)
    return render(request, 'scm/lc_versions_detail.html', {'lc': lc})



@login_required
@permission_required('scm.add_delivery')
def delivery_edit(request, iset_id=None):
    if iset_id is None:
        extra=3
    else:
        extra=0
    
    ## Out model formset, linked to its parent
    InstallableItemFormSet = inlineformset_factory(Delivery, InstallableItem, form=IIForm, extra=extra)
    
    ## Already bound?
    instance = None
    if iset_id is not None:
        instance = InstallableSet.objects.get(pk=iset_id)
        ## A dev can only modify an unvalidated delivery
        if not request.user.has_perm('scm.modify_delivery') and instance.status != 3:
            return redirect(reverse('login') + '?next=%s' % request.path)
    
    ## General parameters
    level = int(getParam('DELIVERY_FORM_DATA_FIELDS'))
    display = { 'd1': False, 'd2': False, 'd3':False, 'd4':False, 'globalfile':False, 'iifile': False }
    if level >= 1:
        display['d1'] = True
    if level >= 2:
        display['d2'] = True
    if level >= 3:
        display['d3'] = True
    if level >= 4:
        display['d4'] = True
    mode = getParam('DELIVERY_FORM_DATAFILE_MODE')
    if mode == 'ONE_FILE_PER_SET':
        display['globalfile']= True
    if mode == 'ONE_FILE_PER_ITEM':
        display['iifile'] = True 
    
{'fraction_non_alphanumeric': 0.10688229571984435, 'fraction_numerical': 0.002735894941634241, 'mean_word_length': 4.687759336099585, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1915066', 'n_tokens_mistral': 5239, 'n_tokens_neox': 5151, 'n_words': 980}