Coverage for portality/events/consumers/journal_editor_group_assigned_notify.py: 97%

32 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-11-09 15:10 +0000

1# from flask import url_for 

2from portality.util import url_for 

3 

4from portality.events.consumer import EventConsumer 

5from portality import constants 

6from portality import models 

7from portality.bll import DOAJ 

8from portality.bll import exceptions 

9 

10 

11class JournalEditorGroupAssignedNotify(EventConsumer): 

12 ID = "journal:editor_group:assigned:notify" 

13 

14 @classmethod 

15 def consumes(cls, event): 

16 return event.id == constants.EVENT_JOURNAL_EDITOR_GROUP_ASSIGNED and \ 

17 event.context.get("journal") is not None 

18 

19 @classmethod 

20 def consume(cls, event): 

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

22 

23 try: 

24 journal = models.Journal(**j_source) 

25 except Exception as e: 

26 raise exceptions.NoSuchObjectException("Unable to construct Journal from supplied source - data structure validation error, {x}".format(x=e)) 

27 

28 if not journal.editor_group: 

29 return 

30 

31 editor_group = models.EditorGroup.pull_by_key("name", journal.editor_group) 

32 

33 if not editor_group.editor: 

34 raise exceptions.NoSuchPropertyException("Editor Group {x} does not have property `editor`".format(x=editor_group.id)) 

35 

36 svc = DOAJ.notificationsService() 

37 

38 notification = models.Notification() 

39 notification.who = editor_group.editor 

40 notification.created_by = cls.ID 

41 notification.classification = constants.NOTIFICATION_CLASSIFICATION_ASSIGN 

42 

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

44 journal_name=journal.bibjson().title 

45 ) 

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

47 notification.action = url_for("editor.journal_page", journal_id=journal.id) 

48 

49 svc.notify(notification)