From 554c8819b96b81afe20b4f936cc99ca89c29c005 Mon Sep 17 00:00:00 2001 From: yashlamba Date: Thu, 19 Dec 2024 12:44:00 +0100 Subject: [PATCH] curation: tasks: send results in email --- site/zenodo_rdm/curation/tasks.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/site/zenodo_rdm/curation/tasks.py b/site/zenodo_rdm/curation/tasks.py index 9b461949..db997b85 100644 --- a/site/zenodo_rdm/curation/tasks.py +++ b/site/zenodo_rdm/curation/tasks.py @@ -10,12 +10,39 @@ from celery import shared_task from flask import current_app +from flask_mail import Message from invenio_access.permissions import system_identity from invenio_rdm_records.proxies import current_rdm_records_service as records_service from invenio_search.engine import dsl from zenodo_rdm.curation.curators import EURecordCurator +RESULT_EMAIL_BODY = """ +EU Record Curation Processed + +Finished at: {finished_at} +Processed since: {since} +Total Records Processed: {processed} +Records Failed: {failed} +Records Approved: {approved} +Record IDs Approved: +{records_moved} +""" + + +def _send_result_email(content): + """Send curation result as email.""" + subject = f"EU Record Curation Processesed {datetime.now().date()}" + body = RESULT_EMAIL_BODY.format(finished_at=datetime.now(timezone.utc), **content) + sender = current_app.config["MAIL_DEFAULT_SENDER"] + admin_email = current_app.config["APP_RDM_ADMIN_EMAIL_RECIPIENT"] + recipients = admin_email + if not isinstance(admin_email, list): + recipients = [admin_email] + mail_ext = current_app.extensions["mail"] + msg = Message(subject, sender=sender, recipients=recipients, body=body) + mail_ext.send(msg) + @shared_task def run_eu_record_curation(since): @@ -82,6 +109,9 @@ def run_eu_record_curation(since): # This catches failures due to other reasons ctx["failed"] += 1 + if not dry_run: + _send_result_email(ctx) + current_app.logger.error( "EU curation processed", extra=ctx,