def close_expired_jobs(notify): now = datetime.datetime.utcnow() today = now.strftime("%Y-%m-%d") record_json = type_coerce(RecordMetadata.json, JSONB) before_deadline_date = record_json["deadline_date"].astext.cast( DateTime) < today only_jobs_collection = record_json["_collections"].contains(["Jobs"]) only_not_closed = not_(record_json["status"].astext == "closed") only_not_deleted = or_( not_(record_json.has_key("deleted")), # noqa: W601 not_(record_json["deleted"] == cast(True, JSONB)), ) expired_jobs = RecordMetadata.query.filter(only_jobs_collection, only_not_deleted, only_not_closed, before_deadline_date).all() expired_job_records = [ JobsRecord(job.json, model=job) for job in expired_jobs ] for job_record in expired_job_records: job_record["status"] = "closed" job_record.update(dict(job_record)) db.session.commit() if notify: for job_record in expired_job_records: send_job_deadline_reminder(dict(job_record)) LOGGER.info("Closed expired jobs", notify=notify, num_records=len(expired_jobs))
def test_send_email_to_contact_details_without_putting_it_in_cc( mock_send_email, inspire_app): expected_recipient = "*****@*****.**" expected_cc = "*****@*****.**" job = { "contact_details": [ { "email": expected_recipient, "name": "Group, Craig" }, { "email": expected_cc, "name": "Haverstrom, Rich" }, ], "position": "Tester", } send_job_deadline_reminder(job) mock_send_email.assert_called_once() mock_call = mock_send_email.mock_calls[0][2] assert mock_call["recipient"] == expected_recipient assert mock_call["cc"] == [expected_cc] assert mock_call["body"] assert mock_call[ "subject"] == "Expired deadline for your INSPIRE job: Tester"
def test_regression_send_email_doesnt_fail_when_contact_details_has_no_email( mock_send_email, inspire_app): expected_recipient = "*****@*****.**" expected_cc = [] job = { "contact_details": [ { "email": expected_recipient, "name": "Group, Craig" }, { "name": "Haverstrom, Rich" }, ], "position": "Tester", } send_job_deadline_reminder(job) mock_send_email.assert_called_once() mock_call = mock_send_email.mock_calls[0][2] assert mock_call["cc"] == expected_cc assert mock_call["recipient"] == expected_recipient assert mock_call["body"] assert mock_call[ "subject"] == "Expired deadline for your INSPIRE job: Tester"
def notify_expired_jobs(dry_run): jobs_to_notify = [] dates = [ datetime.date.today(), (datetime.date.today() - datetime.timedelta(days=30)), (datetime.date.today() - datetime.timedelta(days=60)), ] for d in dates: expired_jobs = JobsRecord.get_jobs_by_deadline(d) LOGGER.info(f"Found {len(expired_jobs)} expired jobs", deadline=d) jobs_to_notify.extend(expired_jobs) if not jobs_to_notify: LOGGER.info("No expired job to notify, exiting.") return if dry_run: LOGGER.warn(f"Skip sending emails for {len(jobs_to_notify)} expired jobs") return LOGGER.info(f"Sending {len(jobs_to_notify)} emails for expired jobs") for job in jobs_to_notify: send_job_deadline_reminder(job.to_dict())