Exemplo n.º 1
0
    def get_learning_units_and_summary_status(self,
                                              requirement_entities=None,
                                              luy_status=None):
        self.cleaned_data['status'] = self._set_status(luy_status)

        if requirement_entities:
            self.cleaned_data['requirement_entities'] = requirement_entities

        queryset = self.get_queryset()
        if self.cleaned_data and queryset.count() > self.MAX_RECORDS:
            raise TooManyResultsException

        queryset = queryset.prefetch_related(
            build_entity_container_prefetch([
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ]),
            Prefetch(
                'attribution_set',
                queryset=Attribution.objects.filter(summary_responsible=True),
                to_attr='summary_responsibles'))

        cms_list = TranslatedText.objects.filter(
            entity=LEARNING_UNIT_YEAR,
            text_label__label__in=CMS_LABEL_PEDAGOGY,
            changed__isnull=False,
            reference__in=queryset.values_list(
                'pk', flat=True)).select_related('text_label')

        for learning_unit_yr in queryset:
            append_latest_entities(learning_unit_yr)
            _set_summary_status_on_luy(cms_list, learning_unit_yr)

        return queryset
Exemplo n.º 2
0
    def get_learning_units_and_summary_status(self, requirement_entities=None, luy_status=None):
        self.cleaned_data['status'] = self._set_status(luy_status)

        if requirement_entities:
            self.cleaned_data['requirement_entities'] = requirement_entities

        queryset = self.get_queryset()
        if self.cleaned_data and queryset.count() > self.MAX_RECORDS:
            raise TooManyResultsException

        queryset = queryset.prefetch_related(
            build_entity_container_prefetch([
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ]),
            Prefetch(
                'attribution_set',
                queryset=Attribution.objects.filter(summary_responsible=True),
                to_attr='summary_responsibles'
            )
        )

        cms_list = TranslatedText.objects.filter(
            entity=LEARNING_UNIT_YEAR,
            text_label__label__in=CMS_LABEL_PEDAGOGY,
            changed__isnull=False,
            reference__in=queryset.values_list('pk', flat=True)
        ).select_related('text_label')

        for learning_unit_yr in queryset:
            append_latest_entities(learning_unit_yr)
            _set_summary_status_on_luy(cms_list, learning_unit_yr)

        return queryset
Exemplo n.º 3
0
    def get_proposal_learning_units(self):
        clean_data = self.cleaned_data

        entity_version_prefetch = Prefetch(
            'entity__entityversion_set',
            queryset=mdl.entity_version.search(),
            to_attr='entity_versions')

        entity_container_prefetch = Prefetch(
            'learning_unit_year__learning_container_year__entitycontaineryear_set',
            queryset=mdl.entity_container_year.search(link_type=[
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ]).prefetch_related(entity_version_prefetch),
            to_attr='entity_containers_year')

        clean_data[
            'learning_container_year_id'] = learning_units_form.get_filter_learning_container_ids(
                clean_data)

        proposal = mdl.proposal_learning_unit.search(**clean_data) \
            .select_related('learning_unit_year__academic_year', 'learning_unit_year__learning_container_year',
                            'learning_unit_year__learning_container_year__academic_year') \
            .prefetch_related(entity_container_prefetch) \
            .order_by('learning_unit_year__academic_year__year', 'learning_unit_year__acronym')

        for learning_unit_proposal in proposal:
            append_latest_entities(learning_unit_proposal.learning_unit_year,
                                   None)

        return proposal
Exemplo n.º 4
0
def _create_xls_proposal_comparison(view_obj, context, **response_kwargs):
    user = view_obj.request.user
    luys = context["filter"].qs
    for luy in luys:
        learning_unit_year_with_context.append_latest_entities(
            luy, service_course_search=False)
    filters = _get_filter(context["form"], view_obj.search_type)
    return create_xls_proposal_comparison(user, luys, filters)
Exemplo n.º 5
0
    def filter_queryset(self, queryset):
        qs = super().filter_queryset(queryset)
        qs = qs.prefetch_related(
            build_entity_container_prefetch(entity_container_year_link_type.ALLOCATION_ENTITY),
            build_entity_container_prefetch(entity_container_year_link_type.REQUIREMENT_ENTITY),
        )

        for luy in qs:
            append_latest_entities(luy, service_course_search=True)

        return qs.filter(pk__in=[lu.pk for lu in qs if lu.entities.get(SERVICE_COURSE)])
