class ExternalLearningUnitModelForm(forms.ModelForm): requesting_entity = EntitiesVersionChoiceField(queryset=EntityVersion.objects.none(), label=_('Requesting entity')) entity_version = None def __init__(self, data, person, *args, **kwargs): self.person = person super().__init__(data, *args, **kwargs) self.instance.author = person self.fields['requesting_entity'].queryset = self.person.find_main_entities_version self.fields['co_graduation'].initial = True self.fields['co_graduation'].disabled = True self.fields['mobility'].disabled = True if self.instance.id and hasattr(self.instance, 'requesting_entity'): self.initial['requesting_entity'] = get_last_version(self.instance.requesting_entity) class Meta: model = ExternalLearningUnitYear fields = ('external_acronym', 'external_credits', 'url', 'requesting_entity', 'co_graduation', 'mobility') def post_clean(self, start_date): entity = self.cleaned_data.get('requesting_entity') if not entity: return True entity_v = entity_version.get_by_entity_and_date(entity, start_date) if not entity_v: self.add_error('requesting_entity', _("The linked entity does not exist at the start date of the " "academic year linked to this learning unit")) else: self.entity_version = entity_v return not self.errors
class ProposalLearningUnitForm(forms.ModelForm): entity = EntitiesVersionChoiceField( queryset=find_pedagogical_entities_version()) def __init__(self, data, person, *args, initial=None, **kwargs): super().__init__(data, *args, initial=initial, **kwargs) self.fields[ 'entity'].queryset = person.find_main_entities_version.filter( Q(entity_type=FACULTY) | Q(acronym="ILV")) if initial: for key, value in initial.items(): setattr(self.instance, key, value) if hasattr(self.instance, 'entity'): self.initial['entity'] = get_last_version(self.instance.entity) self.person = person if self.person.is_central_manager: self.enable_field('state') else: self.disable_field('state') self.disable_field('type') def disable_field(self, field): self.fields[field].disabled = True self.fields[field].required = False def enable_field(self, field): self.fields[field].disabled = False self.fields[field].required = True class Meta: model = ProposalLearningUnit fields = ['entity', 'folder_id', 'state', 'type'] widgets = { 'folder_id': forms.TextInput(), } def save(self, commit=True): if hasattr(self.instance, 'learning_unit_year'): # When we save a creation_proposal, we do not need to save the initial_data if self.instance.type != ProposalType.CREATION.name and not self.instance.initial_data: self.instance.initial_data = copy_learning_unit_data( get_by_id(self.instance.learning_unit_year.id)) return super().save(commit)
class LearningContainerYearModelForm(forms.ModelForm): # TODO :: Refactor code redundant code below for entity fields (requirement - allocation - additionnals) requirement_entity = EntitiesVersionChoiceField( widget=autocomplete.ModelSelect2( url='entity_requirement_autocomplete', attrs={ 'id': 'id_requirement_entity', 'data-html': True, 'onchange': ('updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_1", false);' 'updateAdditionalEntityEditability(this.value, "id_additional_entity_1_country", false);' 'updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", true);' 'updateAdditionalEntityEditability(this.value, "id_additional_entity_2_country", true);' ), }, forward=['country_requirement_entity']), queryset=find_pedagogical_entities_version(), label=_('Requirement entity')) country_requirement_entity = CountryEntityField() allocation_entity = EntitiesVersionChoiceField( widget=autocomplete.ModelSelect2(url='allocation_entity_autocomplete', attrs={ 'id': 'allocation_entity', 'data-html': True, }, forward=['country_allocation_entity' ]), queryset=find_pedagogical_entities_version(), label=_('Allocation entity')) country_allocation_entity = CountryEntityField() additional_entity_1 = EntitiesVersionChoiceField( required=False, widget=autocomplete.ModelSelect2( url='additional_entity_1_autocomplete', attrs={ 'id': 'id_additional_requirement_entity_1', 'data-html': True, 'onchange': ('updateAdditionalEntityEditability(this.value, "id_additional_requirement_entity_2", false);' 'updateAdditionalEntityEditability(this.value, "id_additional_entity_2_country", false);' 'updateAdditionalEntityEditability(this.value, ' '"id_component-0-repartition_volume_additional_entity_1",' ' false);' 'updateAdditionalEntityEditability(this.value, ' '"id_component-1-repartition_volume_additional_entity_1",' ' false);' 'updateAdditionalEntityEditability(this.value,' ' "id_component-0-repartition_volume_additional_entity_2",' ' true);' 'updateAdditionalEntityEditability(this.value,' ' "id_component-1-repartition_volume_additional_entity_2",' ' true);'), }, forward=['country_additional_entity_1']), queryset=find_additional_requirement_entities_choices(), label=_('Additional requirement entity 1')) country_additional_entity_1 = CountryEntityField( widget_attrs={'id': 'id_additional_entity_1_country'}) additional_entity_2 = EntitiesVersionChoiceField( required=False, widget=autocomplete.ModelSelect2( url='additional_entity_2_autocomplete', attrs={ 'id': 'id_additional_requirement_entity_2', 'data-html': True, 'onchange': ('updateAdditionalEntityEditability(this.value,' ' "id_component-0-repartition_volume_additional_entity_2", false);' 'updateAdditionalEntityEditability(this.value,' ' "id_component-1-repartition_volume_additional_entity_2", false);' ), }, forward=['country_additional_entity_2']), queryset=find_additional_requirement_entities_choices(), label=_('Additional requirement entity 2')) country_additional_entity_2 = CountryEntityField( widget_attrs={'id': 'id_additional_entity_2_country'}) def __init__(self, *args, **kwargs): self.person = kwargs.pop('person') self.proposal = kwargs.pop('proposal', False) self.is_create_form = kwargs['instance'] is None super().__init__(*args, **kwargs) self.fields[ 'requirement_entity'].queryset = self.person.find_main_entities_version self.prepare_fields() if self.instance.requirement_entity: self.initial['requirement_entity'] = get_last_version( self.instance.requirement_entity).pk if self.instance.allocation_entity: self.initial['allocation_entity'] = get_last_version( self.instance.allocation_entity).pk if self.instance.additional_entity_1: self.initial['additional_entity_1'] = get_last_version( self.instance.additional_entity_1).pk if self.instance.additional_entity_2: self.initial['additional_entity_2'] = get_last_version( self.instance.additional_entity_2).pk def prepare_fields(self): self.fields['container_type'].widget.attrs = { 'onchange': 'showInternshipSubtype()' } # Limit types for faculty_manager only if simple creation of learning_unit if self.person.is_faculty_manager and not self.proposal and self.is_create_form: self.fields[ "container_type"].choices = _create_faculty_learning_container_type_list( ) else: self.fields[ "container_type"].choices = _create_learning_container_year_type_list( ) def save(self, **kwargs): self.instance.learning_container = kwargs.pop('learning_container') self.instance.acronym = kwargs.pop('acronym') self.instance.academic_year = kwargs.pop('academic_year') self._reset_repartition_volumes_if_entity_removed() return super().save(**kwargs) def _reset_repartition_volumes_if_entity_removed(self): """In case an Entity was removed from container, need to reset repartition volume of this entity to None.""" for entity_link_type in REQUIREMENT_ENTITIES: entity_container_attr = self.instance.get_attrs_by_entity_container_type( )[entity_link_type] entity = getattr(self.instance, entity_container_attr, None) if entity_container_attr in self.changed_data and not entity: repartition_attr_by_type = LearningComponentYear.repartition_volume_attrs_by_entity_container_type( ) attr_name = repartition_attr_by_type[entity_link_type] qs = LearningComponentYear.objects.filter( learning_unit_year__learning_container_year=self.instance) qs.update(**{attr_name: None}) class Meta: model = LearningContainerYear fields = ( 'container_type', 'common_title', 'common_title_english', 'type_declaration_vacant', 'team', 'is_vacant', 'requirement_entity', 'allocation_entity', 'additional_entity_1', 'additional_entity_2', ) def post_clean(self, specific_title): if not self.instance.common_title and not specific_title: self.add_error( "common_title", _("You must either set the common title or the specific title") ) return not self.errors @cached_property def additionnal_entity_version_1(self): return self.fields["additional_entity_1"].entity_version @cached_property def additionnal_entity_version_2(self): return self.fields["additional_entity_2"].entity_version