Coverage for expo_notifications/managers.py: 100%

1 from typing import TYPE_CHECKING

2

3 from django.db import models, transaction

4

5 if TYPE_CHECKING:

6 from expo_notifications.models import Message # pragma: no cover

7

8

9 class MessageQueryset(models.QuerySet):

10 def send(self) -> None:

11 from expo_notifications.tasks import send_messages

12

13 message_pks = list(self.values_list("pk", flat=True))

14 send_messages.delay_on_commit(message_pks)

15

16

17 class MessageManager(models.Manager):

18 def get_queryset(self) -> MessageQueryset:

19 return MessageQueryset(self.model, using=self._db)

20

21 @transaction.atomic

22 def send(self, **kwargs) -> "Message":

23 from expo_notifications.tasks import send_messages

24

25 message = self.create(**kwargs)

26 message_pks = [message.pk]

27

28 send_messages.delay_on_commit(message_pks)

29

30 return message

31

32 @transaction.atomic

33 def bulk_send(self, *args, **kwargs) -> list["Message"]:

34 from expo_notifications.tasks import send_messages

35

36 messages = self.bulk_create(*args, **kwargs)

37 message_pks = [message.pk for message in messages]

38

39 send_messages.delay_on_commit(message_pks)

40

41 return messages