Exemplo n.º 6
0
    def get_proposal_learning_units(self):
        learning_units = self.get_queryset().filter(
            proposallearningunit__isnull=False)

        learning_units = mdl.proposal_learning_unit.filter_proposal_fields(
            learning_units, **self.cleaned_data)

        for learning_unit in learning_units:
            append_latest_entities(learning_unit, False)

        return learning_units
Exemplo n.º 7
0
    def _set_entities(self):
        today = datetime.date.today()
        an_entity = EntityFactory(organization=OrganizationFactory(type=MAIN))
        self.entity_version = EntityVersionFactory(entity=an_entity, entity_type=entity_type.SCHOOL,
                                                   start_date=today.replace(year=1900),
                                                   end_date=None)

        self.l_unit_yr_1.learning_container_year.requirement_entity = self.entity_version.entity
        self.l_unit_yr_1.learning_container_year.allocation_entity = self.entity_version.entity
        self.l_unit_yr_1.learning_container_year.save()

        append_latest_entities(self.proposal_1.learning_unit_year)
Exemplo n.º 8
0
def _get_learning_unit_yrs_on_2_different_years(academic_yr_comparison,
                                                learning_unit_years):
    learning_unit_years = LearningUnitYear.objects.filter(
        learning_unit__in=(_get_learning_units(learning_unit_years)),
        academic_year__year__in=(
            learning_unit_years[0].academic_year.year,
            academic_yr_comparison)).select_related(
                'academic_year', 'learning_container_year',
                'learning_container_year__academic_year').prefetch_related(
                    get_learning_component_prefetch()).prefetch_related(
                        build_entity_container_prefetch(
                            entity_types.ALLOCATION_ENTITY),
                        build_entity_container_prefetch(
                            entity_types.REQUIREMENT_ENTITY),
                        build_entity_container_prefetch(
                            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_1),
                        build_entity_container_prefetch(
                            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_2),
                    ).order_by('learning_unit', 'academic_year__year')
    [
        append_latest_entities(learning_unit)
        for learning_unit in learning_unit_years
    ]
    [append_components(learning_unit) for learning_unit in learning_unit_years]
    return learning_unit_years
Exemplo n.º 9
0
    def get_learning_units(self, service_course_search=None, requirement_entities=None, luy_status=None):
        service_course_search = service_course_search or self.service_course_search
        clean_data = self.cleaned_data
        clean_data['status'] = self._set_status(luy_status)

        if requirement_entities:
            clean_data['requirement_entities'] = requirement_entities

        # TODO Use a queryset instead !!
        clean_data['learning_container_year_id'] = get_filter_learning_container_ids(clean_data)

        if not service_course_search \
                and clean_data \
                and mdl.learning_unit_year.count_search_results(**clean_data) > SearchForm.MAX_RECORDS:
            raise TooManyResultsException

        learning_units = mdl.learning_unit_year.search(**clean_data) \
            .select_related('academic_year', 'learning_container_year',
                            'learning_container_year__academic_year') \
            .prefetch_related(build_entity_container_prefetch()) \
            .order_by('academic_year__year', 'acronym')

        # FIXME We must keep a queryset
        return [append_latest_entities(learning_unit, service_course_search) for learning_unit in
                learning_units]
Exemplo n.º 10
0
    def get_learning_units(self, service_course_search):
        clean_data = self.cleaned_data

        entity_version_prefetch = Prefetch(
            'entity__entityversion_set',
            queryset=mdl_entity_version.search(),
            to_attr='entity_versions')

        entity_container_prefetch = Prefetch(
            'learning_container_year__entitycontaineryear_set',
            queryset=mdl.entity_container_year.search(link_type=[
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ]).prefetch_related(entity_version_prefetch),
            to_attr='entity_containers_year')

        clean_data[
            'learning_container_year_id'] = _get_filter_learning_container_ids(
                clean_data)
        learning_units = mdl.learning_unit_year.search(**clean_data) \
                             .select_related('academic_year', 'learning_container_year',
                                             'learning_container_year__academic_year') \
                             .prefetch_related(entity_container_prefetch) \
                             .order_by('academic_year__year', 'acronym')

        return [
            append_latest_entities(learning_unit, service_course_search)
            for learning_unit in learning_units
        ]
