def test_remove_user_from_represented_and_ccing_users(self): delete_user = baker.make(UserProfile) delete_user2 = baker.make(UserProfile) user1 = baker.make(UserProfile, delegates=[delete_user, delete_user2], cc_users=[delete_user]) user2 = baker.make(UserProfile, delegates=[delete_user], cc_users=[delete_user, delete_user2]) messages = remove_user_from_represented_and_ccing_users(delete_user) self.assertEqual( [set(user1.delegates.all()), set(user1.cc_users.all())], [{delete_user2}, set()]) self.assertEqual( [set(user2.delegates.all()), set(user2.cc_users.all())], [set(), {delete_user2}]) self.assertEqual(len(messages), 4) messages2 = remove_user_from_represented_and_ccing_users(delete_user2) self.assertEqual( [set(user1.delegates.all()), set(user1.cc_users.all())], [set(), set()]) self.assertEqual( [set(user2.delegates.all()), set(user2.cc_users.all())], [set(), set()]) self.assertEqual(len(messages2), 2)
def test_do_nothing_if_test_run(self): delete_user = baker.make(UserProfile) user1 = baker.make(UserProfile, delegates=[delete_user], cc_users=[delete_user]) user2 = baker.make(UserProfile, delegates=[delete_user], cc_users=[delete_user]) messages = remove_user_from_represented_and_ccing_users(delete_user, test_run=True) self.assertEqual([set(user1.delegates.all()), set(user1.cc_users.all())], [{delete_user}, {delete_user}]) self.assertEqual([set(user2.delegates.all()), set(user2.cc_users.all())], [{delete_user}, {delete_user}]) self.assertEqual(len(messages), 4)
def save(self, *args, **kw): super().save(*args, **kw) new_evaluation_list = list( self.instance.evaluations_participating_in.exclude( course__semester=Semester.active_semester())) + list( self.cleaned_data.get("evaluations_participating_in")) self.instance.evaluations_participating_in.set(new_evaluation_list) manager_group = Group.objects.get(name="Manager") grade_publisher_group = Group.objects.get(name="Grade publisher") reviewer_group = Group.objects.get(name="Reviewer") if self.cleaned_data.get("is_manager"): self.instance.groups.add(manager_group) else: self.instance.groups.remove(manager_group) if self.cleaned_data.get("is_grade_publisher"): self.instance.groups.add(grade_publisher_group) else: self.instance.groups.remove(grade_publisher_group) if self.cleaned_data.get( "is_reviewer") and not self.cleaned_data.get("is_manager"): self.instance.groups.add(reviewer_group) else: self.instance.groups.remove(reviewer_group) self.instance.is_active = not self.cleaned_data.get("is_inactive") # remove instance from all other users' delegates and CC users if it is inactive self.remove_messages = ([] if self.instance.is_active else remove_user_from_represented_and_ccing_users( self.instance)) # refresh results cache if any(attribute in self.changed_data for attribute in ["first_name", "last_name", "title"]): evaluations = Evaluation.objects.filter( contributions__contributor=self.instance, state__in=STATES_WITH_RESULTS_CACHING).distinct() for evaluation in evaluations: cache_results(evaluation) self.instance.save()