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

46 statements  

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

1from datetime import datetime 

2 

3from portality.events.consumer import EventConsumer 

4from portality import constants 

5from portality import models 

6from portality.bll import DOAJ, exceptions 

7from portality.core import app 

8 

9 

10class UpdateRequestPublisherRejectedNotify(EventConsumer): 

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

12 

13 @classmethod 

14 def consumes(cls, event): 

15 if event.id != constants.EVENT_APPLICATION_STATUS: 

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("new_status") != constants.APPLICATION_STATUS_REJECTED: 

28 return False 

29 

30 if event.context.get("old_status") == constants.APPLICATION_STATUS_REJECTED: 

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 return 

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_STATUS_CHANGE 

59 datetime_object = datetime.strptime(application.date_applied, '%Y-%m-%dT%H:%M:%SZ') 

60 date_applied = datetime_object.strftime("%d/%b/%Y") 

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

62 title=application.bibjson().title, 

63 date_applied=date_applied, 

64 ) 

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

66 

67 # there is no action url associated with this notification 

68 

69 svc.notify(notification)