예제 #1
0
    def __init__(self, data, person, subtype, *args, external=False, **kwargs):
        super().__init__(data, *args, **kwargs)

        self.external = external
        self.instance.subtype = subtype
        self.person = person

        acronym = self.initial.get('acronym')
        if acronym:
            self.initial['acronym'] = split_acronym(acronym, subtype, instance=self.instance)

        if subtype == learning_unit_year_subtypes.PARTIM:
            self.fields['acronym'] = PartimAcronymField()
            self.fields['specific_title'].label = _('Title proper to the partim')
            self.fields['specific_title_english'].label = _('English title proper to the partim')

        # Disabled fields when it's an update
        if self.instance.pk:
            self.fields['academic_year'].disabled = True

            # we cannot edit the internship subtype if the container_type is not internship
            if 'internship_subtype' in self.fields and \
                    self.instance.learning_container_year.container_type != INTERNSHIP:
                self.fields['internship_subtype'].disabled = True

        if not external:
            self.fields['campus'].queryset = find_main_campuses()
        self.fields['language'].queryset = find_all_languages()
예제 #2
0
    def __init__(self, data, person, subtype, *args, **kwargs):
        super().__init__(data, *args, **kwargs)

        self.instance.subtype = subtype
        self.person = person

        acronym = self.initial.get('acronym')
        if acronym:
            self.initial['acronym'] = split_acronym(acronym, subtype)

        if subtype == learning_unit_year_subtypes.PARTIM:
            self.fields['acronym'] = PartimAcronymField()
            self.fields['specific_title'].label = _(
                'official_title_proper_to_partim')
            self.fields['specific_title_english'].label = _(
                'official_english_title_proper_to_partim')

        # Disabled fields when it's an update
        if self.instance.pk:
            self.fields['academic_year'].disabled = True

            # we cannot edit the internship subtype if the container_type is not internship
            if self.instance.learning_container_year.container_type != INTERNSHIP:
                self.fields['internship_subtype'].disabled = True

        self.fields['campus'].queryset = find_main_campuses()
        self.fields['language'].queryset = find_all_languages()
예제 #3
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields["secondary_domains"].widget.attrs['placeholder'] = _(
            'Enter text to search')
        self.fields['primary_language'].queryset = find_all_languages()

        if getattr(self.instance, 'administration_entity', None):
            self.initial['administration_entity'] = get_last_version(
                self.instance.administration_entity).pk

        self.fields['decree_category'].choices = sorted(add_blank(
            decree_category.DecreeCategories.choices()),
                                                        key=lambda c: c[1])
        self.fields['rate_code'].choices = sorted(rate_code.RATE_CODE,
                                                  key=lambda c: c[1])
        self.fields['main_domain'].queryset = Domain.objects.filter(type=domain_type.UNIVERSITY)\
                                                    .select_related('decree')
        if not self.fields['certificate_aims'].disabled:
            self.fields['section'].disabled = False

        if not getattr(self.initial, 'academic_year', None):
            self.set_initial_diploma_values()

        if 'instance' in kwargs and not kwargs['instance']:
            self.fields['academic_year'].label = _('Start')
예제 #4
0
def create_languages_list():
    return [(language.id, language.name) for language in find_all_languages()]
