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()
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()
def __init__(self, data, person, subtype, *args, external=False, **kwargs): self.person = person self.user = self.person.user super().__init__(data, *args, **kwargs) self.external = external self.instance.subtype = subtype 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 = Language.objects.all().order_by( 'name')
def create_main_campuses_list(): return [ (None, "---------"), ] + [(elem.id, elem.name) for elem in find_main_campuses()]
def __init__(self, queryset, *args, **kwargs): queryset = campus.find_main_campuses() super().__init__(queryset, *args, **kwargs)
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
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
def __init__(self, queryset, *args, **kwargs): queryset = campus.find_main_campuses() super(MainTeachingCampusChoiceField, self).__init__(queryset, *args, **kwargs)