Skip to content

Commit

Permalink
enum for differnet button filter options
Browse files Browse the repository at this point in the history
  • Loading branch information
ybrnr committed Aug 12, 2024
1 parent c83c5ed commit 3d94b85
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 57 deletions.
22 changes: 11 additions & 11 deletions evap/results/templates/results_evaluation_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ <h3 class="mb-0">{{ evaluation.full_name }} ({{ evaluation.course.semester.name
<div class="btn-switch btn-group">

<a
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results=full&view_contributor_results={{view_contributor_results}}"
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results=full&view_contributor_results={{ view_contributor_results.value }}"
role="button"
class="btn btn-sm btn-light{% if view_general_results == 'full' %} active{% endif %} {% if not can_see_general_textanswers %} disabled {% endif %}"
class="btn btn-sm btn-light{% if view_general_results.value == 'full' %} active{% endif %} {% if not can_see_general_textanswers %} disabled {% endif %}"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="{% blocktranslate %}All results of general questions (including text answers){% endblocktranslate %}"
Expand All @@ -53,9 +53,9 @@ <h3 class="mb-0">{{ evaluation.full_name }} ({{ evaluation.course.semester.name


<a
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results=ratings&view_contributor_results={{view_contributor_results}}"
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results=ratings&view_contributor_results={{ view_contributor_results.value }}"
role="button"
class="btn btn-sm btn-light{% if view_general_results == 'ratings' %} active{% endif %} {% if not can_see_general_textanswers %} disabled {% endif %}"
class="btn btn-sm btn-light{% if view_general_results.value == 'ratings' %} active{% endif %} {% if not can_see_general_textanswers %} disabled {% endif %}"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="{% translate 'Only rating results of general questions (without text answers)' %}"
Expand All @@ -74,9 +74,9 @@ <h3 class="mb-0">{{ evaluation.full_name }} ({{ evaluation.course.semester.name
<div class="btn-switch btn-group">

<a
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{view_general_results}}&view_contributor_results=full"
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{ view_general_results.value }}&view_contributor_results=full"
role="button"
class="btn btn-sm btn-light{% if view_contributor_results == 'full' %} active{% endif %} {% if not can_see_contributor_textanswers %} disabled {% endif %}"
class="btn btn-sm btn-light{% if view_contributor_results.value == 'full' %} active{% endif %} {% if not can_see_contributor_textanswers %} disabled {% endif %}"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="{% blocktranslate %}All results of contributor questions available for you (including text answers){% endblocktranslate %}"
Expand All @@ -86,18 +86,18 @@ <h3 class="mb-0">{{ evaluation.full_name }} ({{ evaluation.course.semester.name


<a
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{view_general_results}}&view_contributor_results=ratings"
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{ view_general_results.value }}&view_contributor_results=ratings"
role="button"
class="btn btn-sm btn-light{% if view_contributor_results == 'ratings' %} active{% endif %} {% if not can_see_contributor_textanswers %} disabled {% endif %}"
class="btn btn-sm btn-light{% if view_contributor_results.value == 'ratings' %} active{% endif %} {% if not can_see_contributor_textanswers %} disabled {% endif %}"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="{% translate 'Only rating results of contributor questions (without text answers)' %}"
>
{% translate 'Ratings' %}
</a>
{% if user.is_staff and view_contributor_results == 'personal' or is_contributor %}
{% if user.is_staff and view_contributor_results.value == 'personal' or is_contributor %}
<a
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{view_general_results}}&view_contributor_results=personal"
href="{% url 'results:evaluation_detail' evaluation.course.semester.id evaluation.id %}?view_general_results={{ view_general_results.value }}&view_contributor_results=personal"
role="button"
class="btn btn-sm btn-light{% if view_contributor_results == 'personal' %} active{% endif %} {% if not can_see_contributor_textanswers %} disabled {% endif %}"
data-bs-toggle="tooltip"
Expand All @@ -121,7 +121,7 @@ <h3>{{ evaluation.full_name }} ({{ evaluation.course.semester.name }})</h3>
<div class="card-header d-flex">
<div class="me-auto">{% translate 'Overview' %}</div>
{% if can_export_text_answers %}
<a class="btn btn-sm btn-light d-print-none" href="{% url 'results:evaluation_text_answers_export' evaluation.id %}?view_general_results={{ view_general_results }}&view_contributor_results={{ view_contributor_results }}{% if contributor_id is not None %}&contributor_id={{ contributor_id }}{% endif %}" type="button">
<a class="btn btn-sm btn-light d-print-none" href="{% url 'results:evaluation_text_answers_export' evaluation.id %}?view_general_results={{ view_general_results.value }}&view_contributor_results={{ view_contributor_results.value }}{% if contributor_id is not None %}&contributor_id={{ contributor_id }}{% endif %}" type="button">
{% translate 'Export visible text answers' %}
</a>
{% endif %}
Expand Down
7 changes: 5 additions & 2 deletions evap/results/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
)
from evap.evaluation.tests.tools import make_rating_answer_counters
from evap.results.tools import (
ViewGeneralResults,
ViewContributorResults,

cache_results,
calculate_average_course_distribution,
calculate_average_distribution,
Expand Down Expand Up @@ -563,8 +566,8 @@ def test_contributors_and_delegate_count_in_textanswer_visibility_info(self):
for user in UserProfile.objects.all():
represented_users = [user] + list(user.represented_users.all())
for i, textanswer in enumerate(textanswers):
if can_textanswer_be_seen_by(user, represented_users, textanswer, "full", "full"):
if can_textanswer_be_seen_by(user, [user], textanswer, "full", "full"):
if can_textanswer_be_seen_by(user, represented_users, textanswer, ViewGeneralResults.FULL, ViewContributorResults.FULL):
if can_textanswer_be_seen_by(user, [user], textanswer, ViewGeneralResults.FULL, ViewContributorResults.FULL):
users_seeing_contribution[i][0].add(user)
else:
users_seeing_contribution[i][1].add(user)
Expand Down
26 changes: 9 additions & 17 deletions evap/results/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
render_pages,
)
from evap.results.exporters import TextAnswerExporter
from evap.results.tools import cache_results
from evap.results.tools import ViewContributorResults, ViewGeneralResults, cache_results
from evap.results.views import get_evaluations_with_prefetched_data, update_template_cache
from evap.staff.tests.utils import WebTestStaffMode, helper_exit_staff_mode, run_in_staff_mode

Expand Down Expand Up @@ -474,32 +474,24 @@ def test_default_view(
cache_results(self.evaluation)

page_without_get_parameter = self.app.get(self.url, user=self.manager)
self.assertEqual(page_without_get_parameter.context["view_general_results"], "full")
self.assertEqual(page_without_get_parameter.context["view_contributor_results"], "full")
self.assertEqual(page_without_get_parameter.context["view_general_results"], ViewGeneralResults.FULL)
self.assertEqual(page_without_get_parameter.context["view_contributor_results"], ViewContributorResults.FULL)

page_with_ratings_general_get_parameter = self.app.get(
self.url + "?view_general_results=ratings", user=self.manager
)
self.assertEqual(page_with_ratings_general_get_parameter.context["view_general_results"], "ratings")
self.assertEqual(page_with_ratings_general_get_parameter.context["view_contributor_results"], "full")
self.assertEqual(page_with_ratings_general_get_parameter.context["view_general_results"], ViewGeneralResults.RATINGS)
self.assertEqual(page_with_ratings_general_get_parameter.context["view_contributor_results"], ViewContributorResults.FULL)

page_with_ratings_general_get_parameter = self.app.get(
self.url + "?view_contributor_results=ratings", user=self.manager
)
self.assertEqual(page_with_ratings_general_get_parameter.context["view_general_results"], "full")
self.assertEqual(page_with_ratings_general_get_parameter.context["view_contributor_results"], "ratings")
self.assertEqual(page_with_ratings_general_get_parameter.context["view_general_results"], ViewGeneralResults.FULL)
self.assertEqual(page_with_ratings_general_get_parameter.context["view_contributor_results"], ViewContributorResults.RATINGS)

# digga das doch qustsch unter default view test ihr atzen
# page_with_full_general_get_parameter = self.app.get(self.url + "?view_general_results=full", user=self.manager)
# self.assertEqual(page_with_full_general_get_parameter.context["view_general_results"], "full")

page_with_random_get_parameter = self.app.get(
self.url + "?view_general_results=josefwarhier&view_contributor_results=yannikwarhier", user=self.manager
page_with_random_get_parameter = self.app.get( # raises bad request
self.url + "?view_general_results=josefwarhier&view_contributor_results=yannikwarhier", user=self.manager, status=400
)
self.assertEqual(page_with_random_get_parameter.context["view_general_results"], "full")
self.assertEqual(page_with_random_get_parameter.context["view_contributor_results"], "full")

# noch für den anderen param? auch von view_contributor_results

def test_wrong_state(self):
helper_exit_staff_mode(self)
Expand Down
31 changes: 22 additions & 9 deletions evap/results/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict, defaultdict
from collections.abc import Iterable
from copy import copy
from enum import Enum
from math import ceil, modf
from typing import TypeGuard, cast

Expand Down Expand Up @@ -35,6 +36,17 @@
}


class ViewGeneralResults(Enum):
FULL = "full"
RATINGS = "ratings"


class ViewContributorResults(Enum):
FULL = "full"
RATINGS = "ratings"
PERSONAL = "personal"


class TextAnswerVisibility:
def __init__(self, visible_by_contribution: Iterable[UserProfile], visible_by_delegation_count: int):
self.visible_by_contribution = [discard_cached_related_objects(copy(user)) for user in visible_by_contribution]
Expand Down Expand Up @@ -475,18 +487,18 @@ def can_textanswer_be_seen_by( # noqa: PLR0911,PLR0912
user: UserProfile,
represented_users: list[UserProfile],
textanswer: TextAnswer,
view_general_results: str,
view_contributor_results: str,
view_general_results: ViewGeneralResults,
view_contributor_results: ViewContributorResults,
) -> bool:
assert textanswer.review_decision in [TextAnswer.ReviewDecision.PRIVATE, TextAnswer.ReviewDecision.PUBLIC]
contributor = textanswer.contribution.contributor

# NOTE: when changing this behavior, make sure all changes are also reflected in results.tools.textanswers_visible_to
# and in results.tests.test_tools.TestTextAnswerVisibilityInfo
if textanswer.contribution.is_general:
if view_general_results == "ratings":
if view_general_results == ViewGeneralResults.RATINGS:
return False
elif view_general_results == "full":
elif view_general_results == ViewGeneralResults.FULL:
# reviewer can see everything
if user.is_reviewer:
return True
Expand All @@ -504,19 +516,20 @@ def can_textanswer_be_seen_by( # noqa: PLR0911,PLR0912
for user in textanswer.contribution.evaluation.course.responsibles.all()
):
return True
else:
else:
return False
else:
if view_contributor_results == "ratings":
if view_contributor_results == ViewContributorResults.RATINGS:
return False
if user.is_reviewer:
return True
if view_contributor_results == "personal" or textanswer.is_private: # private textanswers should only be seen by the contributor and reviewer
if (
view_contributor_results == ViewContributorResults.PERSONAL or textanswer.is_private
): # private textanswers should only be seen by the contributor and reviewer
return contributor == user
if view_contributor_results == "full":
if view_contributor_results == ViewContributorResults.FULL:
# users can see textanswers if the contributor is one of their represented users (which includes the user itself)
if contributor in represented_users:
return True


return False
61 changes: 43 additions & 18 deletions evap/results/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@
from django.utils import translation

from evap.evaluation.auth import internal_required
from evap.evaluation.models import Contribution, Course, CourseType, Degree, Evaluation, Semester, TextAnswer, UserProfile
from evap.evaluation.models import (
Contribution,
Course,
CourseType,
Degree,
Evaluation,
Semester,
TextAnswer,
UserProfile,
)
from evap.evaluation.tools import AttachmentResponse, unordered_groupby
from evap.results.exporters import TextAnswerExporter
from evap.results.tools import (
STATES_WITH_RESULT_TEMPLATE_CACHING,
HeadingResult,
RatingResult,
TextResult,
ViewContributorResults,
ViewGeneralResults,
annotate_distributions_and_grades,
can_textanswer_be_seen_by,
get_evaluations_with_course_result_attributes,
Expand Down Expand Up @@ -191,7 +202,7 @@ def evaluation_detail(request, semester_id, evaluation_id):
course_evaluations.sort(key=lambda evaluation: evaluation.name)

contributors_with_omitted_results = []
if view_contributor_results == "personal":
if view_contributor_results == ViewContributorResults.PERSONAL:
contributors_with_omitted_results = [
contribution_result.contributor
for contribution_result in evaluation_result.contribution_results
Expand All @@ -218,7 +229,10 @@ def evaluation_detail(request, semester_id, evaluation_id):
"is_responsible_or_contributor_or_delegate": is_responsible_or_contributor_or_delegate,
"can_download_grades": view_as_user.can_download_grades,
"can_export_text_answers": (
(view_general_results == "full" or view_contributor_results in ("personal", "full"))
(
view_general_results == ViewGeneralResults.FULL
or view_contributor_results in (ViewContributorResults.PERSONAL, ViewContributorResults.FULL)
)
and (view_as_user.is_reviewer or is_responsible_or_contributor_or_delegate)
),
"view_contributor_results": view_contributor_results,
Expand All @@ -232,7 +246,9 @@ def evaluation_detail(request, semester_id, evaluation_id):
if evaluation.is_user_contributor(view_as_user)
else False
),
"can_see_contributor_textanswers": TextAnswer.objects.filter(contribution__contributor=view_as_user, contribution__evaluation=evaluation).exists()
"can_see_contributor_textanswers": TextAnswer.objects.filter(
contribution__contributor=view_as_user, contribution__evaluation=evaluation
).exists(),
}
return render(request, "results_evaluation_detail.html", template_data)

Expand Down Expand Up @@ -325,7 +341,10 @@ def split_evaluation_result_into_top_bottom_and_contributor(evaluation_result, v
else:
top_results.append(questionnaire_result)

elif view_contributor_results != "personal" or view_as_user.id == contribution_result.contributor.id:
elif (
view_contributor_results != ViewContributorResults.PERSONAL
or view_as_user.id == contribution_result.contributor.id
):
contributor_results.append(contribution_result)

if not contributor_results:
Expand Down Expand Up @@ -400,27 +419,33 @@ def evaluation_detail_parse_get_parameters(request, evaluation):
if not evaluation.can_results_page_be_seen_by(request.user):
raise PermissionDenied

view_general_results = request.GET.get(
"view_general_results",
"full",
)
if view_general_results not in ["full", "ratings"]:
view_general_results = "full"
try:
view_contributor_results = ViewContributorResults(
request.GET.get(
"view_contributor_results",
ViewContributorResults.FULL,
)
)
except ValueError as e:
raise BadRequest from e

view_contributor_results = request.GET.get(
"view_contributor_results",
"full",
)
if view_contributor_results not in ["full", "ratings", "personal"]:
view_contributor_results = "full"
try:
view_general_results = ViewGeneralResults(
request.GET.get(
"view_general_results",
ViewGeneralResults.FULL,
)
)
except ValueError as e:
raise BadRequest from e

view_as_user = request.user
try:
contributor = get_object_or_404(UserProfile, pk=request.GET.get("contributor_id", request.user.id))
except ValueError as e:
raise BadRequest from e

if view_contributor_results == "personal" and request.user.is_staff:
if view_contributor_results == ViewContributorResults.PERSONAL and request.user.is_staff:
view_as_user = contributor
contributor_id = contributor.pk if contributor != request.user else None

Expand Down

0 comments on commit 3d94b85

Please sign in to comment.