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

45 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-11-09 15:10 +0000

1from portality.events.consumer import EventConsumer 

2from portality import constants 

3from portality import models 

4from portality.bll import DOAJ 

5from portality.bll import exceptions 

6from portality.lib import dates 

7from portality.core import app 

8 

9 

10class UpdateRequestPublisherAssignedNotify(EventConsumer): 

11 ID = "update_request:publisher:assigned:notify" 

12 

13 @classmethod 

14 def consumes(cls, event): 

15 if event.id != constants.EVENT_APPLICATION_ASSED_ASSIGNED: 

16 return False 

17 

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

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

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

21 return False 

22 

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

24 if app_source is None: 

25 return False 

26 

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

28 return False 

29 

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

31 return False 

32 

33 try: 

34 application = models.Application(**app_source) 

35 except Exception as e: 

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

37 

38 is_update_request = application.application_type == constants.APPLICATION_TYPE_UPDATE_REQUEST 

39 return is_update_request 

40 

41 @classmethod 

42 def consume(cls, event): 

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

44 

45 try: 

46 application = models.Application(**app_source) 

47 except Exception as e: 

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

49 

50 if not application.owner: 

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

52 

53 svc = DOAJ.notificationsService() 

54 

55 notification = models.Notification() 

56 notification.who = application.owner 

57 notification.created_by = cls.ID 

58 notification.classification = constants.NOTIFICATION_CLASSIFICATION_ASSIGN 

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

60 application_title=application.bibjson().title, 

61 application_date=dates.human_date(application.date_applied) 

62 ) 

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

64 # note that there is no action url 

65 

66 svc.notify(notification)