Coverage for expo_notifications/models/ticket.py: 76%

1 from django.db import models

2 from exponent_server_sdk import PushTicket

3

4 from expo_notifications.managers import TicketManager

5

6

7 class Ticket(models.Model):

8 objects = TicketManager()

9

10 message = models.ForeignKey(

11 to="expo_notifications.Message",

12 on_delete=models.CASCADE,

13 related_name="tickets",

14 )

15

16 is_success = models.BooleanField()

17

18 external_id = models.CharField(

19 max_length=64,

20 blank=True,

21 )

22

23 error_message = models.TextField(

24 blank=True,

25 )

26

27 date_received = models.DateTimeField()

28

29 def __str__(self) -> str:

30 return f"Ticket #{self.pk}"

31

32 def to_push_ticket(self) -> PushTicket:

33 return PushTicket(

34 push_message=self.message.to_push_message(),

35 status=(

36 PushTicket.SUCCESS_STATUS

37 if self.is_success

38 else PushTicket.ERROR_STATUS

39 ),

40 message=self.error_message or None,

41 details=None,

42 id=self.external_id,

43 )

44

45 def check_receipt(self) -> None:

46 from expo_notifications.tasks import check_receipts

47

48 check_receipts.delay_on_commit([self.pk])