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

45 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-04 15:38 +0100

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.lib import edges 

8from portality.bll import DOAJ, exceptions 

9from portality.lib.seamless import SeamlessException 

10 

11 

12class ApplicationEditorCompletedNotify(EventConsumer): 

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

14 

15 @classmethod 

16 def consumes(cls, event): 

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

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

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

20 

21 @classmethod 

22 def consume(cls, event): 

23 context = event.context 

24 app = context.get("application") 

25 if app is None: 

26 return 

27 try: 

28 application = models.Application(**app) 

29 except SeamlessException: 

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

31 

32 if application is None: 

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

34 

35 if not application.editor_group: 

36 return 

37 

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

39 associate_editor = "unknown associate editor" 

40 if application.editor: 

41 associate_editor = event.who 

42 

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

44 editor_group_name = application.editor_group 

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

46 eg = models.EditorGroup.pull(editor_group_id) 

47 group_editor = eg.editor 

48 

49 if not group_editor: 

50 return 

51 

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) 

63 

64 # don't make the escaped query, as url_for is also going to escape it, and it will wind up double-escaped! 

65 string_id_query = edges.make_query_json(query_string=application.id) 

66 # note we're using the doaj url_for wrapper, not the flask url_for directly, due to the request context hack required 

67 notification.action = url_for("editor.group_suggestions", source=string_id_query) 

68 

69 svc.notify(notification)