From e1b999e83abe9bca9da0c9a7c01dd53695c01359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 18 Jan 2019 14:59:01 +0200 Subject: [PATCH] Prepare for the removal of Check.user_id --- hc/accounts/admin.py | 23 ++++++++++---------- hc/accounts/tests/test_badges.py | 2 +- hc/accounts/views.py | 12 +++++------ hc/api/admin.py | 37 ++++++++++++++++++++------------ templates/accounts/badges.html | 8 +++---- 5 files changed, 45 insertions(+), 37 deletions(-) diff --git a/hc/accounts/admin.py b/hc/accounts/admin.py index 7d7331b4..22cf828d 100644 --- a/hc/accounts/admin.py +++ b/hc/accounts/admin.py @@ -52,7 +52,7 @@ class ProfileAdmin(admin.ModelAdmin): def get_queryset(self, request): qs = super(ProfileAdmin, self).get_queryset(request) qs = qs.annotate(Count("member", distinct=True)) - qs = qs.annotate(Count("user__check", distinct=True)) + qs = qs.annotate(num_checks=Count("user__project__check", distinct=True)) return qs @mark_safe @@ -66,14 +66,13 @@ class ProfileAdmin(admin.ModelAdmin): @mark_safe def checks(self, obj): - num_checks = obj.user__check__count - pct = 100 * num_checks / max(obj.check_limit, 1) + pct = 100 * obj.num_checks / max(obj.check_limit, 1) pct = min(100, int(pct)) return """   %d of %d - """ % (pct, num_checks, obj.check_limit) + """ % (pct, obj.num_checks, obj.check_limit) def invited(self, obj): return "%d of %d" % (obj.member__count, obj.team_limit) @@ -106,8 +105,8 @@ class HcUserAdmin(UserAdmin): def get_queryset(self, request): qs = super().get_queryset(request) - qs = qs.annotate(Count("check", distinct=True)) - qs = qs.annotate(Count("channel", distinct=True)) + qs = qs.annotate(num_checks=Count("project__check", distinct=True)) + qs = qs.annotate(num_channels=Count("project__channel", distinct=True)) return qs @@ -115,19 +114,19 @@ class HcUserAdmin(UserAdmin): def engagement(self, user): result = "" - if user.check__count == 0: + if user.num_checks == 0: result += "0 checks, " - elif user.check__count == 1: + elif user.num_checks == 1: result += "1 check, " else: - result += "%d checks, " % user.check__count + result += "%d checks, " % user.num_checks - if user.channel__count == 0: + if user.num_channels == 0: result += "0 channels" - elif user.channel__count == 1: + elif user.num_channels == 1: result += "1 channel, " else: - result += "%d channels, " % user.channel__count + result += "%d channels, " % user.num_channels return result diff --git a/hc/accounts/tests/test_badges.py b/hc/accounts/tests/test_badges.py index 423a93b4..25f0d72a 100644 --- a/hc/accounts/tests/test_badges.py +++ b/hc/accounts/tests/test_badges.py @@ -9,7 +9,7 @@ class BadgesTestCase(BaseTestCase): Check.objects.create(user=self.alice, tags="foo a-B_1 baz@", project=self.project) Check.objects.create(user=self.bob, tags="bobs-tag", - project=self.project) + project=self.bobs_project) r = self.client.get("/accounts/profile/badges/") self.assertContains(r, "foo.svg") diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 195814b8..9ba063f3 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -311,21 +311,21 @@ def notifications(request): def badges(request): _ensure_own_team(request) - teams = [request.profile] + projects = [request.project] for membership in request.user.memberships.all(): - teams.append(membership.team) + projects.append(membership.project) badge_sets = [] - for team in teams: + for project in projects: tags = set() - for check in Check.objects.filter(user=team.user): + for check in Check.objects.filter(project=project): tags.update(check.tags_list()) sorted_tags = sorted(tags, key=lambda s: s.lower()) sorted_tags.append("*") # For the "overall status" badge urls = [] - username = team.user.username + username = project.owner.username for tag in sorted_tags: if not re.match("^[\w-]+$", tag) and tag != "*": continue @@ -335,7 +335,7 @@ def badges(request): "json": get_badge_url(username, tag, format="json"), }) - badge_sets.append({"team": team, "urls": urls}) + badge_sets.append({"project": project, "urls": urls}) ctx = { "page": "profile", diff --git a/hc/api/admin.py b/hc/api/admin.py index 8b57463a..92bc6e66 100644 --- a/hc/api/admin.py +++ b/hc/api/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django.core.paginator import Paginator from django.db import connection -from django.db.models import Count +from django.db.models import Count, F from django.utils.safestring import mark_safe from hc.api.models import Channel, Check, Flip, Notification, Ping from hc.lib.date import format_duration @@ -15,17 +15,22 @@ class ChecksAdmin(admin.ModelAdmin): 'all': ('css/admin/checks.css',) } - search_fields = ["name", "user__email", "code"] - list_display = ("id", "name_tags", "created", "code", "timeout_schedule", - "status", "email", "last_start", "last_ping", "n_pings") - list_select_related = ("user", ) + search_fields = ["name", "code", "project__owner__email"] + raw_id_fields = ("project", ) + list_display = ("id", "name_tags", "email", "created", "n_pings", + "timeout_schedule", "status", "last_start", "last_ping") list_filter = ("status", "kind", "last_ping", "last_start") actions = ["send_alert"] + def get_queryset(self, request): + qs = super().get_queryset(request) + qs = qs.annotate(email=F("project__owner__email")) + return qs + def email(self, obj): - return obj.user.email if obj.user else None + return obj.email def name_tags(self, obj): if not obj.tags: @@ -137,9 +142,9 @@ class LargeTablePaginator(Paginator): @admin.register(Ping) class PingsAdmin(admin.ModelAdmin): - search_fields = ("owner__name", "owner__code", "owner__user__email") + search_fields = ("owner__name", "owner__code") readonly_fields = ("owner", ) - list_select_related = ("owner", "owner__user") + list_select_related = ("owner", ) list_display = ("id", "created", "owner", "email", "scheme", "method", "ua") list_filter = ("created", SchemeListFilter, MethodListFilter, @@ -147,8 +152,13 @@ class PingsAdmin(admin.ModelAdmin): paginator = LargeTablePaginator + def get_queryset(self, request): + qs = super().get_queryset(request) + qs = qs.annotate(email=F("owner__project__owner__email")) + return qs + def email(self, obj): - return obj.owner.user.email if obj.owner.user else None + return obj.email @admin.register(Channel) @@ -158,21 +168,20 @@ class ChannelsAdmin(admin.ModelAdmin): 'all': ('css/admin/channels.css',) } - search_fields = ["value", "user__email"] - list_select_related = ("user", ) + search_fields = ["value", "project__owner__email"] list_display = ("id", "name", "email", "formatted_kind", "value", "num_notifications") list_filter = ("kind", ) - raw_id_fields = ("user", "checks", ) + raw_id_fields = ("project", "checks", ) def get_queryset(self, request): qs = super().get_queryset(request) qs = qs.annotate(Count("notification", distinct=True)) - + qs = qs.annotate(email=F("project__owner__email")) return qs def email(self, obj): - return obj.user.email if obj.user else None + return obj.email @mark_safe def formatted_kind(self, obj): diff --git a/templates/accounts/badges.html b/templates/accounts/badges.html index 7f9311e6..260c9269 100644 --- a/templates/accounts/badges.html +++ b/templates/accounts/badges.html @@ -29,8 +29,8 @@

{% site_name %} provides status badges for each of the tags you have used. Additionally, the "{% site_name %}" - badge shows the overall status of all checks in your - account. The badges have public, but hard-to-guess + badge shows the overall status of all checks in a + project. The badges have public, but hard-to-guess URLs. You can use them in your READMEs, dashboards or status pages.

@@ -47,7 +47,7 @@ {% for badge_set in badges %} - + {% for urldict in badge_set.urls %} @@ -64,7 +64,7 @@
{{ badge_set.team }}{{ badge_set.project }}
{% for badge_set in badges %} - + {% for urldict in badge_set.urls %}
{{ badge_set.team }}{{ badge_set.project }}