Browse Source

Admin tweaks

pull/211/head
Pēteris Caune 6 years ago
parent
commit
e76329a364
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 37 additions and 41 deletions
  1. +15
    -10
      hc/accounts/admin.py
  2. +19
    -31
      hc/api/admin.py
  3. +3
    -0
      hc/api/models.py

+ 15
- 10
hc/accounts/admin.py View File

@ -6,7 +6,6 @@ from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from hc.accounts.models import Profile from hc.accounts.models import Profile
from hc.api.models import Channel, Check
class Fieldset: class Fieldset:
@ -88,32 +87,38 @@ class ProfileAdmin(admin.ModelAdmin):
class HcUserAdmin(UserAdmin): class HcUserAdmin(UserAdmin):
actions = ["send_report"] actions = ["send_report"]
list_display = ('id', 'email', 'date_joined', 'engagement',
list_display = ('id', 'email', 'date_joined', 'last_login', 'engagement',
'is_staff', 'checks') 'is_staff', 'checks')
list_display_links = ("id", "email")
list_filter = ("last_login", "date_joined", "is_staff", "is_active") list_filter = ("last_login", "date_joined", "is_staff", "is_active")
ordering = ["-id"] ordering = ["-id"]
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(Count("check", distinct=True))
qs = qs.annotate(Count("channel", distinct=True))
return qs
@mark_safe @mark_safe
def engagement(self, user): def engagement(self, user):
result = "" result = ""
num_checks = Check.objects.filter(user=user).count()
num_channels = Channel.objects.filter(user=user).count()
if num_checks == 0:
if user.check__count == 0:
result += "0 checks, " result += "0 checks, "
elif num_checks == 1:
elif user.check__count == 1:
result += "1 check, " result += "1 check, "
else: else:
result += "<strong>%d checks</strong>, " % num_checks
result += "<strong>%d checks</strong>, " % user.check__count
if num_channels == 0:
if user.channel__count == 0:
result += "0 channels" result += "0 channels"
elif num_channels == 1:
elif user.channel__count == 1:
result += "1 channel, " result += "1 channel, "
else: else:
result += "<strong>%d channels</strong>, " % num_channels
result += "<strong>%d channels</strong>, " % user.channel__count
return result return result


+ 19
- 31
hc/api/admin.py View File

@ -1,26 +1,12 @@
from django.contrib import admin 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
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
class OwnershipListFilter(admin.SimpleListFilter):
title = "Ownership"
parameter_name = 'ownership'
def lookups(self, request, model_admin):
return (
('assigned', "Assigned"),
)
def queryset(self, request, queryset):
if self.value() == 'assigned':
return queryset.filter(user__isnull=False)
return queryset
@admin.register(Check) @admin.register(Check)
class ChecksAdmin(admin.ModelAdmin): class ChecksAdmin(admin.ModelAdmin):
@ -31,9 +17,11 @@ class ChecksAdmin(admin.ModelAdmin):
search_fields = ["name", "user__email", "code"] search_fields = ["name", "user__email", "code"]
list_display = ("id", "name_tags", "created", "code", "timeout_schedule", list_display = ("id", "name_tags", "created", "code", "timeout_schedule",
"status", "email", "last_ping", "n_pings")
"status", "email", "last_start", "last_ping", "n_pings")
list_select_related = ("user", ) list_select_related = ("user", )
list_filter = ("status", OwnershipListFilter, "kind", "last_ping")
list_filter = ("status", "kind", "last_ping",
"last_start")
actions = ["send_alert"] actions = ["send_alert"]
def email(self, obj): def email(self, obj):
@ -136,14 +124,14 @@ class LargeTablePaginator(Paginator):
@admin.register(Ping) @admin.register(Ping)
class PingsAdmin(admin.ModelAdmin): class PingsAdmin(admin.ModelAdmin):
search_fields = ("owner__name", "owner__code", "owner__user__email") search_fields = ("owner__name", "owner__code", "owner__user__email")
readonly_fields = ("owner", )
list_select_related = ("owner", "owner__user") list_select_related = ("owner", "owner__user")
list_display = ("id", "created", "check_name", "email", "scheme", "method",
list_display = ("id", "created", "owner", "email", "scheme", "method",
"ua") "ua")
list_filter = ("created", SchemeListFilter, MethodListFilter)
paginator = LargeTablePaginator
list_filter = ("created", SchemeListFilter, MethodListFilter,
"start", "fail")
def check_name(self, obj):
return obj.owner.name if obj.owner.name else obj.owner.code
paginator = LargeTablePaginator
def email(self, obj): def email(self, obj):
return obj.owner.user.email if obj.owner.user else None return obj.owner.user.email if obj.owner.user else None
@ -163,6 +151,12 @@ class ChannelsAdmin(admin.ModelAdmin):
list_filter = ("kind", ) list_filter = ("kind", )
raw_id_fields = ("user", "checks", ) raw_id_fields = ("user", "checks", )
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(Count("notification", distinct=True))
return qs
def email(self, obj): def email(self, obj):
return obj.user.email if obj.user else None return obj.user.email if obj.user else None
@ -176,7 +170,7 @@ class ChannelsAdmin(admin.ModelAdmin):
formatted_kind.short_description = "Kind" formatted_kind.short_description = "Kind"
def num_notifications(self, obj): def num_notifications(self, obj):
return Notification.objects.filter(channel=obj).count()
return obj.notification__count
num_notifications.short_description = "# Notifications" num_notifications.short_description = "# Notifications"
@ -185,13 +179,10 @@ class ChannelsAdmin(admin.ModelAdmin):
class NotificationsAdmin(admin.ModelAdmin): class NotificationsAdmin(admin.ModelAdmin):
search_fields = ["owner__name", "owner__code", "channel__value"] search_fields = ["owner__name", "owner__code", "channel__value"]
list_select_related = ("owner", "channel") list_select_related = ("owner", "channel")
list_display = ("id", "created", "check_status", "check_name",
list_display = ("id", "created", "check_status", "owner",
"channel_kind", "channel_value") "channel_kind", "channel_value")
list_filter = ("created", "check_status", "channel__kind") list_filter = ("created", "check_status", "channel__kind")
def check_name(self, obj):
return obj.owner.name_then_code()
def channel_kind(self, obj): def channel_kind(self, obj):
return obj.channel.kind return obj.channel.kind
@ -201,9 +192,6 @@ class NotificationsAdmin(admin.ModelAdmin):
@admin.register(Flip) @admin.register(Flip)
class FlipsAdmin(admin.ModelAdmin): class FlipsAdmin(admin.ModelAdmin):
list_display = ("id", "created", "processed", "check_name", "old_status",
list_display = ("id", "created", "processed", "owner", "old_status",
"new_status") "new_status")
raw_id_fields = ("owner", ) raw_id_fields = ("owner", )
def check_name(self, obj):
return obj.owner.name if obj.owner.name else obj.owner.code

+ 3
- 0
hc/api/models.py View File

@ -82,6 +82,9 @@ class Check(models.Model):
alert_after = models.DateTimeField(null=True, blank=True, editable=False) alert_after = models.DateTimeField(null=True, blank=True, editable=False)
status = models.CharField(max_length=6, choices=STATUSES, default="new") status = models.CharField(max_length=6, choices=STATUSES, default="new")
def __str__(self):
return "%s (%d)" % (self.name or self.code, self.id)
def name_then_code(self): def name_then_code(self):
if self.name: if self.name:
return self.name return self.name


Loading…
Cancel
Save