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

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

8from portality.bll import DOAJ, exceptions 

9 

10 

11class ApplicationAssedInprogressNotify(EventConsumer): 

12 ID = "application:assed:inprogress: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") in [constants.APPLICATION_STATUS_COMPLETED, constants.APPLICATION_STATUS_READY] and \ 

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

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: 

31 return 

32 

33 svc = DOAJ.notificationsService() 

34 

35 notification = models.Notification() 

36 notification.who = application.editor 

37 notification.created_by = cls.ID 

38 notification.classification = constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE 

39 notification.long = svc.long_notification(cls.ID).format(application_title=application.bibjson().title) 

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

41 

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

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

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

45 notification.action = url_for("editor.associate_suggestions", source=string_id_query) 

46 

47 svc.notify(notification)