Exemplo n.º 11
0
 def _set_entities(self):
     today = datetime.date.today()
     an_entity = EntityFactory(organization=OrganizationFactory(type=MAIN))
     self.entity_version = EntityVersionFactory(
         entity=an_entity,
         entity_type=entity_type.SCHOOL,
         start_date=today.replace(year=1900),
         end_date=None)
     self.entity_container_year = EntityContainerYearFactory(
         learning_container_year=self.l_unit_yr_1.learning_container_year,
         type=entity_container_year_link_type.REQUIREMENT_ENTITY,
         entity=self.entity_version.entity)
     self.entity_container_year = EntityContainerYearFactory(
         learning_container_year=self.l_unit_yr_1.learning_container_year,
         type=entity_container_year_link_type.ALLOCATION_ENTITY,
         entity=self.entity_version.entity)
     append_latest_entities(self.proposal_1.learning_unit_year)
Exemplo n.º 12
0
    def get_proposal_learning_units(self):
        learning_units = self.get_queryset().filter(
            proposallearningunit__isnull=False)

        learning_units = mdl.proposal_learning_unit.filter_proposal_fields(
            learning_units, **self.cleaned_data)

        if self.cleaned_data and learning_units.count(
        ) > LearningUnitSearchForm.MAX_RECORDS:
            raise TooManyResultsException

        for learning_unit in learning_units:
            # TODO Use an annotate
            append_latest_entities(learning_unit, None)

        # TODO It'll be easier to return a queryset of learningunits
        return [
            learning_unit.proposallearningunit
            for learning_unit in learning_units
        ]
Exemplo n.º 13
0
    def get_learning_units(self, service_course_search=None):
        service_course_search = service_course_search or self.service_course_search

        learning_units = self.get_queryset()
        if not service_course_search and self.cleaned_data and learning_units.count() > self.MAX_RECORDS:
            raise TooManyResultsException

        learning_units = learning_units.prefetch_related(
            build_entity_container_prefetch([
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ])
        )

        if self.borrowed_course_search:
            # TODO must return a queryset
            learning_units = list(self._filter_borrowed_learning_units(learning_units))

        for learning_unit in learning_units:
            append_latest_entities(learning_unit, service_course_search)

        return learning_units
Exemplo n.º 14
0
    def get_learning_units(self, service_course_search=None):
        service_course_search = service_course_search or self.service_course_search

        learning_units = self.get_queryset()
        if not service_course_search and self.cleaned_data and learning_units.count(
        ) > self.MAX_RECORDS:
            raise TooManyResultsException

        learning_units = learning_units.prefetch_related(
            build_entity_container_prefetch([
                entity_container_year_link_type.ALLOCATION_ENTITY,
                entity_container_year_link_type.REQUIREMENT_ENTITY
            ]))

        if self.borrowed_course_search:
            # TODO must return a queryset
            learning_units = list(
                self._filter_borrowed_learning_units(learning_units))

        for learning_unit in learning_units:
            append_latest_entities(learning_unit, service_course_search)

        return learning_units
Exemplo n.º 15
0
    def get_learning_units(self, service_course_search=None):
        service_course_search = service_course_search or self.service_course_search

        learning_units = self.get_queryset()

        # FIXME: use one queryset for service cource search and borrowed course search instead of filtering in python
        if not service_course_search and not self.borrowed_course_search \
                and self.cleaned_data and learning_units.count() > self.MAX_RECORDS:
            raise TooManyResultsException

        if self.borrowed_course_search:
            learning_units = self._filter_borrowed_learning_units(
                learning_units)

        learning_units = learning_units.prefetch_related(
            build_entity_container_prefetch(
                entity_container_year_link_type.ALLOCATION_ENTITY),
            build_entity_container_prefetch(
                entity_container_year_link_type.REQUIREMENT_ENTITY),
        )
        for learning_unit in learning_units:
            append_latest_entities(learning_unit, service_course_search)

        return learning_units
