Repository: e-valuation/EvaP

Issue:
Consistent permission method naming
we currently have multiple methods checking permissions which don't use consistent naming. e.g.,
- `user_can_see_textanswer(user, textanswer, ...)` (which is defined in `results.views` although it should probably rather be located in `results.tools`)
- `Course.can_user_see_course(user)`
- `Course.can_manager_edit()`

these should be refactored to something like
- `can_user_see_textanswer(user, textanswer, ...)`
- `Course.can_be_seen_by(user)`
- `Course.can_be_edited_by_managers()`

the methods above are just some examples. all models, views and tools should be checked for similar occurrences.

_Originally posted by @karyon in https://github.com/fsr-de/EvaP/pull/1294#discussion_r250199820_
Consistent permission method naming
we currently have multiple methods checking permissions which don't use consistent naming. e.g.,
- `user_can_see_textanswer(user, textanswer, ...)` (which is defined in `results.views` although it should probably rather be located in `results.tools`)
- `Course.can_user_see_course(user)`
- `Course.can_manager_edit()`

these should be refactored to something like
- `can_user_see_textanswer(user, textanswer, ...)`
- `Course.can_be_seen_by(user)`
- `Course.can_be_edited_by_managers()`

the methods above are just some examples. all models, views and tools should be checked for similar occurrences.

_Originally posted by @karyon in https://github.com/fsr-de/EvaP/pull/1294#discussion_r250199820_


Discussion:
#1319 was missing changes in the html templates, accessing renamed methods. the current release is broken.
#1319 was missing changes in the html templates, accessing renamed methods. the current release is broken.

Resolving patch:
diff --git a/evap/evaluation/templatetags/evaluation_filters.py b/evap/evaluation/templatetags/evaluation_filters.py
--- a/evap/evaluation/templatetags/evaluation_filters.py
+++ b/evap/evaluation/templatetags/evaluation_filters.py
@@ -75,12 +75,12 @@ def statedescription(state):
 
 
 @register.filter
-def can_user_see_results_page(evaluation, user):
+def can_results_page_be_seen_by(evaluation, user):
     return evaluation.can_results_page_be_seen_by(user)
 
 
-@register.filter(name='can_user_use_reward_points')
-def _can_user_use_reward_points(user):
+@register.filter(name='can_reward_points_be_used_by')
+def _can_reward_points_be_used_by(user):
     return can_reward_points_be_used_by(user)
 
 
diff --git a/evap/results/tools.py b/evap/results/tools.py
--- a/evap/results/tools.py
+++ b/evap/results/tools.py
@@ -290,7 +290,7 @@ def textanswers_visible_to(contribution):
     return TextAnswerVisibility(visible_by_contribution=contributors, visible_by_delegation_count=num_delegates)
 
 
-def can_user_see_textanswer(user, represented_users, textanswer, view):
+def can_textanswer_be_seen_by(user, represented_users, textanswer, view):
     assert textanswer.state in [TextAnswer.PRIVATE, TextAnswer.PUBLISHED]
     contributor = textanswer.contribution.contributor
 
diff --git a/evap/results/views.py b/evap/results/views.py
--- a/evap/results/views.py
+++ b/evap/results/views.py
@@ -15,7 +15,7 @@
 from evap.evaluation.auth import internal_required
 from evap.results.tools import (collect_results, calculate_average_distribution, distribution_to_grade,
                                 get_evaluations_with_course_result_attributes, get_single_result_rating_result,
-                                HeadingResult, TextResult, can_user_see_textanswer)
+                                HeadingResult, TextResult, can_textanswer_be_seen_by)
 
 
 def get_course_result_template_fragment_cache_key(course_id, language):
@@ -181,7 +181,7 @@ def evaluation_detail(request, semester_id, evaluation_id):
     for questionnaire_result in evaluation_result.questionnaire_results:
         for question_result in questionnaire_result.question_results:
             if isinstance(question_result, TextResult):
-                question_result.answers = [answer for answer in question_result.answers if can_user_see_textanswer(view_as_user, represented_users, answer, view)]
+                question_result.answers = [answer for answer in question_result.answers if can_textanswer_be_seen_by(view_as_user, represented_users, answer, view)]
         # remove empty TextResults
         questionnaire_result.question_results = [result for result in questionnaire_result.question_results if not isinstance(result, TextResult) or len(result.answers) > 0]
 


Test patch:
diff --git a/evap/evaluation/tests/test_models.py b/evap/evaluation/tests/test_models.py
--- a/evap/evaluation/tests/test_models.py
+++ b/evap/evaluation/tests/test_models.py
@@ -179,9 +179,7 @@ def test_single_result_can_be_deleted_only_in_reviewed(self):
     def test_single_result_can_be_published(self):
         """ Regression test for #1238 """
         responsible = mommy.make(UserProfile)
