Browse Source

Prepare for the removal of Check.user_id

pull/214/head
Pēteris Caune 6 years ago
parent
commit
e1b999e83a
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 45 additions and 37 deletions
  1. +11
    -12
      hc/accounts/admin.py
  2. +1
    -1
      hc/accounts/tests/test_badges.py
  3. +6
    -6
      hc/accounts/views.py
  4. +23
    -14
      hc/api/admin.py
  5. +4
    -4
      templates/accounts/badges.html

+ 11
- 12
hc/accounts/admin.py View File

@ -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 """
<span class="bar"><span style="width: %dpx"></span></span>
&nbsp; %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 += "<strong>%d checks</strong>, " % user.check__count
result += "<strong>%d checks</strong>, " % 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 += "<strong>%d channels</strong>, " % user.channel__count
result += "<strong>%d channels</strong>, " % user.num_channels
return result


+ 1
- 1
hc/accounts/tests/test_badges.py View File

@ -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")


+ 6
- 6
hc/accounts/views.py View File

@ -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",


+ 23
- 14
hc/api/admin.py View File

@ -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):


+ 4
- 4
templates/accounts/badges.html View File

@ -29,8 +29,8 @@
<p id="badges-description">
{% 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.
</p>
@ -47,7 +47,7 @@
<table id="badges-svg" class="badges table">
{% for badge_set in badges %}
<tr>
<th colspan="2">{{ badge_set.team }}</th>
<th colspan="2">{{ badge_set.project }}</th>
</tr>
{% for urldict in badge_set.urls %}
<tr>
@ -64,7 +64,7 @@
<table id="badges-json" class="badges table">
{% for badge_set in badges %}
<tr>
<th colspan="2">{{ badge_set.team }}</th>
<th colspan="2">{{ badge_set.project }}</th>
</tr>
{% for urldict in badge_set.urls %}


Loading…
Cancel
Save