def get_evaluations_with_prefetched_data(evaluations): if isinstance(evaluations, QuerySet): evaluations = evaluations.select_related("course__type").prefetch_related( "course__degrees", "course__semester", "course__responsibles", ) evaluations = Evaluation.annotate_with_participant_and_voter_counts(evaluations) annotate_distributions_and_grades(evaluations) return evaluations
def get_evaluations_of_course(course, request): course_evaluations = [] if course.evaluations.count() > 1: course_evaluations = [ evaluation for evaluation in course.evaluations.filter( state=Evaluation.State.PUBLISHED) if evaluation.can_be_seen_by(request.user) ] if request.user.is_reviewer: course_evaluations += course.evaluations.filter(state__in=[ Evaluation.State.IN_EVALUATION, Evaluation.State.EVALUATED, Evaluation.State.REVIEWED ]) annotate_distributions_and_grades(course_evaluations) course_evaluations = get_evaluations_with_course_result_attributes( course_evaluations) return course_evaluations
def index(request): user = request.user show_delegated = get_parameter_from_url_or_session(request, "show_delegated", True) represented_proxy_users = user.represented_users.filter(is_proxy_user=True) contributor_visible_states = [ Evaluation.State.PREPARED, Evaluation.State.EDITOR_APPROVED, Evaluation.State.APPROVED, Evaluation.State.IN_EVALUATION, Evaluation.State.EVALUATED, Evaluation.State.REVIEWED, Evaluation.State.PUBLISHED, ] own_courses = Course.objects.filter( Q(evaluations__state__in=contributor_visible_states) & (Q(responsibles=user) | Q(evaluations__contributions__contributor=user) | Q(evaluations__contributions__contributor__in=represented_proxy_users) | Q(responsibles__in=represented_proxy_users))) own_evaluations = (Evaluation.objects.filter( course__in=own_courses).annotate(contributes_to=Exists( Evaluation.objects.filter( id=OuterRef( "id"), contributions__contributor=user))).prefetch_related( "course", "course__evaluations", "course__degrees", "course__type", "course__semester")) own_evaluations = [ evaluation for evaluation in own_evaluations if evaluation.can_be_seen_by(user) ] displayed_evaluations = own_evaluations if show_delegated: represented_users = user.represented_users.exclude(is_proxy_user=True) delegated_courses = Course.objects.filter( Q(evaluations__state__in=contributor_visible_states) & (Q(responsibles__in=represented_users) | Q( evaluations__contributions__role=Contribution.Role.EDITOR, evaluations__contributions__contributor__in=represented_users, ))) delegated_evaluations = Evaluation.objects.filter( course__in=delegated_courses).prefetch_related( "course", "course__evaluations", "course__degrees", "course__type", "course__semester") delegated_evaluations = [ evaluation for evaluation in delegated_evaluations if evaluation.can_be_seen_by(user) ] for evaluation in delegated_evaluations: evaluation.delegated_evaluation = True displayed_evaluations += set(delegated_evaluations) - set( displayed_evaluations) displayed_evaluations.sort( key=lambda evaluation: (evaluation.course.name, evaluation.name) ) # evaluations must be sorted for regrouping them in the template annotate_distributions_and_grades(e for e in displayed_evaluations if e.state == Evaluation.State.PUBLISHED) displayed_evaluations = get_evaluations_with_course_result_attributes( displayed_evaluations) semesters = Semester.objects.all() semester_list = [ dict( semester_name=semester.name, id=semester.id, is_active=semester.is_active, evaluations=[ evaluation for evaluation in displayed_evaluations if evaluation.course.semester_id == semester.id ], ) for semester in semesters ] template_data = dict( semester_list=semester_list, show_delegated=show_delegated, delegate_selection_form=DelegateSelectionForm(), ) return render(request, "contributor_index.html", template_data)
def index(request): query = (Evaluation.objects.annotate(participates_in=Exists( Evaluation.objects.filter(id=OuterRef("id"), participants=request.user) )).annotate(voted_for=Exists( Evaluation.objects.filter(id=OuterRef( "id"), voters=request.user))).filter( ~Q(state=Evaluation.State.NEW), course__evaluations__participants=request.user).exclude( state=Evaluation.State.NEW).prefetch_related( "course", "course__semester", "course__grade_documents", "course__type", "course__evaluations", "course__responsibles", "course__degrees", ).distinct()) query = Evaluation.annotate_with_participant_and_voter_counts(query) evaluations = [ evaluation for evaluation in query if evaluation.can_be_seen_by(request.user) ] inner_evaluation_ids = [ inner_evaluation.id for evaluation in evaluations for inner_evaluation in evaluation.course.evaluations.all() ] inner_evaluation_query = Evaluation.objects.filter( pk__in=inner_evaluation_ids) inner_evaluation_query = Evaluation.annotate_with_participant_and_voter_counts( inner_evaluation_query) evaluations_by_id = { evaluation["id"]: evaluation for evaluation in inner_evaluation_query.values() } for evaluation in evaluations: for inner_evaluation in evaluation.course.evaluations.all(): inner_evaluation.num_voters = evaluations_by_id[ inner_evaluation.id]["num_voters"] inner_evaluation.num_participants = evaluations_by_id[ inner_evaluation.id]["num_participants"] annotate_distributions_and_grades(e for e in evaluations if e.state == Evaluation.State.PUBLISHED) evaluations = get_evaluations_with_course_result_attributes(evaluations) # evaluations must be sorted for regrouping them in the template evaluations.sort( key=lambda evaluation: (evaluation.course.name, evaluation.name)) semesters = Semester.objects.all() semester_list = [ dict( semester_name=semester.name, id=semester.id, results_are_archived=semester.results_are_archived, grade_documents_are_deleted=semester.grade_documents_are_deleted, evaluations=[ evaluation for evaluation in evaluations if evaluation.course.semester_id == semester.id ], ) for semester in semesters ] unfinished_evaluations_query = (Evaluation.objects.filter( participants=request.user, state__in=[ Evaluation.State.PREPARED, Evaluation.State.EDITOR_APPROVED, Evaluation.State.APPROVED, Evaluation.State.IN_EVALUATION, ], ).exclude(voters=request.user).prefetch_related("course__responsibles", "course__type", "course__semester")) unfinished_evaluations_query = Evaluation.annotate_with_participant_and_voter_counts( unfinished_evaluations_query) unfinished_evaluations = list(unfinished_evaluations_query) # available evaluations come first, ordered by time left for evaluation and the name # evaluations in other (visible) states follow by name def sorter(evaluation): return ( evaluation.state != Evaluation.State.IN_EVALUATION, evaluation.vote_end_date if evaluation.state == Evaluation.State.IN_EVALUATION else None, evaluation.full_name, ) unfinished_evaluations.sort(key=sorter) template_data = dict( semester_list=semester_list, can_download_grades=request.user.can_download_grades, unfinished_evaluations=unfinished_evaluations, evaluation_end_warning_period=settings.EVALUATION_END_WARNING_PERIOD, ) return render(request, "student_index.html", template_data)