diff --git a/src/fellchensammlung/admin.py b/src/fellchensammlung/admin.py index 5ce0996..0e1ca6b 100644 --- a/src/fellchensammlung/admin.py +++ b/src/fellchensammlung/admin.py @@ -1,11 +1,14 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib import admin from django.utils.html import format_html +from django.urls import reverse +from django.utils.http import urlencode from .models import User, Language, Text, ReportComment, ReportAdoptionNotice, Log, Timestamp from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule, Image, ModerationAction, \ Comment, Report, Announcement, AdoptionNoticeStatus, User, Subscriptions +from django.utils.translation import gettext_lazy as _ class StatusInline(admin.StackedInline): @@ -15,13 +18,27 @@ class StatusInline(admin.StackedInline): @admin.register(AdoptionNotice) class AdoptionNoticeAdmin(admin.ModelAdmin): search_fields = ("name__icontains", "description__icontains") + list_filter = ("owner",) inlines = [ StatusInline, ] # Re-register UserAdmin -admin.site.register(User) +@admin.register(User) +class UserAdmin(admin.ModelAdmin): + search_fields = ("usernamname__icontains", "first_name__icontains", "last_name__icontains", "email__icontains") + list_display = ("username", "email", "trust_level", "is_active", "view_adoption_notices") + list_filter = ("is_active", "trust_level",) + + def view_adoption_notices(self, obj): + count = obj.adoption_notices.count() + url = ( + reverse("admin:fellchensammlung_adoptionnotice_changelist") + + "?" + + urlencode({"owner__id": f"{obj.id}"}) + ) + return format_html('{} Adoption Notices', url, count) def _reported_content_link(obj): diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index f94b55d..fce205f 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -74,6 +74,10 @@ class User(AbstractUser): def get_num_unread_notifications(self): return BaseNotification.objects.filter(user=self, read=False).count() + @property + def adoption_notices(self): + return AdoptionNotice.objects.filter(owner=self) + @property def owner(self): return self