Coverage for src/ptf_tools/admin.py: 75%
67 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 12:11 +0000
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 12:11 +0000
1from django.contrib import admin
2from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin
3from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
4from django.contrib.auth.models import Group
5from django.contrib.auth.models import User
6from django.db.models import QuerySet
7from django.http import HttpRequest
9from comments_moderation.forms import UserCommentModeratorForm
10from comments_moderation.models import CommentModerator
11from mersenne_cms.admin import PageAdmin
12from mersenne_cms.admin import Site
13from mersenne_cms.models import Page
14from ptf.admin import ptf_admin
15from ptf_tools.models import CollectionGroup
17from .forms import InvitationAdminAddForm
18from .forms import InvitationAdminChangeForm
19from .models import Invitation
22class GroupInline(admin.StackedInline):
23 model = CollectionGroup
24 can_delete = False
25 verbose_name_plural = "collection groups"
28class GroupAdmin(BaseGroupAdmin):
29 inlines = (GroupInline,)
30 list_display = ("name", "get_collections")
32 def get_queryset(self, request: HttpRequest) -> QuerySet:
33 return (
34 super()
35 .get_queryset(request)
36 .prefetch_related("collectiongroup", "collectiongroup__collections")
37 )
39 @admin.display(description="Collections")
40 def get_collections(self, obj) -> str:
41 return obj.collectiongroup.get_collections()
44# Re-register GroupAdmin
45ptf_admin.unregister(Group)
46ptf_admin.register(Group, GroupAdmin)
47ptf_admin.register(Page, PageAdmin)
48ptf_admin.register(Site)
51class ModeratorUserInline(admin.StackedInline):
52 model = CommentModerator
53 can_delete = False
54 fields = ("is_moderator", "collections")
55 form = UserCommentModeratorForm
56 add_form = UserCommentModeratorForm
59class UserAdmin(BaseUserAdmin):
60 inlines = (ModeratorUserInline,)
61 list_display = (
62 "username",
63 "email",
64 "first_name",
65 "last_name",
66 "is_staff",
67 "is_superuser",
68 "is_moderator",
69 )
71 def get_queryset(self, request: HttpRequest) -> QuerySet:
72 return super().get_queryset(request).prefetch_related("comment_moderator")
74 @admin.display(
75 description="Moderator",
76 ordering="comment_moderator__is_moderator",
77 )
78 @admin.display(boolean=True)
79 def is_moderator(self, obj) -> bool:
80 return hasattr(obj, "comment_moderator") and obj.comment_moderator.is_moderator
83ptf_admin.unregister(User)
84ptf_admin.register(User, UserAdmin)
87class InvitationAdmin(admin.ModelAdmin):
88 list_display = ("email", "first_name", "last_name", "sent", "accepted", "key")
89 raw_id_fields = ("inviter",)
91 readonly_fields = ["accepted", "key", "sent", "inviter", "created"]
93 def get_form(self, request, obj=None, **kwargs):
94 if obj:
95 kwargs["form"] = InvitationAdminChangeForm
96 else:
97 kwargs["form"] = InvitationAdminAddForm
98 kwargs["form"].user = request.user
99 kwargs["form"].request = request
100 return super().get_form(request, obj, **kwargs)
102 def get_readonly_fields(self, request: HttpRequest, obj):
103 """
104 Disable editing the email field once the invite has already been sent.
105 """
106 readonly_fields = list(self.readonly_fields)
107 if obj:
108 readonly_fields += ["email"]
109 return readonly_fields
112ptf_admin.register(Invitation, InvitationAdmin)