Coverage for portality/events/consumers/application_publisher_accepted_notify.py: 89%

46 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, dates 

8from portality.bll import DOAJ, exceptions 

9from portality.core import app 

10 

11 

12class ApplicationPublisherAcceptedNotify(EventConsumer): 

13 ID = "application:publisher:accepted:notify" 

14 

15 @classmethod 

16 def consumes(cls, event): 

17 if event.id != constants.EVENT_APPLICATION_STATUS: 

18 return False 

19 

20 # TODO: in the long run this needs to move out to the user's email preferences but for now it 

21 # is here to replicate the behaviour in the code it replaces 

22 if not app.config.get("ENABLE_PUBLISHER_EMAIL", False): 

23 return False 

24 

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

26 if app_source is None: 

27 return False 

28 

29 if event.context.get("new_status") != constants.APPLICATION_STATUS_ACCEPTED: 

30 return False 

31 

32 try: 

33 application = models.Application(**app_source) 

34 except Exception as e: 

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

36 

37 is_new_application = application.application_type == constants.APPLICATION_TYPE_NEW_APPLICATION 

38 return is_new_application 

39 

40 @classmethod 

41 def consume(cls, event): 

42 # TODO: in the long run this needs to move out to the user's email preferences but for now it 

43 # is here to replicate the behaviour in the code it replaces 

44 if not app.config.get("ENABLE_PUBLISHER_EMAIL", False): 

45 return 

46 

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

48 

49 try: 

50 application = models.Application(**app_source) 

51 except Exception as e: 

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

53 

54 if not application.owner: 

55 return 

56 

57 svc = DOAJ.notificationsService() 

58 

59 notification = models.Notification() 

60 notification.who = application.owner 

61 notification.created_by = cls.ID 

62 notification.classification = constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE 

63 

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

65 application_title=application.bibjson().title, 

66 application_date=dates.human_date(application.date_applied), 

67 publisher_dashboard_url=app.config.get('BASE_URL', "https://doaj.org") + url_for("publisher.journals"), 

68 faq_url=app.config.get('BASE_URL', "https://doaj.org") + url_for("doaj.faq") 

69 ) 

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

71 

72 notification.action = url_for("publisher.journals") 

73 

74 svc.notify(notification)