Coverage for expo_notifications/admin.py: 79%

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 autocomplete_fields = ["user"]

20

21 def get_ordering(self, request):

22 return ["-id"]

23

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

25 @admin_anchor("user")

26 def user_link(self, instance):

27 return str(instance.user)

28

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

30 @admin_anchor("messages")

31 def messages_link(self, instance):

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

33

34

35 class MessageAdmin(admin.ModelAdmin):

36 list_display = [

37 "__str__",

38 "title",

39 "body",

40 "date_created",

41 "device_link",

42 "tickets_link",

43 ]

44 list_filter = [

45 "date_created",

46 "expiration",

47 "priority",

48 "channel_id",

49 "category_id",

50 "mutable_content",

51 ]

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

53 autocomplete_fields = ["device"]

54 actions = ["send_messages"]

55

56 def get_ordering(self, request):

57 return ["-id"]

58

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

60 @admin_anchor("device")

61 def device_link(self, instance):

62 return str(instance.device)

63

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

65 @admin_anchor("tickets")

66 def tickets_link(self, instance):

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

68

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

70 def send_messages(modeladmin, request, queryset):

71 queryset.send()

72

73 modeladmin.message_user(

74 request,

75 ngettext(

76 "%d message will be send.",

77 "%d messages will be send.",

78 queryset.count(),

79 )

80 % queryset.count(),

81 )

82

83

84 class ReceiptAdmin(admin.ModelAdmin):

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

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

87 autocomplete_fields = ["ticket"]

88

89 def get_ordering(self, request):

90 return ["-id"]

91

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

93 @admin_anchor("ticket")

94 def ticket_link(self, instance):

95 return str(instance.ticket)

96

97

98 class TicketAdmin(admin.ModelAdmin):

99 list_display = [

100 "__str__",

101 "is_success",

102 "date_received",

103 "external_id",

104 "message_link",

105 "receipts_link",

106 ]

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

108 search_fields = ["external_id"]

109 autocomplete_fields = ["message"]

110 actions = ["check_tickets"]

111

112 def get_ordering(self, request):

113 return ["-id"]

114

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

116 @admin_anchor("message")

117 def message_link(self, instance):

118 return str(instance.message)

119

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

121 @admin_anchor("receipts")

122 def receipts_link(self, instance):

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

124

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

126 def check_tickets(modeladmin, request, queryset):

127 queryset.check_receipts()

128

129 modeladmin.message_user(

130 request,

131 ngettext(

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

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

134 queryset.count(),

135 )

136 % queryset.count(),

137 )

138

139

140 admin.site.register(Device, DeviceAdmin)

141 admin.site.register(Message, MessageAdmin)

142 admin.site.register(Receipt, ReceiptAdmin)

143 admin.site.register(Ticket, TicketAdmin)