Coverage for portality / events / consumers / application_editor_completed_notify.py: 91%

45 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-04 09:41 +0100

1# ~~ApplicationEditorCompletedNotify:Consumer~~ 

2from portality.events import consumer_utils 

3from portality.util import url_for 

4from portality.events.consumer import EventConsumer 

5from portality import constants 

6from portality import models 

7from portality.bll import DOAJ, exceptions 

8from portality.lib.seamless import SeamlessException 

9 

10 

11class ApplicationEditorCompletedNotify(EventConsumer): 

12 ID = "application:editor:completed:notify" 

13 

14 @classmethod 

15 def should_consume(cls, event): 

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

17 event.context.get("old_status") != constants.APPLICATION_STATUS_COMPLETED and \ 

18 event.context.get("new_status") == constants.APPLICATION_STATUS_COMPLETED 

19 

20 @classmethod 

21 def consume(cls, event): 

22 context = event.context 

23 app = context.get("application") 

24 if app is None: 

25 return None 

26 try: 

27 application = models.Application(**app) 

28 except SeamlessException: 

29 raise exceptions.NoSuchObjectException("Unable to create application model from source") 

30 

31 if application is None: 

32 raise exceptions.NoSuchObjectException("Unable to create application model from source") 

33 

34 if not application.editor_group: 

35 return None 

36 

37 # The associate editor in question is who created this event 

38 associate_editor = "unknown associate editor" 

39 if application.editor: 

40 associate_editor = event.who 

41 

42 # Notification is to the editor in charge of this application's assigned editor group 

43 editor_group_name = application.editor_group 

44 editor_group_id = models.EditorGroup.group_exists_by_name(name=editor_group_name) 

45 eg = models.EditorGroup.pull(editor_group_id) 

46 group_editor = eg.editor 

47 

48 if not group_editor: 

49 return None 

50 

51 # ~~-> Notifications:Service ~~ 

52 svc = DOAJ.notificationsService() 

53 

54 notification = models.Notification() 

55 notification.who = group_editor 

56 notification.created_by = cls.ID 

57 notification.classification = constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE 

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

59 application_title=application.bibjson().title, 

60 associate_editor=associate_editor 

61 ) 

62 notification.short = svc.short_notification(cls.ID).format( 

63 issns=application.bibjson().issns_as_text() 

64 ) 

65 notification.action = url_for("editor.application", application_id=application.id) 

66 

67 svc.notify(notification) 

68 return notification