Coverage for src/expo_notifications/admin.py: 100%

1 from admin_anchors import admin_anchor

2 from django.contrib import admin

3 from django.utils.translation import ngettext

4

5 from expo_notifications.models import Device, Message, Receipt, Ticket

6

7

8 class DeviceAdmin(admin.ModelAdmin):

9 list_display = [

10 "__str__",

11 "is_active",

12 "date_registered",

13 "lang",

14 "user_link",

15 "messages_link",

16 ]

17 list_filter = ["is_active", "lang", "date_registered"]

18 search_fields = ["user__username", "push_token"]

19

20 def get_ordering(self, request):

21 return ["-id"]

22

23 @admin.display(description="User")

24 @admin_anchor("user")

25 def user_link(self, instance):

26 return str(instance.user)

27

28 @admin.display(description="Messages")

29 @admin_anchor("messages")

30 def messages_link(self, instance):

31 return str(instance.messages.count())

32

33

34 class MessageAdmin(admin.ModelAdmin):

35 list_display = [

36 "__str__",

37 "title",

38 "body",

39 "date_created",

40 "device_link",

41 "tickets_link",

42 ]

43 list_filter = [

44 "date_created",

45 "expiration",

46 "priority",

47 "channel_id",

48 "category_id",

49 "mutable_content",

50 ]

51 search_fields = ["title", "body", "subtitle"]

52 actions = ["send_messages"]

53

54 def get_ordering(self, request):

55 return ["-id"]

56

57 @admin.display(description="Device")

58 @admin_anchor("device")

59 def device_link(self, instance):

60 return str(instance.device)

61

62 @admin.display(description="Tickets")

63 @admin_anchor("tickets")

64 def tickets_link(self, instance):

65 return str(instance.tickets.count())

66

67 @admin.action(description="Send selected messages")

68 def send_messages(modeladmin, request, queryset):

69 queryset.send()

70

71 modeladmin.message_user(

72 request,

73 ngettext(

74 "%d message will be send.",

75 "%d messages will be send.",

76 queryset.count(),

77 )

78 % queryset.count(),

79 )

80

81

82 class ReceiptAdmin(admin.ModelAdmin):

83 list_display = ["__str__", "is_success", "date_checked", "ticket_link"]

84 list_filter = ["is_success", "date_checked"]

85

86 def get_ordering(self, request):

87 return ["-id"]

88

89 @admin.display(description="Ticket")

90 @admin_anchor("ticket")

91 def ticket_link(self, instance):

92 return str(instance.ticket)

93

94

95 class TicketAdmin(admin.ModelAdmin):

96 list_display = [

97 "__str__",

98 "is_success",

99 "date_received",

100 "external_id",

101 "message_link",

102 "receipts_link",

103 ]

104 list_filter = ["is_success", "date_received"]

105 search_fields = ["external_id"]

106 actions = ["check_tickets"]

107

108 def get_ordering(self, request):

109 return ["-id"]

110

111 @admin.display(description="Message")

112 @admin_anchor("message")

113 def message_link(self, instance):

114 return str(instance.message)

115

116 @admin.display(description="Receipts")

117 @admin_anchor("receipts")

118 def receipts_link(self, instance):

119 return str(instance.receipts.count())

120

121 @admin.action(description="Check selected tickets")

122 def check_tickets(modeladmin, request, queryset):

123 queryset.check_receipts()

124

125 modeladmin.message_user(

126 request,

127 ngettext(

128 "%d ticket receipt will be checked.",

129 "%d ticket receipts will be checked.",

130 queryset.count(),

131 )

132 % queryset.count(),

133 )

134

135

136 admin.site.register(Device, DeviceAdmin)

137 admin.site.register(Message, MessageAdmin)

138 admin.site.register(Receipt, ReceiptAdmin)

139 admin.site.register(Ticket, TicketAdmin)