Exemplo n.º 16
0
    def get_learning_units(self, service_course_search=None, requirement_entities=None, luy_status=None):
        service_course_search = service_course_search or self.service_course_search
        self.cleaned_data['status'] = self._set_status(luy_status)

        if requirement_entities:
            self.cleaned_data['requirement_entities'] = requirement_entities

        learning_units = self.get_queryset()

        if not service_course_search and self.cleaned_data and learning_units.count() > self.MAX_RECORDS:
            raise TooManyResultsException

        if self.borrowed_course_search:
            # TODO must return a queryset
            learning_units = self._filter_borrowed_learning_units(learning_units)

        # FIXME We must keep a queryset
        return [append_latest_entities(learning_unit, service_course_search) for learning_unit in learning_units]
Exemplo n.º 17
0
def _get_learning_unit_yr_with_component(learning_unit_years):
    learning_unit_years = LearningUnitYear.objects.filter(
        learning_unit_year_id__in=[luy.id for luy in learning_unit_years]
    ).select_related(
        'academic_year', 'learning_container_year',
        'learning_container_year__academic_year').prefetch_related(
            get_learning_component_prefetch()).prefetch_related(
                build_entity_container_prefetch([
                    entity_types.ALLOCATION_ENTITY,
                    entity_types.REQUIREMENT_ENTITY,
                    entity_types.ADDITIONAL_REQUIREMENT_ENTITY_1,
                    entity_types.ADDITIONAL_REQUIREMENT_ENTITY_2
                ])).order_by('learning_unit', 'academic_year__year')
    [
        append_latest_entities(learning_unit)
        for learning_unit in learning_unit_years
    ]
    [append_components(learning_unit) for learning_unit in learning_unit_years]
    return learning_unit_years
Exemplo n.º 18
0
def _get_learning_unit_yr_with_component(learning_unit_years):
    learning_unit_years = LearningUnitYear.objects.filter(
        learning_unit_year_id__in=[luy.id for luy in learning_unit_years]
        ).select_related(
        'academic_year',
        'learning_container_year',
        'learning_container_year__academic_year'
    ).prefetch_related(
        get_learning_component_prefetch()
    ).prefetch_related(
        build_entity_container_prefetch([
            entity_types.ALLOCATION_ENTITY,
            entity_types.REQUIREMENT_ENTITY,
            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_1,
            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_2
        ])
    ).order_by('learning_unit', 'academic_year__year')
    [append_latest_entities(learning_unit) for learning_unit in learning_unit_years]
    [append_components(learning_unit) for learning_unit in learning_unit_years]
    return learning_unit_years
Exemplo n.º 19
0
    def _get_learning_units(self, service_course_search=None):
        clean_data = self.cleaned_data
        service_course_search = service_course_search or self.service_course_search
        clean_data[
            'learning_container_year_id'] = get_filter_learning_container_ids(
                clean_data)

        if not service_course_search \
                and clean_data \
                and mdl.learning_unit_year.count_search_results(**clean_data) > SearchForm.MAX_RECORDS:
            raise TooManyResultsException

        learning_units = mdl.learning_unit_year.search(**clean_data) \
            .select_related('academic_year', 'learning_container_year',
                            'learning_container_year__academic_year') \
            .prefetch_related(build_entity_container_prefetch()) \
            .order_by('academic_year__year', 'acronym')

        return [
            append_latest_entities(learning_unit, service_course_search)
            for learning_unit in learning_units
        ]
Exemplo n.º 20
0
def _get_learning_unit_yrs_on_2_different_years(academic_yr_comparison, learning_unit_years):
    learning_unit_years = LearningUnitYear.objects.filter(
        learning_unit__in=(_get_learning_units(learning_unit_years)),
        academic_year__year__in=(
            learning_unit_years[0].academic_year.year,
            academic_yr_comparison)
    ).select_related(
        'academic_year',
        'learning_container_year',
        'learning_container_year__academic_year'
    ).prefetch_related(
        get_learning_component_prefetch()
    ).prefetch_related(
        build_entity_container_prefetch([
            entity_types.ALLOCATION_ENTITY,
            entity_types.REQUIREMENT_ENTITY,
            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_1,
            entity_types.ADDITIONAL_REQUIREMENT_ENTITY_2
        ])
    ).order_by('learning_unit', 'academic_year__year')
    [append_latest_entities(learning_unit) for learning_unit in learning_unit_years]
    [append_components(learning_unit) for learning_unit in learning_unit_years]
    return learning_unit_years