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

34 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.lib import dates 

5from portality.events.consumer import EventConsumer 

6from portality import constants 

7from portality import models 

8from portality.bll import DOAJ, exceptions 

9from portality.lib.seamless import SeamlessException 

10 

11 

12class ApplicationPublisherCreatedNotify(EventConsumer): 

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

14 

15 @classmethod 

16 def consumes(cls, event): 

17 return event.id == constants.EVENT_APPLICATION_CREATED and event.context.get("application") is not None 

18 

19 @classmethod 

20 def consume(cls, event): 

21 context = event.context 

22 app = context.get("application") 

23 if app is None: 

24 return 

25 try: 

26 application = models.Application(**app) 

27 except SeamlessException: 

28 raise exceptions.NoSuchObjectException("Could not create application object") 

29 if application is None: 

30 raise exceptions.NoSuchObjectException("Could not create application object") 

31 if not application.owner: 

32 return 

33 

34 svc = DOAJ.notificationsService() 

35 

36 notification = models.Notification() 

37 notification.who = application.owner 

38 notification.created_by = cls.ID 

39 notification.classification = constants.NOTIFICATION_CLASSIFICATION_CREATE 

40 notification.long = svc.long_notification(cls.ID).format(title=application.bibjson().title, 

41 journal_url=application.bibjson().journal_url, 

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

43 volunteers_url=url_for("doaj.volunteers")) 

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

45 

46 svc.notify(notification)