Coverage for sites/ptf_tools/ptf_tools/admin.py: 77%

67 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +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 

8 

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 

16 

17from .forms import InvitationAdminAddForm 

18from .forms import InvitationAdminChangeForm 

19from .models import Invitation 

20 

21 

22class GroupInline(admin.StackedInline): 

23 model = CollectionGroup 

24 can_delete = False 

25 verbose_name_plural = "collection groups" 

26 

27 

28class GroupAdmin(BaseGroupAdmin): 

29 inlines = (GroupInline,) 

30 list_display = ("name", "get_collections") 

31 

32 def get_queryset(self, request: HttpRequest) -> QuerySet: 

33 return ( 

34 super() 

35 .get_queryset(request) 

36 .prefetch_related("collectiongroup", "collectiongroup__collections") 

37 ) 

38 

39 @admin.display(description="Collections") 

40 def get_collections(self, obj) -> str: 

41 return obj.collectiongroup.get_collections() 

42 

43 

44# Re-register GroupAdmin 

45ptf_admin.unregister(Group) 

46ptf_admin.register(Group, GroupAdmin) 

47ptf_admin.register(Page, PageAdmin) 

48ptf_admin.register(Site) 

49 

50 

51class ModeratorUserInline(admin.StackedInline): 

52 model = CommentModerator 

53 can_delete = False 

54 fields = ("is_moderator", "collections") 

55 form = UserCommentModeratorForm 

56 add_form = UserCommentModeratorForm 

57 

58 

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 ) 

70 

71 def get_queryset(self, request: HttpRequest) -> QuerySet: 

72 return super().get_queryset(request).prefetch_related("comment_moderator") 

73 

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 

81 

82 

83ptf_admin.unregister(User) 

84ptf_admin.register(User, UserAdmin) 

85 

86 

87class InvitationAdmin(admin.ModelAdmin): 

88 list_display = ("email", "first_name", "last_name", "sent", "accepted", "key") 

89 raw_id_fields = ("inviter",) 

90 

91 readonly_fields = ["accepted", "key", "sent", "inviter", "created"] 

92 

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) 

101 

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 

110 

111 

112ptf_admin.register(Invitation, InvitationAdmin)