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

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.bll import DOAJ 

8from portality.bll import exceptions 

9from portality.lib import dates 

10from portality.core import app 

11 

12 

13class ApplicationPublisherAssignedNotify(EventConsumer): 

14 ID = "application:publisher:assigned:notify" 

15 

16 @classmethod 

17 def consumes(cls, event): 

18 if event.id != constants.EVENT_APPLICATION_ASSED_ASSIGNED: 

19 return False 

20 

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

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

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

24 return False 

25 

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

27 if app_source is None: 

28 return False 

29 

30 if event.context.get("old_editor") not in [None, ""]: 

31 return False 

32 

33 if event.context.get("new_editor") in [None, ""]: 

34 return False 

35 

36 try: 

37 application = models.Application(**app_source) 

38 except Exception as e: 

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

40 

41 is_new_application = application.application_type == constants.APPLICATION_TYPE_NEW_APPLICATION 

42 return is_new_application 

43 

44 @classmethod 

45 def consume(cls, event): 

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

47 

48 try: 

49 application = models.Application(**app_source) 

50 except Exception as e: 

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

52 

53 if not application.owner: 

54 raise exceptions.NoSuchPropertyException("Application {x} does not have property `owner`".format(x=application.id)) 

55 

56 svc = DOAJ.notificationsService() 

57 

58 notification = models.Notification() 

59 notification.who = application.owner 

60 notification.created_by = cls.ID 

61 notification.classification = constants.NOTIFICATION_CLASSIFICATION_ASSIGN 

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

63 application_title=application.bibjson().title, 

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

65 volunteers_url=app.config.get('BASE_URL', "https://doaj.org") + url_for("doaj.volunteers"), 

66 ) 

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

68 # note that there is no action url 

69 

70 svc.notify(notification)