def update_education_group(request, root_id, education_group_year_id): education_group_year = get_object_or_404( EducationGroupYear.objects.select_related( 'education_group_type').prefetch_related( Prefetch('groupelementyear_set', queryset=GroupElementYear.objects.select_related( 'child_leaf__learning_container_year', 'child_branch__education_group_type', 'parent__education_group_type'))), pk=education_group_year_id) person = request.user.person # Store root in the instance to avoid to pass the root in methods # it will be used in the templates. education_group_year.root = root_id if program_manager.is_program_manager(request.user, education_group=education_group_year.education_group) \ and not any((request.user.is_superuser, person.is_faculty_manager, person.is_central_manager)): return _update_certificate_aims(request, root_id, education_group_year) groupelementyear_formset = GroupElementYearFormset( request.POST or None, prefix='group_element_year_formset', queryset=education_group_year.groupelementyear_set.all()) return _update_education_group_year(request, root_id, education_group_year, groupelementyear_formset)
def can_user_edit_administrative_data(a_user, an_education_group_year, raise_exception=False): """ Edition of administrative data is allowed for user which have permission AND if CENTRAL_MANAGER: Check attached entities [person_entity] else Check if user is program manager of education group """ # Tricky solution to make compatible several uses if isinstance(a_user, Person): person = a_user a_user = person.user else: person = Person.objects.get(user=a_user) if not check_permission( person, "base.can_edit_education_group_administrative_data", raise_exception): return False if person.is_central_manager() and _is_management_entity_linked_to_user( person, an_education_group_year): return True return is_program_manager( a_user, education_group=an_education_group_year.education_group)
def update_enrollments(scores_encoding_list, user): is_program_manager = program_manager.is_program_manager(user) updated_enrollments = [] for enrollment in scores_encoding_list.enrollments: enrollment_updated = update_enrollment(enrollment, user, is_program_manager) if enrollment_updated: updated_enrollments.append(enrollment_updated) return updated_enrollments
def test_is_program_manager_of_education_group(self): user = UserFactory(username="******") an_education_group = EducationGroupFactory() ProgramManagerFactory(education_group=an_education_group, person=PersonFactory(user=user)) self.assertTrue( program_manager.is_program_manager( user=user, education_group=an_education_group))
def get_scores_encoding_list(user, **kwargs): current_academic_year = academic_year.current_academic_year() current_number_session = session_exam_calendar.find_session_exam_number() is_program_manager = program_manager.is_program_manager(user) learning_unit_year_id = kwargs.get('learning_unit_year_id') offer_year_id = kwargs.get('offer_year_id') tutor_id = kwargs.get('tutor_id') enrollments_ids = kwargs.get('enrollments_ids') justification = kwargs.get('justification') only_enrolled = _need_to_filter_students_enrolled_to_exam( justification, kwargs) if is_program_manager: professor = tutor.find_by_id(tutor_id) if tutor_id else None offers_year = [offer_year.find_by_id(offer_year_id)] if offer_year_id else \ list(offer_year.find_by_user(user, academic_yr=current_academic_year)) enrollments = exam_enrollment.find_for_score_encodings( academic_year=current_academic_year, session_exam_number=current_number_session, learning_unit_year_id=learning_unit_year_id, tutor=professor, offers_year=offers_year, registration_id=kwargs.get('registration_id'), student_last_name=kwargs.get('student_last_name'), student_first_name=kwargs.get('student_first_name'), justification=justification, only_enrolled=only_enrolled, ) else: professor = tutor.find_by_user(user) enrollments = exam_enrollment.find_for_score_encodings( academic_year=current_academic_year, session_exam_number=current_number_session, learning_unit_year_id=learning_unit_year_id, tutor=professor, only_enrolled=only_enrolled) # Want a subset of exam enrollment list if enrollments_ids: enrollments = enrollments.filter(id__in=enrollments_ids) # Append deadline/deadline_tutor for each exam enrollments enrollments = _append_session_exam_deadline(list(enrollments)) enrollments = sort_encodings(enrollments) return ScoresEncodingList( **{ 'academic_year': current_academic_year, 'number_session': current_number_session, 'learning_unit_year': learning_unit_year.get_by_id(learning_unit_year_id ) if learning_unit_year_id else None, 'enrollments': enrollments })
def get_progress_by_learning_unit_years_and_offer_years( user, session_exam_number, learning_unit_year_id=None, learning_unit_year_ids=None, offer_year_id=None, academic_year=None, only_enrolled=False): if offer_year_id: offer_year_ids = [offer_year_id] else: offer_year_ids = offer_year.find_by_user(user).values_list('id', flat=True) tutor_user = None if not program_manager.is_program_manager(user): tutor_user = tutor.find_by_user(user) queryset = find_for_score_encodings( session_exam_number=session_exam_number, learning_unit_year_id=learning_unit_year_id, learning_unit_year_ids=learning_unit_year_ids, offers_year=offer_year_ids, tutor=tutor_user, academic_year=academic_year, with_session_exam_deadline=False, only_enrolled=only_enrolled) return queryset.values('session_exam', 'learning_unit_enrollment__learning_unit_year', 'learning_unit_enrollment__offer_enrollment__offer_year') \ .annotate( total_exam_enrollments=Count('id'), learning_unit_enrollment__learning_unit_year__acronym= F('learning_unit_enrollment__learning_unit_year__acronym'), learning_unit_enrollment__learning_unit_year__specific_title= F('learning_unit_enrollment__learning_unit_year__specific_title'), learning_unit_enrollment__learning_unit_year__learning_container_year__common_title= F('learning_unit_enrollment__learning_unit_year__learning_container_year__common_title'), exam_enrollments_encoded=Sum(Case( When(Q(score_final__isnull=False) | Q(justification_final__isnull=False), then=1), default=0, output_field=IntegerField()) ), scores_not_yet_submitted=Sum(Case( When((Q(score_draft__isnull=False) & Q(score_final__isnull=True) & Q(justification_final__isnull=True)) | (Q(justification_draft__isnull=False) & Q(score_final__isnull=True) & Q(justification_final__isnull=True)) , then=1), default=0, output_field=IntegerField())) )
def get_scores_encoding_list(user, **kwargs): current_academic_year = academic_year.current_academic_year() current_number_session = session_exam_calendar.find_session_exam_number() is_program_manager = program_manager.is_program_manager(user) learning_unit_year_id = kwargs.get('learning_unit_year_id') offer_year_id = kwargs.get('offer_year_id') tutor_id = kwargs.get('tutor_id') enrollments_ids = kwargs.get('enrollments_ids') justification = kwargs.get('justification') only_enrolled = _need_to_filter_students_enrolled_to_exam(justification, kwargs) if is_program_manager: professor = tutor.find_by_id(tutor_id) if tutor_id else None offers_year = [offer_year.find_by_id(offer_year_id)] if offer_year_id else \ list(offer_year.find_by_user(user, academic_yr=current_academic_year)) enrollments = exam_enrollment.find_for_score_encodings( academic_year=current_academic_year, session_exam_number=current_number_session, learning_unit_year_id=learning_unit_year_id, tutor=professor, offers_year=offers_year, registration_id=kwargs.get('registration_id'), student_last_name=kwargs.get('student_last_name'), student_first_name=kwargs.get('student_first_name'), justification=justification, only_enrolled=only_enrolled, ) else: professor = tutor.find_by_user(user) enrollments = exam_enrollment.find_for_score_encodings( academic_year=current_academic_year, session_exam_number=current_number_session, learning_unit_year_id=learning_unit_year_id, tutor=professor, only_enrolled=only_enrolled ) # Want a subset of exam enrollment list if enrollments_ids: enrollments = enrollments.filter(id__in=enrollments_ids) # Append deadline/deadline_tutor for each exam enrollments enrollments = _append_session_exam_deadline(list(enrollments)) enrollments = sort_encodings(enrollments) return ScoresEncodingList(**{ 'academic_year': current_academic_year, 'number_session': current_number_session, 'learning_unit_year': learning_unit_year.get_by_id(learning_unit_year_id) if learning_unit_year_id else None, 'enrollments': enrollments })
def can_user_edit_administrative_data(a_user, an_education_group_year): """ Edition of administrative data is allowed for user which have permission AND if CENTRAL_MANAGER: Check attached entities [person_entity] else Check if user is program manager of education group """ if not a_user.has_perm("base.can_edit_education_group_administrative_data"): return False person = Person.objects.get(user=a_user) if person.is_central_manager() and _is_management_entity_linked_to_user(person, an_education_group_year): return True return is_program_manager(a_user, education_group=an_education_group_year.education_group)
def get_progress_by_learning_unit_years_and_offer_years(user, session_exam_number, learning_unit_year_id=None, learning_unit_year_ids=None, offer_year_id=None, academic_year=None, only_enrolled=False): if offer_year_id: offer_year_ids = [offer_year_id] else: offer_year_ids = offer_year.find_by_user(user).values_list('id', flat=True) tutor_user = None if not program_manager.is_program_manager(user): tutor_user = tutor.find_by_user(user) queryset = find_for_score_encodings(session_exam_number=session_exam_number, learning_unit_year_id=learning_unit_year_id, learning_unit_year_ids=learning_unit_year_ids, offers_year=offer_year_ids, tutor=tutor_user, academic_year=academic_year, with_session_exam_deadline=False, only_enrolled=only_enrolled) return queryset.values('session_exam', 'learning_unit_enrollment__learning_unit_year', 'learning_unit_enrollment__offer_enrollment__offer_year') \ .annotate( total_exam_enrollments=Count('id'), learning_unit_enrollment__learning_unit_year__acronym= F('learning_unit_enrollment__learning_unit_year__acronym'), learning_unit_enrollment__learning_unit_year__specific_title= F('learning_unit_enrollment__learning_unit_year__specific_title'), learning_unit_enrollment__learning_unit_year__learning_container_year__common_title= F('learning_unit_enrollment__learning_unit_year__learning_container_year__common_title'), exam_enrollments_encoded=Sum(Case( When(Q(score_final__isnull=False) | Q(justification_final__isnull=False), then=1), default=0, output_field=IntegerField()) ), scores_not_yet_submitted=Sum(Case( When((Q(score_draft__isnull=False) & Q(score_final__isnull=True) & Q(justification_final__isnull=True)) | (Q(justification_draft__isnull=False) & Q(score_final__isnull=True) & Q(justification_final__isnull=True)) , then=1), default=0, output_field=IntegerField())) )
def update_enrollment(enrollment, user, is_program_manager=None): if is_program_manager is None: is_program_manager = program_manager.is_program_manager(user) enrollment = clean_score_and_justification(enrollment) if can_modify_exam_enrollment(enrollment, is_program_manager) and \ is_enrollment_changed(enrollment, is_program_manager): with transaction.atomic(): enrollment_updated = set_score_and_justification(enrollment, is_program_manager) if is_program_manager: exam_enrollment.create_exam_enrollment_historic(user, enrollment) return enrollment_updated return None
def _is_eligible(self): if self.education_group_year.education_group_type.category != TRAINING: raise PermissionDenied( _("The education group is not a training type")) if self.education_group_year.academic_year.year < settings.YEAR_LIMIT_EDG_MODIFICATION: raise PermissionDenied( _("You cannot change a education group before %(limit_year)s") % {"limit_year": settings.YEAR_LIMIT_EDG_MODIFICATION}) if self.user.is_superuser: return True if not program_manager.is_program_manager( self.user, education_group=self.education_group_year.education_group): raise PermissionDenied( _("The user is not the program manager of the education group") ) return True
def can_user_edit_administrative_data(a_user, an_education_group_year, raise_exception=False): """ Edition of administrative data is allowed for user which have permission AND if CENTRAL_MANAGER: Check attached entities [person_entity] else Check if user is program manager of education group """ # Tricky solution to make compatible several uses if isinstance(a_user, Person): person = a_user a_user = person.user else: person = Person.objects.get(user=a_user) if not check_permission(person, "base.can_edit_education_group_administrative_data", raise_exception): return False if person.is_central_manager and _is_management_entity_linked_to_user(person, an_education_group_year): return True return is_program_manager(a_user, education_group=an_education_group_year.education_group)
def test_is_not_program_manager(self): user = UserFactory(username="******") self.assertFalse(program_manager.is_program_manager(user=user))
def test_is_program_manager(self): user = UserFactory(username="******") ProgramManagerFactory(offer_year=self.offer_year, person=PersonFactory(user=user)) self.assertTrue(program_manager.is_program_manager(user=user))