예제 #5
0
class LearningUnitYearForm(BootstrapForm):
    first_letter = forms.ChoiceField(choices=lazy(_create_first_letter_choices,
                                                  tuple),
                                     required=True)
    acronym = forms.CharField(widget=forms.TextInput(attrs={
        'maxlength': "15",
        'required': True
    }))
    academic_year = forms.ModelChoiceField(
        queryset=mdl.academic_year.find_academic_years(), required=True)
    status = forms.BooleanField(required=False, initial=True)
    internship_subtype = forms.TypedChoiceField(choices=add_blank(
        mdl.enums.internship_subtypes.INTERNSHIP_SUBTYPES),
                                                required=False,
                                                empty_value=None)
    credits = forms.DecimalField(
        decimal_places=2,
        validators=[MinValueValidator(0),
                    MaxValueValidator(MAXIMUM_CREDITS)],
        widget=forms.NumberInput(attrs={
            'min': MINIMUM_CREDITS,
            'max': MAXIMUM_CREDITS
        }))
    common_title = forms.CharField()
    common_title_english = forms.CharField(required=False,
                                           widget=forms.TextInput())
    specific_title = forms.CharField(required=False)
    specific_title_english = forms.CharField(required=False,
                                             widget=forms.TextInput())
    session = forms.ChoiceField(add_blank(
        mdl.enums.learning_unit_year_session.LEARNING_UNIT_YEAR_SESSION),
                                required=False)
    subtype = forms.CharField(widget=forms.HiddenInput())
    container_type = forms.ChoiceField(
        choices=lazy(_create_learning_container_year_type_list, tuple),
        widget=forms.Select(attrs={'onchange': 'showInternshipSubtype()'}))
    faculty_remark = forms.CharField(required=False,
                                     widget=forms.Textarea(attrs={'rows': 2}))
    other_remark = forms.CharField(required=False,
                                   widget=forms.Textarea(attrs={'rows': 2}))
    periodicity = forms.CharField(widget=forms.Select(
        choices=PERIODICITY_TYPES))
    quadrimester = forms.CharField(widget=forms.Select(
        choices=add_blank(LEARNING_UNIT_YEAR_QUADRIMESTERS)),
                                   required=False)
    campus = forms.ModelChoiceField(queryset=find_main_campuses())
    requirement_entity = EntitiesVersionChoiceField(
        find_main_entities_version().none(),
        widget=forms.Select(
            attrs={
                'onchange':
                ('updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_1", false);'
                 'updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", true);'
                 )
            }))
    allocation_entity = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=True,
        widget=forms.Select(attrs={'id': 'allocation_entity'}))
    additional_requirement_entity_1 = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=False,
        widget=forms.Select(
            attrs={
                'onchange':
                'updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", false)',
                'disable': 'disable'
            }))
    additional_requirement_entity_2 = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=False,
        widget=forms.Select(attrs={'disable': 'disable'}))
    language = forms.ModelChoiceField(find_all_languages(), empty_label=None)

    def clean(self):
        cleaned_data = super().clean()
        if 'internship_subtype' in self.fields \
                and cleaned_data.get("container_type") == INTERNSHIP \
                and not (cleaned_data['internship_subtype']):
            self.add_error('internship_subtype', _('field_is_required'))
        return cleaned_data

    def clean_acronym(self, regex=LEARNING_UNIT_ACRONYM_REGEX_ALL):
        acronym = _merge_first_letter_and_acronym(
            self.cleaned_data.get('first_letter', ""),
            self.cleaned_data.get('acronym', ""))
        if not re.match(regex, acronym):
            self.add_error('acronym', _('invalid_acronym'))
        return acronym
