Exemple #1
0
class ExternalLearningUnitYearForm(LearningUnitYearForm):
    country = forms.ModelChoiceField(queryset=Country.objects.filter(
        organizationaddress__isnull=False).distinct().order_by('name'),
                                     required=False,
                                     label=_("Country"))
    campus = DynamicChoiceField(
        choices=BLANK_CHOICE_DASH,
        required=False,
        label=_("Institution"),
        help_text=_("Please select a country and a city first"))
    city = DynamicChoiceField(choices=BLANK_CHOICE_DASH,
                              required=False,
                              label=_("City"),
                              help_text=_("Please select a country first"))

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if self.data.get('country'):
            self._init_dropdown_list()

    def _init_dropdown_list(self):
        if self.data.get('city', None):
            self._get_cities()
        if self.data.get('campus', None):
            self._get_campus_list()

    def _get_campus_list(self):
        campus_list = Campus.objects.filter(
            organization__organizationaddress__city=self.data['city']
        ).distinct('organization__name').order_by('organization__name').values(
            'pk', 'organization__name')
        campus_choice_list = []
        for a_campus in campus_list:
            campus_choice_list.append(
                ((a_campus['pk']), (a_campus['organization__name'])))
        self.fields['campus'].choices = add_blank(campus_choice_list)

    def _get_cities(self):
        cities = find_distinct_by_country(self.data['country'])
        cities_choice_list = []
        for a_city in cities:
            city_name = a_city['city']
            cities_choice_list.append(tuple((city_name, city_name)))

        self.fields['city'].choices = add_blank(cities_choice_list)

    def get_queryset(self):
        learning_units = super().get_queryset().filter(
            externallearningunityear__co_graduation=True,
            externallearningunityear__mobility=False,
        ).select_related('campus__organization')
        return learning_units
Exemple #2
0
class LearningContainerYearExternalModelForm(LearningContainerYearModelForm):
    country = ModelChoiceField(
        queryset=Country.objects.filter(organizationaddress__isnull=False).distinct().order_by('name'),
        required=False,
        label=_("country")
    )
    city = DynamicChoiceField(required=False, label=_('city'), choices=BLANK_CHOICE_DASH)

    def prepare_fields(self):
        self.fields["container_type"].choices = ((EXTERNAL, _(EXTERNAL)),)
        self.fields['container_type'].disabled = True
        self.fields['container_type'].required = False

    @staticmethod
    def clean_container_type():
        return EXTERNAL
Exemple #3
0
class ExternalLearningUnitYearForm(LearningUnitYearForm):
    country = forms.ModelChoiceField(queryset=Country.objects.filter(organizationaddress__isnull=False)
                                     .distinct().order_by('name'),
                                     required=False, label=_("country"))
    campus = DynamicChoiceField(choices=BLANK_CHOICE_DASH, required=False, label=_("institution"))
    city = DynamicChoiceField(choices=BLANK_CHOICE_DASH, required=False, label=_("city"))

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if self.data.get('country'):
            self._init_dropdown_list()

    def _init_dropdown_list(self):
        if self.data.get('city', None):
            self._get_cities()
        if self.data.get('campus', None):
            self._get_campus_list()

    def _get_campus_list(self):
        campus_list = Campus.objects.filter(
            organization__organizationaddress__city=self.data['city']
        ).distinct('organization__name').order_by('organization__name').values('pk', 'organization__name')
        campus_choice_list = []
        for a_campus in campus_list:
            campus_choice_list.append(((a_campus['pk']), (a_campus['organization__name'])))
        self.fields['campus'].choices = add_blank(campus_choice_list)

    def _get_cities(self):
        cities = find_distinct_by_country(self.data['country'])
        cities_choice_list = []
        for a_city in cities:
            city_name = a_city['city']
            cities_choice_list.append(tuple((city_name, city_name)))

        self.fields['city'].choices = add_blank(cities_choice_list)

    def get_learning_units(self):
        clean_data = self.cleaned_data
        learning_units = mdl.external_learning_unit_year.search(
            academic_year_id=clean_data['academic_year_id'],
            acronym=clean_data['acronym'],
            title=clean_data['title'],
            country=clean_data['country'],
            city=clean_data['city'],
            campus=clean_data['campus']
        ).select_related('learning_unit_year__academic_year', ) \
            .order_by('learning_unit_year__academic_year__year', 'learning_unit_year__acronym')

        return learning_units

    def clean_city(self):
        return _get_value(self.cleaned_data.get('city'))

    def clean_country(self):
        return _get_value(self.cleaned_data.get('country'))

    def clean_campus(self):
        return _get_value(self.cleaned_data.get('campus'))

    def clean(self):
        if not self._has_criteria():
            self.add_error(None, _('minimum_one_criteria'))