Browse Source

Admin tweaks.

pull/366/head
Pēteris Caune 5 years ago
parent
commit
abdff95ce8
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 67 additions and 74 deletions
  1. +32
    -64
      hc/accounts/admin.py
  2. +28
    -10
      hc/api/admin.py
  3. +7
    -0
      static/css/admin/checks.css

+ 32
- 64
hc/accounts/admin.py View File

@ -9,6 +9,27 @@ from django.utils.safestring import mark_safe
from hc.accounts.models import Profile, Project from hc.accounts.models import Profile, Project
@mark_safe
def _format_usage(num_checks, num_channels):
result = ""
if num_checks == 0:
result += "0 checks, "
elif num_checks == 1:
result += "1 check, "
else:
result += f"<strong>{num_checks} checks</strong>, "
if num_channels == 0:
result += "0 channels"
elif num_channels == 1:
result += "1 channel"
else:
result += f"<strong>{num_channels} channels</strong>"
return result
class Fieldset: class Fieldset:
name = None name = None
fields = [] fields = []
@ -56,9 +77,9 @@ class ProfileAdmin(admin.ModelAdmin):
list_display = ( list_display = (
"id", "id",
"email", "email",
"engagement",
"usage",
"date_joined", "date_joined",
"last_login",
"last_active_date",
"projects", "projects",
"invited", "invited",
"sms", "sms",
@ -66,7 +87,7 @@ class ProfileAdmin(admin.ModelAdmin):
) )
list_filter = ( list_filter = (
"user__date_joined", "user__date_joined",
"user__last_login",
"last_active_date",
"reports_allowed", "reports_allowed",
"check_limit", "check_limit",
) )
@ -82,25 +103,8 @@ class ProfileAdmin(admin.ModelAdmin):
qs = qs.annotate(plan=F("user__subscription__plan_name")) qs = qs.annotate(plan=F("user__subscription__plan_name"))
return qs return qs
@mark_safe
def engagement(self, obj):
result = ""
if obj.num_checks == 0:
result += "0 checks, "
elif obj.num_checks == 1:
result += "1 check, "
else:
result += "<strong>%d checks</strong>, " % obj.num_checks
if obj.num_channels == 0:
result += "0 channels"
elif obj.num_channels == 1:
result += "1 channel, "
else:
result += "<strong>%d channels</strong>, " % obj.num_channels
return result
def usage(self, obj):
return _format_usage(obj.num_checks, obj.num_channels)
@mark_safe @mark_safe
def email(self, obj): def email(self, obj):
@ -110,9 +114,6 @@ class ProfileAdmin(admin.ModelAdmin):
return s return s
def last_login(self, obj):
return obj.user.last_login
def date_joined(self, obj): def date_joined(self, obj):
return obj.user.date_joined return obj.user.date_joined
@ -131,7 +132,7 @@ class ProfileAdmin(admin.ModelAdmin):
class ProjectAdmin(admin.ModelAdmin): class ProjectAdmin(admin.ModelAdmin):
readonly_fields = ("code", "owner") readonly_fields = ("code", "owner")
list_select_related = ("owner",) list_select_related = ("owner",)
list_display = ("id", "name_", "users", "engagement", "switch")
list_display = ("id", "name_", "users", "usage", "switch")
search_fields = ["id", "name", "owner__email"] search_fields = ["id", "name", "owner__email"]
class Media: class Media:
@ -160,25 +161,8 @@ class ProjectAdmin(admin.ModelAdmin):
def email(self, obj): def email(self, obj):
return obj.owner.email return obj.owner.email
@mark_safe
def engagement(self, obj):
result = ""
if obj.num_checks == 0:
result += "0 checks, "
elif obj.num_checks == 1:
result += "1 check, "
else:
result += "<strong>%d checks</strong>, " % obj.num_checks
if obj.num_channels == 0:
result += "0 channels"
elif obj.num_channels == 1:
result += "1 channel, "
else:
result += "<strong>%d channels</strong>, " % obj.num_channels
return result
def usage(self, obj):
return _format_usage(obj.num_checks, obj.num_channels)
@mark_safe @mark_safe
def switch(self, obj): def switch(self, obj):
@ -191,7 +175,7 @@ class HcUserAdmin(UserAdmin):
list_display = ( list_display = (
"id", "id",
"email", "email",
"engagement",
"usage",
"date_joined", "date_joined",
"last_login", "last_login",
"is_staff", "is_staff",
@ -210,24 +194,8 @@ class HcUserAdmin(UserAdmin):
return qs return qs
@mark_safe @mark_safe
def engagement(self, user):
result = ""
if user.num_checks == 0:
result += "0 checks, "
elif user.num_checks == 1:
result += "1 check, "
else:
result += "<strong>%d checks</strong>, " % user.num_checks
if user.num_channels == 0:
result += "0 channels"
elif user.num_channels == 1:
result += "1 channel, "
else:
result += "<strong>%d channels</strong>, " % user.num_channels
return result
def usage(self, user):
return _format_usage(user.num_checks, user.num_channels)
def send_report(self, request, qs): def send_report(self, request, qs):
for user in qs: for user in qs:


+ 28
- 10
hc/api/admin.py View File

@ -2,6 +2,8 @@ from django.contrib import admin
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db import connection from django.db import connection
from django.db.models import Count, F from django.db.models import Count, F
from django.urls import reverse
from django.utils.html import escape
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from hc.api.models import Channel, Check, Flip, Notification, Ping from hc.api.models import Channel, Check, Flip, Notification, Ping
from hc.lib.date import format_duration from hc.lib.date import format_duration
@ -17,7 +19,7 @@ class ChecksAdmin(admin.ModelAdmin):
list_display = ( list_display = (
"id", "id",
"name_tags", "name_tags",
"email",
"project_",
"created", "created",
"n_pings", "n_pings",
"timeout_schedule", "timeout_schedule",
@ -32,16 +34,26 @@ class ChecksAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
qs = super().get_queryset(request) qs = super().get_queryset(request)
qs = qs.annotate(email=F("project__owner__email")) qs = qs.annotate(email=F("project__owner__email"))
qs = qs.annotate(project_name=F("project__name"))
return qs return qs
def email(self, obj):
return obj.email
@mark_safe
def project_(self, obj):
url = reverse("hc-checks", args=[obj.project.code])
name = escape(obj.project_name or "Default")
email = escape(obj.email)
return f'<a href="{url}"">{name}</a> &bull; {email}'
@mark_safe
def name_tags(self, obj): def name_tags(self, obj):
if not obj.tags:
return obj.name
url = reverse("hc-details", args=[obj.code])
name = escape(obj.name or "unnamed")
s = f'<a href="{url}"">{name}</a>'
for tag in obj.tags_list():
s += " <span>%s</span>" % escape(tag)
return "%s [%s]" % (obj.name, obj.tags)
return s
def timeout_schedule(self, obj): def timeout_schedule(self, obj):
if obj.kind == "simple": if obj.kind == "simple":
@ -166,7 +178,7 @@ class ChannelsAdmin(admin.ModelAdmin):
list_display = ( list_display = (
"id", "id",
"name", "name",
"email",
"project_",
"formatted_kind", "formatted_kind",
"value", "value",
"num_notifications", "num_notifications",
@ -174,15 +186,20 @@ class ChannelsAdmin(admin.ModelAdmin):
list_filter = ("kind",) list_filter = ("kind",)
raw_id_fields = ("project", "checks") raw_id_fields = ("project", "checks")
@mark_safe
def project_(self, obj):
url = reverse("hc-checks", args=[obj.project.code])
name = escape(obj.project_name or "Default")
email = escape(obj.email)
return f"<a href='{url}'>{name}</a> &bull; {email}"
def get_queryset(self, request): def get_queryset(self, request):
qs = super().get_queryset(request) qs = super().get_queryset(request)
qs = qs.annotate(Count("notification", distinct=True)) qs = qs.annotate(Count("notification", distinct=True))
qs = qs.annotate(project_name=F("project__name"))
qs = qs.annotate(email=F("project__owner__email")) qs = qs.annotate(email=F("project__owner__email"))
return qs return qs
def email(self, obj):
return obj.email
@mark_safe @mark_safe
def formatted_kind(self, obj): def formatted_kind(self, obj):
if obj.kind == "email" and not obj.email_verified: if obj.kind == "email" and not obj.email_verified:
@ -213,6 +230,7 @@ class NotificationsAdmin(admin.ModelAdmin):
"error", "error",
) )
list_filter = ("created", "check_status", "channel__kind") list_filter = ("created", "check_status", "channel__kind")
raw_id_fields = ("channel",)
def channel_kind(self, obj): def channel_kind(self, obj):
return obj.channel.kind return obj.channel.kind


+ 7
- 0
static/css/admin/checks.css View File

@ -1,4 +1,11 @@
.field-code { .field-code {
font-family: monospace; font-family: monospace;
font-size: 80%; font-size: 80%;
}
.field-name_tags span {
color: #555;
background: #EEE;
padding: 1px 4px;
border-radius: 2px;
} }

Loading…
Cancel
Save