예제 #6
0
class CreateLearningUnitYearForm(BootstrapForm):
    acronym = forms.CharField(widget=forms.TextInput(attrs={
        'maxlength': "15",
        'required': True
    }))
    academic_year = forms.ModelChoiceField(
        queryset=mdl.academic_year.find_academic_years(),
        required=True,
        empty_label=_('all_label'))
    status = forms.CharField(required=False, widget=forms.CheckboxInput())
    internship_subtype = forms.ChoiceField(
        choices=((None, EMPTY_FIELD), ) +
        mdl.enums.internship_subtypes.INTERNSHIP_SUBTYPES,
        required=False)
    credits = forms.DecimalField(decimal_places=2)
    title = forms.CharField(widget=forms.TextInput(attrs={'required': True}))
    title_english = forms.CharField(required=False, widget=forms.TextInput())
    session = forms.ChoiceField(
        choices=((None, EMPTY_FIELD), ) +
        mdl.enums.learning_unit_year_session.LEARNING_UNIT_YEAR_SESSION,
        required=False)
    subtype = forms.CharField(widget=forms.HiddenInput())
    first_letter = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'text-center',
            'maxlength': "1",
            'readonly': 'readonly'
        }))
    container_type = forms.ChoiceField(
        choices=lazy(create_learning_container_year_type_list, tuple),
        widget=forms.Select(
            attrs={'onchange': 'showInternshipSubtype(this.value)'}))
    faculty_remark = forms.CharField(required=False,
                                     widget=forms.Textarea(attrs={'rows': 2}))
    other_remark = forms.CharField(required=False,
                                   widget=forms.Textarea(attrs={'rows': 2}))
    periodicity = forms.CharField(widget=forms.Select(
        choices=PERIODICITY_TYPES))
    quadrimester = forms.CharField(widget=forms.Select(
        choices=((None, EMPTY_FIELD), ) + LEARNING_UNIT_YEAR_QUADRIMESTERS),
                                   required=False)
    campus = forms.ModelChoiceField(
        queryset=find_administration_campuses(),
        widget=forms.Select(attrs={'onchange': 'setFirstLetter()'}))

    requirement_entity = EntitiesVersionChoiceField(
        find_main_entities_version().none(),
        widget=forms.Select(
            attrs={
                'onchange':
                'showAdditionalEntity(this.value, "id_additional_entity_1")'
            }))

    allocation_entity = EntitiesVersionChoiceField(
        find_main_entities_version(),
        required=False,
        widget=forms.Select(attrs={
            'class': 'form-control',
            'id': 'allocation_entity'
        }))
    additional_entity_1 = EntitiesVersionChoiceField(
        find_main_entities_version(),
        required=False,
        widget=forms.Select(
            attrs={
                'onchange':
                'showAdditionalEntity(this.value, "id_additional_entity_2")',
                'disable': 'disable'
            }))

    additional_entity_2 = EntitiesVersionChoiceField(
        find_main_entities_version(),
        required=False,
        widget=forms.Select(attrs={'disable': 'disable'}))

    language = forms.ModelChoiceField(find_all_languages(), empty_label=None)

    acronym_regex = "^[BLMW][A-Z]{2,4}\d{4}$"

    def __init__(self, person, *args, **kwargs):
        super(CreateLearningUnitYearForm, self).__init__(*args, **kwargs)
        # When we create a learning unit, we can only select requirement entity which are attached to the person
        self.fields[
            "requirement_entity"].queryset = find_main_entities_version_filtered_by_person(
                person)

    def clean_acronym(self):
        data_cleaned = self.data.get('first_letter') + self.cleaned_data.get(
            'acronym')
        if data_cleaned:
            return data_cleaned.upper()

    def is_valid(self):
        if not super().is_valid():
            return False
        try:
            academic_year = mdl.academic_year.find_academic_year_by_id(
                self.data.get('academic_year'))
        except ObjectDoesNotExist:
            return False
        learning_unit_years = mdl.learning_unit_year.find_gte_year_acronym(
            academic_year, self.data['acronym'])
        learning_unit_years_list = [
            learning_unit_year.acronym.lower()
            for learning_unit_year in learning_unit_years
        ]
        if self.data['acronym'] in learning_unit_years_list:
            self.add_error('acronym', _('existing_acronym'))
        elif not re.match(self.acronym_regex, self.cleaned_data['acronym']):
            self.add_error('acronym', _('invalid_acronym'))
        elif self.cleaned_data["container_type"] == INTERNSHIP \
                and not (self.cleaned_data['internship_subtype']):
            self._errors['internship_subtype'] = _('field_is_required')
        else:
            return True
