Coverage for portality/tasks/approaching_flag_deadline.py: 75%

72 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1import time 

2 

3from portality.core import app 

4from portality.bll import DOAJ 

5from portality.lib import dates 

6from portality import models 

7from portality.util import url_for 

8 

9from portality.tasks.redis_huey import scheduled_short_queue as queue 

10 

11from portality.background import BackgroundTask, BackgroundApi 

12from portality.tasks.helpers import background_helper 

13from portality.ui.messages import Messages 

14from portality import constants 

15 

16 

17class ApproachingDeadlineQuery: 

18 def __init__(self): 

19 self._delta = app.config.get('FLAG_APPROACHING_DEADLINE_DELTA', 7) 

20 self._ddate = dates.days_after_now(days=self._delta) 

21 

22 def query(self): 

23 return { 

24 "query": { 

25 "bool": { 

26 "filter": { 

27 "bool": { 

28 "must": [ 

29 {"term": {"index.most_urgent_flag_deadline": dates.format(self._ddate, format="%Y-%m-%d")}}, 

30 {"term": {"admin.in_doaj": True}} 

31 ] 

32 } 

33 } 

34 } 

35 } 

36 } 

37 

38 

39# ~~FindFlagsWithApproachingDeadlineTask:Task~~ 

40class ApproachingFlagDeadlineTask(BackgroundTask): 

41 __action__ = "approaching_flag_deadline" 

42 

43 def __init__(self, job): 

44 super(ApproachingFlagDeadlineTask, self).__init__(job) 

45 self._delta = app.config.get('FLAG_APPROACHING_DEADLINE_DELTA', 7) 

46 self._ddate = dates.days_after_now(days=self._delta) 

47 

48 def find_journals_with_approaching_deadlines(self): 

49 jdata = [] 

50 

51 for journal in models.Journal.iterate(q=ApproachingDeadlineQuery().query(), keepalive='5m', wrap=True): 

52 # ~~->Journal:Model~~ 

53 jdata.append(journal.id) 

54 self.background_job.add_audit_message(Messages.JOURNALS_WITH_APPROACHING_DEADLINES_FOUND.format(delta=self._delta, id=journal.id)) 

55 

56 return jdata 

57 

58 def run(self): 

59 journals = self.find_journals_with_approaching_deadlines() 

60 if len(journals): 

61 for j in journals: 

62 journal = models.Journal.pull(j) 

63 assignee = journal.flags[0]["flag"]["assigned_to"] 

64 

65 svc = DOAJ.notificationsService() 

66 

67 acc = models.Account.pull(assignee) 

68 if acc is None: 

69 self.background_job.add_audit_message(Messages.EXCEPTION_NOTIFICATION_NO_ACCOUNT.format(x=assignee)) 

70 continue 

71 

72 notification = models.Notification() 

73 notification.classification = constants.NOTIFICATION_CLASSIFICATION_STATUS 

74 notification.who = acc.id 

75 

76 source_id = "bg:job:" + self.__action__ + ":notify" 

77 

78 notification.created_by = source_id 

79 notification.long = svc.long_notification(source_id).format( 

80 journal_title=journal.bibjson().title, 

81 id=journal.id) 

82 

83 notification.short = svc.short_notification(source_id) 

84 notification.action = url_for("admin.journal_page", journal_id=journal.id) 

85 

86 svc.notify(notification) 

87 

88 self.background_job.add_audit_message(Messages.JOURNALS_WITH_APPROACHING_DEADLINES_FOUND_NOTIFICATION_SENT_LOG) 

89 else: 

90 self.background_job.add_audit_message(Messages.NO_JOURNALS_WITH_APPROACHING_DEADLINES_FOUND_LOG) 

91 

92 def cleanup(self): 

93 """ 

94 Cleanup after a successful OR failed run of the task 

95 :return: 

96 """ 

97 pass 

98 

99 @classmethod 

100 def prepare(cls, username, **kwargs): 

101 """ 

102 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob, 

103 or fail with a suitable exception 

104 

105 :param username: User account for this task to complete as 

106 :param kwargs: arbitrary keyword arguments pertaining to this task type 

107 :return: a BackgroundJob instance representing this task 

108 """ 

109 

110 # first prepare a job record 

111 job = background_helper.create_job(username, cls.__action__, 

112 queue_id=huey_helper.queue_id, ) 

113 return job 

114 

115 @classmethod 

116 def submit(cls, background_job): 

117 """ 

118 Submit the specified BackgroundJob to the background queue 

119 

120 :param background_job: the BackgroundJob instance 

121 :return: 

122 """ 

123 background_job.save() 

124 approaching_flag_deadline.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10)) 

125 

126 

127huey_helper = ApproachingFlagDeadlineTask.create_huey_helper(queue) 

128 

129 

130@huey_helper.register_schedule 

131def scheduled_approaching_flag_deadline(): 

132 user = app.config.get("SYSTEM_USERNAME") 

133 job = ApproachingFlagDeadlineTask.prepare(user) 

134 ApproachingFlagDeadlineTask.submit(job) 

135 

136 

137@huey_helper.register_execute(is_load_config=False) 

138def approaching_flag_deadline(job_id): 

139 job = models.BackgroundJob.pull(job_id) 

140 task = ApproachingFlagDeadlineTask(job) 

141 BackgroundApi.execute(task)