-        single_result = mommy.make(Evaluation,
-            is_single_result=True, _participant_count=5, _voter_count=5
-        )
+        single_result = mommy.make(Evaluation, is_single_result=True, _participant_count=5, _voter_count=5)
         contribution = mommy.make(Contribution,
             evaluation=single_result, contributor=responsible, can_edit=True, textanswer_visibility=Contribution.GENERAL_TEXTANSWERS,
             questionnaires=[Questionnaire.single_result_questionnaire()]
@@ -290,7 +288,7 @@ def test_publishing_and_unpublishing_effect_on_template_cache(self):
 
 
 class TestCourse(TestCase):
-    def test_can_manager_delete(self):
+    def test_can_be_deleted_by_manager(self):
         course = mommy.make(Course)
         evaluation = mommy.make(Evaluation, course=course)
         self.assertFalse(course.can_be_deleted_by_manager)
@@ -337,7 +335,7 @@ def test_is_student(self):
 
         self.assertFalse(user.is_student)
 
-    def test_can_manager_delete(self):
+    def test_can_be_deleted_by_manager(self):
         user = mommy.make(UserProfile)
         mommy.make(Evaluation, participants=[user], state="new")
         self.assertFalse(user.can_be_deleted_by_manager)
@@ -442,9 +440,7 @@ def test_evaluation_participations_are_not_archived_if_participant_count_is_set(
 
     def test_archiving_participations_doesnt_change_single_results_participant_count(self):
         responsible = mommy.make(UserProfile)
-        evaluation = mommy.make(Evaluation,
-            state="published", is_single_result=True, _participant_count=5, _voter_count=5
-        )
+        evaluation = mommy.make(Evaluation, state="published", is_single_result=True, _participant_count=5, _voter_count=5)
         contribution = mommy.make(Contribution, evaluation=evaluation, contributor=responsible, can_edit=True, textanswer_visibility=Contribution.GENERAL_TEXTANSWERS)
         contribution.questionnaires.add(Questionnaire.single_result_questionnaire())
 
diff --git a/evap/results/tests/test_tools.py b/evap/results/tests/test_tools.py
--- a/evap/results/tests/test_tools.py
+++ b/evap/results/tests/test_tools.py
@@ -10,7 +10,7 @@
 from evap.results.tools import (calculate_average_course_distribution, calculate_average_distribution, collect_results,
                                 distribution_to_grade, get_collect_results_cache_key, get_single_result_rating_result,
                                 normalized_distribution, RatingResult, textanswers_visible_to,
-                                unipolarized_distribution, can_user_see_textanswer)
+                                unipolarized_distribution, can_textanswer_be_seen_by)
 from evap.staff.tools import merge_users
 
 
@@ -359,8 +359,8 @@ def test_correct_contributors_and_delegate_count_are_shown_in_textanswer_visibil
         for user in UserProfile.objects.all():
             represented_users = [user] + list(user.represented_users.all())
             for i in range(len(textanswers)):
-                if can_user_see_textanswer(user, represented_users, textanswers[i], 'full'):
-                    if can_user_see_textanswer(user, [user], textanswers[i], 'full'):
+                if can_textanswer_be_seen_by(user, represented_users, textanswers[i], 'full'):
+                    if can_textanswer_be_seen_by(user, [user], textanswers[i], 'full'):
                         users_seeing_contribution[i][0].add(user)
                     else:
                         users_seeing_contribution[i][1].add(user)
diff --git a/evap/staff/tests/test_views.py b/evap/staff/tests/test_views.py
--- a/evap/staff/tests/test_views.py
+++ b/evap/staff/tests/test_views.py
@@ -95,7 +95,9 @@ def test_num_queries_is_constant(self):
         """
         num_users = 50
         semester = mommy.make(Semester, participations_are_archived=True)
-        evaluation = mommy.make(Evaluation, state="published", course=mommy.make(Course, semester=semester), _participant_count=1, _voter_count=1)  # this triggers more checks in UserProfile.can_manager_delete
+
+        # this triggers more checks in UserProfile.can_be_deleted_by_manager
+        evaluation = mommy.make(Evaluation, state="published", course=mommy.make(Course, semester=semester), _participant_count=1, _voter_count=1)
         mommy.make(UserProfile, _quantity=num_users, evaluations_participating_in=[evaluation])
 
         with self.assertNumQueries(FuzzyInt(0, num_users - 1)):