def learning_units_search(request, search_type): service_course_search = search_type == SERVICE_COURSES_SEARCH borrowed_course_search = search_type == BORROWED_COURSE form = LearningUnitYearForm( request.GET or None, service_course_search=service_course_search, borrowed_course_search=borrowed_course_search, initial={'academic_year_id': starting_academic_year()} ) found_learning_units = LearningUnitYear.objects.none() try: if form.is_valid(): found_learning_units = form.get_activity_learning_units() check_if_display_message(request, found_learning_units) except TooManyResultsException: display_error_messages(request, 'too_many_results') if request.POST.get('xls_status') == "xls": return create_xls(request.user, found_learning_units, _get_filter(form, search_type)) if request.POST.get('xls_status') == "xls_comparison": return create_xls_comparison( request.user, found_learning_units, _get_filter(form, search_type), request.POST.get('comparison_year') ) if request.POST.get('xls_status') == "xls_with_parameters": return create_xls_with_parameters( request.user, found_learning_units, _get_filter(form, search_type), { WITH_GRP: request.POST.get('with_grp') == 'true', WITH_ATTRIBUTIONS: request.POST.get('with_attributions') == 'true' } ) form_comparison = SelectComparisonYears(academic_year=get_academic_year_of_reference(found_learning_units)) context = { 'form': form, 'academic_years': get_last_academic_years(), 'container_types': learning_container_year_types.LEARNING_CONTAINER_YEAR_TYPES, 'types': learning_unit_year_subtypes.LEARNING_UNIT_YEAR_SUBTYPES, 'learning_units_count': len(found_learning_units) if isinstance(found_learning_units, list) else found_learning_units.count(), 'current_academic_year': starting_academic_year(), 'experimental_phase': True, 'search_type': search_type, 'is_faculty_manager': request.user.person.is_faculty_manager, 'form_comparison': form_comparison, 'page_obj': paginate_queryset(found_learning_units, request.GET), } return render(request, "learning_units.html", context)
def test_with_faculty_borrowing_set(self): qs = LearningUnitYear.objects.filter(pk__in=[ luy.pk for luy in self.luys_in_different_faculty_than_education_group ]) group = GroupElementYear.objects.get( child_leaf=self.luys_in_different_faculty_than_education_group[0]) entity = OfferYearEntity.objects.get( education_group_year=group.parent).entity result = list( filter_is_borrowed_learning_unit_year( qs, self.academic_year.start_date, faculty_borrowing=entity.id)) self.assertCountEqual(result, [ obj.id for obj in self.luys_in_different_faculty_than_education_group[:1] ]) data = { "academic_year_id": self.academic_year.id, "faculty_borrowing_acronym": entity.most_recent_acronym } form = LearningUnitYearForm(data, borrowed_course_search=True) form.is_valid() results = list(form.get_activity_learning_units()) self.assertEqual( results[0].id, self.luys_in_different_faculty_than_education_group[:1][0].id)
def learning_units_summary_list(request): a_user_person = find_by_user(request.user) learning_units_found = [] initial_academic_year = current_academic_year() if academic_calendar.is_academic_calendar_has_started(initial_academic_year, SUMMARY_COURSE_SUBMISSION): initial_academic_year = initial_academic_year.next() search_form = LearningUnitYearForm(request.GET or None, initial={'academic_year_id': initial_academic_year}) try: if search_form.is_valid(): learning_units_found_search = search_form.get_learning_units( requirement_entities=a_user_person.find_main_entities_version, luy_status=True ) learning_units_found = get_learning_units_and_summary_status(learning_units_found_search) check_if_display_message(request, learning_units_found_search) except TooManyResultsException: display_error_messages(request, 'too_many_results') responsible_and_learning_unit_yr_list = get_responsible_and_learning_unit_yr_list(learning_units_found) learning_units = sorted(learning_units_found, key=lambda learning_yr: learning_yr.acronym) errors = [can_learning_unit_year_educational_information_be_udpated(learning_unit_year_id=luy.id) for luy in learning_units] context = { 'form': search_form, 'formset': _get_formset(request, responsible_and_learning_unit_yr_list), 'learning_units_with_errors': list(zip(learning_units, errors)), 'experimental_phase': True, 'search_type': SUMMARY_LIST, 'is_faculty_manager': a_user_person.is_faculty_manager() } return layout.render(request, "learning_units.html", context)
def learning_units_summary_list(request): a_user_person = request.user.person found_learning_units = LearningUnitYear.objects.none() initial_academic_year = current_academic_year() if academic_calendar.is_academic_calendar_has_started( initial_academic_year, SUMMARY_COURSE_SUBMISSION): initial_academic_year = initial_academic_year.next() search_form = LearningUnitYearForm(request.GET or None, initial={ 'academic_year_id': initial_academic_year, 'with_entity_subordinated': True }) try: if search_form.is_valid(): found_learning_units = search_form.get_learning_units_and_summary_status( requirement_entities=a_user_person.find_main_entities_version, ) check_if_display_message(request, found_learning_units) except TooManyResultsException: display_error_messages(request, 'too_many_results') responsible_and_learning_unit_yr_list = get_responsible_and_learning_unit_yr_list( found_learning_units) for luy in found_learning_units: luy.errors = can_learning_unit_year_educational_information_be_udpated( learning_unit_year_id=luy) if request.GET.get('xls_status') == "xls_teaching_material": try: return generate_xls_teaching_material(request.user, found_learning_units) except ObjectDoesNotExist: display_warning_messages( request, _("the list to generate is empty.").capitalize()) form_comparison = SelectComparisonYears( academic_year=get_academic_year_of_reference(found_learning_units)) context = { 'form': search_form, 'formset': _get_formset(request, responsible_and_learning_unit_yr_list), 'learning_units_count': found_learning_units.count(), 'search_type': SUMMARY_LIST, 'is_faculty_manager': a_user_person.is_faculty_manager, 'form_comparison': form_comparison, 'page_obj': paginate_queryset(found_learning_units, request.GET), } return render(request, "learning_units.html", context)
def learning_units_summary_list(request): a_user_person = find_by_user(request.user) learning_units_found = [] initial_academic_year = current_academic_year() if academic_calendar.is_academic_calendar_has_started( initial_academic_year, SUMMARY_COURSE_SUBMISSION): initial_academic_year = initial_academic_year.next() search_form = LearningUnitYearForm( request.GET or None, initial={'academic_year_id': initial_academic_year}) try: if search_form.is_valid(): learning_units_found_search = search_form.get_learning_units( requirement_entities=a_user_person.find_main_entities_version, luy_status=True) # TODO refactoring : too many queries learning_units_found = get_learning_units_and_summary_status( learning_units_found_search) check_if_display_message(request, learning_units_found_search) except TooManyResultsException: display_error_messages(request, 'too_many_results') responsible_and_learning_unit_yr_list = get_responsible_and_learning_unit_yr_list( learning_units_found) learning_units = sorted(learning_units_found, key=lambda learning_yr: learning_yr.acronym) errors = [ can_learning_unit_year_educational_information_be_udpated( learning_unit_year_id=luy.id) for luy in learning_units ] if request.GET.get('xls_status') == "xls_teaching_material": try: return generate_xls_teaching_material(request.user, learning_units_found) except ObjectDoesNotExist: display_warning_messages( request, _("the list to generate is empty.").capitalize()) form_comparison = SelectComparisonYears( academic_year=get_academic_year_of_reference(learning_units_found)) context = { 'form': search_form, 'formset': _get_formset(request, responsible_and_learning_unit_yr_list), 'learning_units_with_errors': list(zip(learning_units, errors)), 'search_type': SUMMARY_LIST, 'is_faculty_manager': a_user_person.is_faculty_manager(), 'form_comparison': form_comparison } return layout.render(request, "learning_units.html", context)
def test_get_research_criteria_with_choice_field(self): data = QueryDict(mutable=True) data.update({ "academic_year_id": str(self.academic_years[0].id), "container_type": learning_container_year_types.COURSE }) form = LearningUnitYearForm(data) self.assertTrue(form.is_valid()) expected_research_criteria = [(_('Ac yr.'), self.academic_years[0]), (_('Type'), _("Course"))] actual_research_criteria = get_research_criteria(form) self.assertListEqual(expected_research_criteria, actual_research_criteria)
def test_with_faculty_borrowing_set_and_no_entity_version(self): group = GroupElementYear.objects.get( child_leaf=self.luys_in_different_faculty_than_education_group[0]) data = { "academic_year_id": self.academic_year.id, "faculty_borrowing_acronym": group.parent.acronym } form = LearningUnitYearForm(data, borrowed_course_search=True) form.is_valid() result = list(form.get_activity_learning_units()) self.assertEqual(result, [])
def test_search_on_external_mobility(self): data = QueryDict(mutable=True) academic_year = self.academic_years[0] data.update({ "academic_year_id": str(academic_year.id), "container_type": LearningUnitSearchForm.MOBILITY }) ExternalLearningUnitYearFactory( learning_unit_year__academic_year=academic_year, learning_unit_year__learning_container_year__container_type= learning_container_year_types.EXTERNAL, mobility=True, co_graduation=False, ) form = LearningUnitYearForm(data) self.assertTrue(form.is_valid()) self.assertEqual(form.get_queryset().count(), 1)
def learning_units_search(request, search_type): service_course_search = search_type == SERVICE_COURSES_SEARCH borrowed_course_search = search_type == BORROWED_COURSE form = LearningUnitYearForm( request.GET or None, service_course_search=service_course_search, borrowed_course_search=borrowed_course_search, initial={'academic_year_id': current_academic_year()}) found_learning_units = [] try: if form.is_valid(): found_learning_units = form.get_activity_learning_units() check_if_display_message(request, found_learning_units) except TooManyResultsException: messages.add_message(request, messages.ERROR, _('too_many_results')) if request.POST.get('xls_status') == "xls": return create_xls(request.user, found_learning_units, _get_filter(form, search_type)) if request.POST.get('xls_status') == "xls_attribution": return create_xls_attribution(request.user, found_learning_units, _get_filter(form, search_type)) a_person = find_by_user(request.user) context = { 'form': form, 'academic_years': get_last_academic_years(), 'container_types': learning_container_year_types.LEARNING_CONTAINER_YEAR_TYPES, 'types': learning_unit_year_subtypes.LEARNING_UNIT_YEAR_SUBTYPES, 'learning_units': found_learning_units, 'current_academic_year': current_academic_year(), 'experimental_phase': True, 'search_type': search_type, 'is_faculty_manager': a_person.is_faculty_manager() } return layout.render(request, "learning_units.html", context)
def test_search_too_many_results(self): random_luy = LearningUnitYearFactory() form = LearningUnitYearForm( data={ 'acronym': random_luy.acronym, 'academic_year_id': random_luy.academic_year.pk }) form.MAX_RECORDS = 0 self.assertTrue(form.is_valid()) with self.assertRaises(TooManyResultsException): form.get_learning_units()
def test_search_too_many_results(self): cpt = 0 max_limit_of_results = LearningUnitYearForm.MAX_RECORDS while cpt < max_limit_of_results + 1: LearningUnitYearFactory(acronym="L{}".format(cpt), ) cpt += 1 form = LearningUnitYearForm({ 'acronym': 'L', 'service_course_search': False }) self.assertTrue(form.is_valid()) with self.assertRaises(TooManyResultsException): form.get_learning_units() with self.assertRaises(TooManyResultsException): form.get_learning_units_and_summary_status()
def test_search_too_many_results_is_not_raised_when_borrowed_course_search( self): random_luy = LearningUnitYearFactory( academic_year=self.academic_years[0]) form = LearningUnitYearForm(data={ 'acronym': random_luy.acronym, 'academic_year_id': random_luy.academic_year.pk }, borrowed_course_search=True) form.MAX_RECORDS = 0 self.assertTrue(form.is_valid()) form.get_learning_units()
def test_init_with_entity_subordinated_search_form(self): form = LearningUnitYearForm(None, ) self.assertTrue(form.fields['with_entity_subordinated'].initial)