예제 #7
0
class LearningUnitYearForm(BootstrapForm):
    first_letter = forms.ChoiceField(choices=lazy(_create_first_letter_choices,
                                                  tuple),
                                     required=True)
    acronym = forms.CharField(widget=forms.TextInput(attrs={
        'maxlength': "15",
        'required': True
    }))
    academic_year = forms.ModelChoiceField(
        queryset=mdl.academic_year.find_academic_years(), required=True)
    status = forms.BooleanField(required=False, initial=True)
    internship_subtype = forms.TypedChoiceField(choices=add_blank(
        mdl.enums.internship_subtypes.INTERNSHIP_SUBTYPES),
                                                required=False,
                                                empty_value=None)
    credits = forms.DecimalField(
        decimal_places=2,
        validators=[MinValueValidator(0),
                    MaxValueValidator(MAXIMUM_CREDITS)],
        widget=forms.NumberInput(attrs={
            'min': MINIMUM_CREDITS,
            'max': MAXIMUM_CREDITS
        }))
    common_title = forms.CharField(required=False)
    common_title_english = forms.CharField(required=False,
                                           widget=forms.TextInput())
    specific_title = forms.CharField(required=False)
    specific_title_english = forms.CharField(required=False,
                                             widget=forms.TextInput())
    session = forms.ChoiceField(add_blank(
        mdl.enums.learning_unit_year_session.LEARNING_UNIT_YEAR_SESSION),
                                required=False)
    subtype = forms.CharField(widget=forms.HiddenInput())
    container_type = forms.ChoiceField(
        choices=lazy(_create_learning_container_year_type_list, tuple),
        widget=forms.Select(attrs={'onchange': 'showInternshipSubtype()'}))
    faculty_remark = forms.CharField(required=False,
                                     widget=forms.Textarea(attrs={'rows': 2}))
    other_remark = forms.CharField(required=False,
                                   widget=forms.Textarea(attrs={'rows': 2}))
    periodicity = forms.CharField(widget=forms.Select(
        choices=PERIODICITY_TYPES))
    quadrimester = forms.CharField(widget=forms.Select(
        choices=add_blank(LEARNING_UNIT_YEAR_QUADRIMESTERS)),
                                   required=False)
    campus = forms.ModelChoiceField(queryset=find_main_campuses())
    requirement_entity = EntitiesVersionChoiceField(
        find_main_entities_version().none(),
        widget=forms.Select(
            attrs={
                'onchange':
                ('updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_1", false);'
                 'updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", true);'
                 )
            }))
    allocation_entity = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=True,
        widget=forms.Select(attrs={'id': 'allocation_entity'}))
    additional_requirement_entity_1 = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=False,
        widget=forms.Select(
            attrs={
                'onchange':
                'updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", false)',
                'disable': 'disable'
            }))
    additional_requirement_entity_2 = EntitiesVersionChoiceField(
        queryset=find_main_entities_version(),
        required=False,
        widget=forms.Select(attrs={'disable': 'disable'}))
    language = forms.ModelChoiceField(find_all_languages(), empty_label=None)

    def __init__(self, *args, **kwargs):
        self.learning_unit = kwargs.pop('learning_unit', None)
        super(LearningUnitYearForm, self).__init__(*args, **kwargs)

        # TODO the default value must be set in model.
        qs = Campus.objects.filter(name='Louvain-la-Neuve')
        if qs.exists():
            self.fields['campus'].initial = qs.get()

        qs = Language.objects.filter(code='FR')
        if qs.exists():
            self.fields['language'].initial = qs.get().id

        if self.initial.get('subtype') == "PARTIM":
            self.fields['specific_title'].label = _(
                'official_title_proper_to_partim')
            self.fields['specific_title_english'].label = _(
                'official_english_title_proper_to_partim')
        else:
            self.fields['specific_title'].label = _(
                'official_title_proper_to_UE')
            self.fields['specific_title_english'].label = _(
                'official_english_title_proper_to_UE')

    def _get_existing_acronym_list(self, academic_year, acronym):
        if self.learning_unit:
            learning_unit_years = mdl.learning_unit_year.find_gte_year_acronym(academic_year, acronym) \
                .exclude(learning_unit=self.learning_unit)
        else:
            learning_unit_years = mdl.learning_unit_year.find_gte_year_acronym(
                academic_year, acronym)
        return [
            learning_unit_year.acronym
            for learning_unit_year in learning_unit_years
        ]

    def clean(self):
        cleaned_data = super().clean()
        if self.errors:
            return cleaned_data

        self._check_if_acronym_already_exists(cleaned_data)
        if 'internship_subtype' in self.fields \
                and cleaned_data.get("container_type") == INTERNSHIP \
                and not (cleaned_data['internship_subtype']):
            self.add_error('internship_subtype', _('field_is_required'))
        if not cleaned_data["common_title"] and not cleaned_data[
                "specific_title"]:
            self.add_error("common_title",
                           _("must_set_common_title_or_specific_title"))

        requirement_entity = cleaned_data["requirement_entity"]
        allocation_entity = cleaned_data["allocation_entity"]
        container_type = cleaned_data["container_type"]
        self._are_requirement_and_allocation_entities_valid(
            requirement_entity, allocation_entity, container_type)
        return cleaned_data

    def _check_if_acronym_already_exists(self, cleaned_data):
        if 'acronym' in cleaned_data and 'academic_year' in cleaned_data and cleaned_data[
                'academic_year']:
            acronym = cleaned_data['acronym']
            academic_year = cleaned_data['academic_year']
            learning_unit_years_list = self._get_existing_acronym_list(
                academic_year, acronym)
            if acronym in learning_unit_years_list:
                self.add_error('acronym', _('already_existing_acronym'))

    def _are_requirement_and_allocation_entities_valid(self,
                                                       requirement_entity,
                                                       allocation_entity,
                                                       container_type):
        if requirement_entity != allocation_entity and \
                container_type in LEARNING_CONTAINER_YEAR_TYPES_MUST_HAVE_SAME_ENTITIES:
            self.add_error(
                "allocation_entity",
                _("requirement_and_allocation_entities_cannot_be_different"))

    def clean_acronym(self, regex=LEARNING_UNIT_ACRONYM_REGEX_ALL):
        acronym = _merge_first_letter_and_acronym(
            self.cleaned_data.get('first_letter', ""),
            self.cleaned_data.get('acronym', ""))
        if not re.match(regex, acronym):
            raise ValidationError(_('invalid_acronym'))
        return acronym