From fba8806e9732a6ce6c7e5d5719b6c7ec8f3abdef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 14 Jan 2019 22:33:28 +0200 Subject: [PATCH] Prepare for the removal of Member.team_id --- hc/accounts/models.py | 38 +++++++++++++----------- hc/accounts/tests/test_profile.py | 2 +- hc/accounts/views.py | 8 +++-- hc/api/management/commands/sendalerts.py | 2 +- hc/front/views.py | 6 ++-- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 91d7fd12..e76dd658 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -129,17 +129,19 @@ class Profile(models.Model): self.api_key_readonly = urlsafe_b64encode(os.urandom(24)).decode() self.save() - def checks_from_all_teams(self): - """ Return a queryset of checks from all teams we have access for. """ + def checks_from_all_projects(self): + """ Return a queryset of checks from projects we have access to. """ - team_ids = set(self.user.memberships.values_list("team_id", flat=True)) - team_ids.add(self.id) + is_owner = models.Q(owner=self.user) + is_member = models.Q(member__user=self.user) + q = Project.objects.filter(is_owner | is_member) + project_ids = list(q.values_list("id", flat=True)) from hc.api.models import Check - return Check.objects.filter(user__profile__id__in=team_ids) + return Check.objects.filter(project_id__in=project_ids) def send_report(self, nag=False): - checks = self.checks_from_all_teams() + checks = self.checks_from_all_projects() # Has there been a ping in last 6 months? result = checks.aggregate(models.Max("last_ping")) @@ -221,18 +223,6 @@ class Profile(models.Model): self.save() return True - def set_next_nag_date(self): - """ Set next_nag_date for all members of this team. """ - - is_owner = models.Q(id=self.id) - is_member = models.Q(user__memberships__team=self) - q = Profile.objects.filter(is_owner | is_member) - q = q.exclude(nag_period=NO_NAG) - # Exclude profiles with next_nag_date already set - q = q.filter(next_nag_date__isnull=True) - - q.update(next_nag_date=timezone.now() + models.F("nag_period")) - def get_own_project(self): project = self.user.project_set.first() if project is None: @@ -261,6 +251,18 @@ class Project(models.Model): num_used = Check.objects.filter(project__owner=self.owner).count() return self.owner_profile.check_limit - num_used + def set_next_nag_date(self): + """ Set next_nag_date on profiles of all members of this project. """ + + is_owner = models.Q(user=self.owner) + is_member = models.Q(user__memberships__project=self) + q = Profile.objects.filter(is_owner | is_member) + q = q.exclude(nag_period=NO_NAG) + # Exclude profiles with next_nag_date already set + q = q.filter(next_nag_date__isnull=True) + + q.update(next_nag_date=timezone.now() + models.F("nag_period")) + class Member(models.Model): team = models.ForeignKey(Profile, models.CASCADE) diff --git a/hc/accounts/tests/test_profile.py b/hc/accounts/tests/test_profile.py index 47a695fa..97c905ec 100644 --- a/hc/accounts/tests/test_profile.py +++ b/hc/accounts/tests/test_profile.py @@ -132,7 +132,7 @@ class ProfileTestCase(BaseTestCase): r = self.client.post("/accounts/profile/", form) self.assertEqual(r.status_code, 200) - members = self.profile.member_set.all() + members = self.project.member_set.all() self.assertEqual(members.count(), 2) frank_found = False diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 809430af..35f98797 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -259,7 +259,7 @@ def profile(request): farewell_user.profile.current_project = None farewell_user.profile.save() - Member.objects.filter(team=profile, + Member.objects.filter(project=request.project, user=farewell_user).delete() ctx["team_member_removed"] = email @@ -433,6 +433,7 @@ def unsubscribe_reports(request, username): def switch_team(request, target_username): try: target_team = Profile.objects.get(user__username=target_username) + target_project = target_team.get_own_project() except Profile.DoesNotExist: return HttpResponseForbidden() @@ -446,13 +447,14 @@ def switch_team(request, target_username): # Users can switch to teams they are members of. if not access_ok: - access_ok = request.user.memberships.filter(team=target_team).exists() + q = request.user.memberships.filter(project=target_project) + access_ok = q.exists() if not access_ok: return HttpResponseForbidden() request.profile.current_team = target_team - request.profile.current_project = target_team.get_own_project() + request.profile.current_project = target_project request.profile.save() return redirect("hc-checks") diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index bf3755a3..001cf82a 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -21,7 +21,7 @@ def notify(flip_id, stdout): # Set dates for followup nags if flip.new_status == "down": - check.project.owner_profile.set_next_nag_date() + check.project.set_next_nag_date() # Send notifications errors = flip.send_alerts() diff --git a/hc/front/views.py b/hc/front/views.py index 30d7903d..748bb0a4 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -70,7 +70,7 @@ def _get_check_for_user(request, code): if request.user.is_superuser: q = Check.objects else: - q = request.profile.checks_from_all_teams() + q = request.profile.checks_from_all_projects() try: return q.get(code=code) @@ -87,8 +87,8 @@ def _has_access(request, username): if request.user.is_superuser: return True - mq = request.user.memberships - return mq.filter(team__user__username=username).exists() + q = request.user.memberships + return q.filter(project__owner__username=username).exists() @login_required