Coverage for src/expo_notifications/managers/message_manager.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

15 if message_pks:

16 send_messages.delay_on_commit(message_pks)

17

18

19 class MessageManager(models.Manager):

20 def get_queryset(self) -> MessageQueryset:

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

22

23 @transaction.atomic

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

25 from expo_notifications.tasks import send_messages

26

27 message = self.create(**kwargs)

28 message_pks = [message.pk]

29

30 send_messages.delay_on_commit(message_pks)

31

32 return message

33

34 @transaction.atomic

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

36 from expo_notifications.tasks import send_messages

37

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

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

40

41 if message_pks:

42 send_messages.delay_on_commit(message_pks)

43

44 return messages