Coverage for portality/events/consumers/application_maned_ready_notify.py: 95%

40 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-09-13 22:06 +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 

9 

10 

11class ApplicationManedReadyNotify(EventConsumer): 

12 ID = "application:maned:ready:notify" 

13 

14 @classmethod 

15 def consumes(cls, event): 

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

17 event.context.get("application") is not None and \ 

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

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

20 

21 @classmethod 

22 def consume(cls, event): 

23 app_source = event.context.get("application") 

24 

25 try: 

26 application = models.Application(**app_source) 

27 except Exception as e: 

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

29 

30 if not application.editor_group: 

31 return 

32 

33 

34 eg = models.EditorGroup.pull_by_key("name", application.editor_group) 

35 managing_editor = eg.maned 

36 if not managing_editor: 

37 return 

38 

39 editor = eg.editor 

40 if not editor: 

41 editor = "unknown editor" 

42 

43 svc = DOAJ.notificationsService() 

44 

45 notification = models.Notification() 

46 notification.who = managing_editor 

47 notification.created_by = cls.ID 

48 notification.classification = constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE 

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

50 application_title=application.bibjson().title, 

51 editor=editor 

52 ) 

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

54 

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

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

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

58 if application.application_type == constants.APPLICATION_TYPE_NEW_APPLICATION: 

59 url_for_application = url_for("admin.suggestions", source=string_id_query) 

60 else: 

61 url_for_application = url_for("admin.update_requests", source=string_id_query) 

62 notification.action = url_for_application 

63 

64 svc.notify(notification)