Coverage for portality/events/consumers/flag_assigned.py: 94%

35 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1# ~~ FlagAssigned:Consumer ~~ 

2from portality.ui.messages import Messages 

3from portality.util import url_for 

4from portality.events.consumer import EventConsumer 

5from portality import constants 

6from portality.bll import exceptions 

7from portality import models 

8from portality.bll import DOAJ 

9 

10class FlagAssigned(EventConsumer): 

11 ID = "flag:assigned:notify" 

12 

13 @classmethod 

14 def should_consume(cls, event): 

15 return event.id == constants.EVENT_FLAG_ASSIGNED and event.context.get("assignee") is not None 

16 

17 @classmethod 

18 def consume(cls, event): 

19 assignee_id = event.context.get("assignee") 

20 j_source = event.context.get("journal") 

21 

22 try: 

23 journal = models.Journal(**j_source) 

24 except Exception as e: 

25 raise exceptions.NoSuchObjectException( 

26 Messages.EXCEPTION_UNABLE_TO_CONSTRUCT_JOURNAL.format(x=e)) 

27 

28 acc = models.Account.pull(assignee_id) 

29 

30 if not acc: 

31 raise exceptions.NoSuchObjectException(Messages.EXCEPTION_NOTIFICATION_NO_ACCOUNT.format(x=assignee_id)) 

32 

33 if not acc.email: 

34 raise exceptions.NoSuchPropertyException(Messages.EXCEPTION_NOTIFICATION_NO_EMAIL.format(x=acc.id)) 

35 

36 # ~~-> Notifications:Service ~~ 

37 svc = DOAJ.notificationsService() 

38 

39 notification = models.Notification() 

40 notification.classification = constants.NOTIFICATION_CLASSIFICATION_ASSIGN 

41 notification.who = acc.id 

42 notification.created_by = cls.ID 

43 notification.long = svc.long_notification(cls.ID).format( 

44 journal_title=journal.bibjson().title, 

45 id=journal.id) 

46 

47 notification.short = svc.short_notification(cls.ID) 

48 notification.action = url_for("admin.journal_page", journal_id=journal.id) 

49 

50 svc.notify(notification) 

51 return notification