class EducationGroupType(SerializableModel): objects = EducationGroupTypeManager() external_id = models.CharField(max_length=100, blank=True, null=True, db_index=True) changed = models.DateTimeField(null=True, auto_now=True) category = models.CharField( max_length=25, choices=Categories.choices(), default=Categories.TRAINING.name, verbose_name=_('Category'), ) name = models.CharField( max_length=255, choices=education_group_types.ALL_TYPES, verbose_name=_('Type of training'), unique=True, ) learning_unit_child_allowed = models.BooleanField(default=False) def __str__(self): return self.get_name_display() def natural_key(self): return (self.name, )
class EducationGroupFilter(forms.Form): academic_year = forms.ModelChoiceField( queryset=academic_year.find_academic_years(), required=False, empty_label=pgettext_lazy("plural", "All"), label=_('Ac yr.')) category = forms.ChoiceField( choices=[("", pgettext_lazy("plural", "All"))] + list(Categories.choices()), required=False, label=_('Category')) education_group_type = ModelChoiceFieldWithData( queryset=EducationGroupType.objects.none(), required=False, empty_label=pgettext_lazy("plural", "All"), label=_('Type')) acronym = forms.CharField(max_length=40, required=False, label=_('Acronym')) title = forms.CharField(max_length=255, required=False, label=_('Title')) requirement_entity_acronym = forms.CharField(max_length=20, required=False, label=_('Entity')) partial_acronym = forms.CharField(max_length=15, required=False, label=_('Code')) with_entity_subordinated = forms.BooleanField(required=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[ "education_group_type"].queryset = EducationGroupType.objects.all( ).order_by_translated_name() self.fields["education_group_type"].set_data_attrs() def clean_category(self): data_cleaned = self.cleaned_data.get('category') if data_cleaned: return data_cleaned def get_object_list(self): clean_data = { key: value for key, value in self.cleaned_data.items() if value is not None } result = education_group_year.search(**clean_data) if clean_data.get('requirement_entity_acronym'): result = _get_filter_entity_management( result, clean_data['requirement_entity_acronym'], clean_data.get('with_entity_subordinated', False)) # TODO User should choice the order return result.order_by('acronym')
def dispatch(self, request, *args, **kwargs): is_root_a_training = EducationGroupYear.objects.filter( id=kwargs["root_id"], education_group_type__category__in=Categories.training_categories( )).exists() if is_root_a_training: return LearningUnitPrerequisiteTraining.as_view()(request, *args, **kwargs) return LearningUnitPrerequisiteGroup.as_view()(request, *args, **kwargs)
class EducationGroupFilter(FilterSet): academic_year = filters.ModelChoiceFilter( queryset=AcademicYear.objects.all(), required=False, empty_label=pgettext_lazy("plural", "All"), label=_('Ac yr.')) category = filters.ChoiceFilter( choices=list(Categories.choices()), required=False, label=_('Category'), field_name='education_group_type__category', empty_label=pgettext_lazy("plural", "All")) education_group_type = filters.ModelChoiceFilter( queryset=EducationGroupType.objects.none(), required=False, empty_label=pgettext_lazy("plural", "All"), label=_('Type'), widget=SelectWithData) management_entity = filters.CharFilter( method='filter_with_entity_subordinated', label=_('Entity')) with_entity_subordinated = filters.BooleanFilter( method=lambda queryset, *args, **kwargs: queryset, label=_('With subord. ent.'), widget=forms.CheckboxInput) acronym = filters.CharFilter( field_name="acronym", lookup_expr='icontains', max_length=40, required=False, label=_('Acronym/Short title'), ) title = filters.CharFilter(field_name="title", lookup_expr='icontains', max_length=255, required=False, label=_('Title')) partial_acronym = filters.CharFilter( field_name="partial_acronym", lookup_expr='icontains', max_length=15, required=False, label=_('Code'), ) order_by_field = 'ordering' ordering = OrderingFilter( fields=(('acronym', 'acronym'), ('partial_acronym', 'code'), ('academic_year__year', 'academic_year'), ('title', 'title'), ('education_group_type__name', 'type'), ('management_entity__entityversion__acronym', 'management_entity')), widget=forms.HiddenInput) class Meta: model = EducationGroupYear fields = [ 'acronym', 'partial_acronym', 'title', 'education_group_type__name', 'management_entity', 'with_entity_subordinated', ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.form.fields[ 'education_group_type'].queryset = EducationGroupType.objects.all( ).order_by_translated_name() self.form.fields['academic_year'].initial = current_academic_year() self.form.fields[ 'category'].initial = education_group_categories.TRAINING def filter_with_entity_subordinated(self, queryset, name, value): with_subordinated = self.form.cleaned_data['with_entity_subordinated'] if value: entity_ids = get_entities_ids(value, with_subordinated) queryset = queryset.filter(management_entity__in=entity_ids) return queryset
class EducationGroupFilter(FilterSet): academic_year = filters.ModelChoiceFilter( queryset=AcademicYear.objects.all(), required=False, label=_('Ac yr.'), empty_label=pgettext_lazy("plural", "All"), ) category = filters.ChoiceFilter( choices=list(Categories.choices()), required=False, label=_('Category'), field_name='education_group_type__category', empty_label=pgettext_lazy("plural", "All")) education_group_type = filters.ModelMultipleChoiceFilter( queryset=EducationGroupType.objects.none(), required=False, label=_('Type'), widget=autocomplete.ModelSelect2Multiple( url='education_group_type_autocomplete', forward=['category'], ), ) management_entity = filters.CharFilter( method='filter_with_entity_subordinated', label=_('Entity')) with_entity_subordinated = filters.BooleanFilter( method=lambda queryset, *args, **kwargs: queryset, label=_('Include subordinate entities'), widget=forms.CheckboxInput) acronym = filters.CharFilter( field_name="acronym", method="filter_education_group_year_field", max_length=40, required=False, label=_('Acronym/Short title'), ) title = filters.CharFilter(field_name="title", method='filter_education_group_year_field', max_length=255, required=False, label=_('Title')) partial_acronym = filters.CharFilter( field_name="partial_acronym", method='filter_education_group_year_field', max_length=15, required=False, label=_('Code'), ) order_by_field = 'ordering' ordering = OrderingFilter( fields=(('acronym', 'acronym'), ('partial_acronym', 'code'), ('academic_year__year', 'academic_year'), ('title', 'title'), ('type_ordering', 'type'), ('entity_management_version', 'management_entity')), widget=forms.HiddenInput) class Meta: model = EducationGroupYear fields = [ 'acronym', 'partial_acronym', 'title', 'education_group_type__name', 'management_entity', 'with_entity_subordinated', ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.queryset = self.get_queryset() self.form.fields[ 'education_group_type'].queryset = EducationGroupType.objects.all( ).order_by_translated_name() self.form.fields['academic_year'].initial = starting_academic_year() self.form.fields[ 'category'].initial = education_group_categories.TRAINING self.form.fields["with_entity_subordinated"].initial = kwargs.pop( 'with_entity_subordinated', True) def filter_with_entity_subordinated(self, queryset, name, value): with_subordinated = self.form.cleaned_data['with_entity_subordinated'] if value: entity_ids = get_entities_ids(value, with_subordinated) queryset = queryset.filter(management_entity__in=entity_ids) return queryset @staticmethod def filter_education_group_year_field(queryset, name, value): return filter_field_by_regex(queryset, name, value) def get_queryset(self): # Need this close so as to return empty query by default when form is unbound if not self.data: return EducationGroupYear.objects.none() management_entity = entity_version.EntityVersion.objects.filter( entity=OuterRef('management_entity'), ).current( OuterRef('academic_year__start_date')).values('acronym')[:1] return EducationGroupYear.objects.all().annotate(type_ordering=Case( *[ When(education_group_type__name=key, then=Value(str(_(val)))) for i, (key, val) in enumerate(education_group_types.ALL_TYPES) ], default=Value(''), output_field=CharField())).annotate( entity_management_version=Subquery(management_entity)) def filter_queryset(self, queryset): # Order by id to always ensure same order when objects have same values for order field (ex: title) qs = super().filter_queryset(queryset) order_fields = qs.query.order_by + ('id', ) return qs.order_by(*order_fields)