From 749114d856cd6c9e37e8f3c4beaf9f6aa790dc07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 1 Aug 2015 20:24:56 +0300 Subject: [PATCH] Nicer Django admin --- hc/accounts/admin.py | 17 +++++++++++++++++ hc/api/admin.py | 27 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/hc/accounts/admin.py b/hc/accounts/admin.py index 8c38f3f3..58fe51eb 100644 --- a/hc/accounts/admin.py +++ b/hc/accounts/admin.py @@ -1,3 +1,20 @@ from django.contrib import admin # Register your models here. +from django.contrib.auth.admin import UserAdmin +from django.contrib.auth.models import User + +from hc.api.models import Check + + +class HcUserAdmin(UserAdmin): + list_display = ('id', 'username', 'email', 'date_joined', 'num_checks', + 'is_staff') + + ordering = ["-id"] + + def num_checks(self, user): + return Check.objects.filter(user=user).count() + +admin.site.unregister(User) +admin.site.register(User, HcUserAdmin) diff --git a/hc/api/admin.py b/hc/api/admin.py index db7e14e3..de15171a 100644 --- a/hc/api/admin.py +++ b/hc/api/admin.py @@ -3,6 +3,21 @@ from django.contrib import admin from hc.api.models import Check, Ping +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) class ChecksAdmin(admin.ModelAdmin): @@ -16,6 +31,8 @@ class ChecksAdmin(admin.ModelAdmin): list_select_related = ("user", ) actions = ["send_alert"] + list_filter = ("status", OwnershipListFilter) + def email(self, obj): return obj.user.email if obj.user else None @@ -30,4 +47,12 @@ class ChecksAdmin(admin.ModelAdmin): @admin.register(Ping) class PingsAdmin(admin.ModelAdmin): - list_display = ("id", "created", "owner", "method", "ua") + list_select_related = ("owner", ) + list_display = ("id", "created", "check_name", "email", "scheme", "method", + "ua") + + def check_name(self, obj): + return obj.owner.name if obj.owner.name else obj.owner.code + + def email(self, obj): + return obj.owner.user.email if obj.owner.user else None \ No newline at end of file