|
| 1 | +import logging |
| 2 | +import argparse |
| 3 | + |
| 4 | +from django.core.management.base import BaseCommand |
| 5 | + |
| 6 | +from coldfront.core.grant.models import Grant |
| 7 | +from coldfront.core.project.models import Project |
| 8 | +from coldfront.core.publication.models import Publication |
| 9 | +from coldfront.core.user.models import UserProfile |
| 10 | +from coldfront.core.utils.common import import_from_settings |
| 11 | +from coldfront.core.utils.mail import send_email_template |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +EMAIL_SIGNATURE = import_from_settings("EMAIL_SIGNATURE") |
| 17 | +EMAIL_SENDER = import_from_settings("EMAIL_SENDER") |
| 18 | +CENTER_BASE_URL = import_from_settings("CENTER_BASE_URL") |
| 19 | + |
| 20 | + |
| 21 | +class Command(BaseCommand): |
| 22 | + help = "Send emails to PIs with projects that have no grants/publications associated with them" |
| 23 | + |
| 24 | + def add_arguments(self, parser): |
| 25 | + parser.add_argument( |
| 26 | + "--dry-run", |
| 27 | + action=argparse.BooleanOptionalAction, |
| 28 | + default=False, |
| 29 | + help="dry run, log email content and recipients without sending the emails", |
| 30 | + ) |
| 31 | + |
| 32 | + def handle(self, *args, **options): |
| 33 | + dry_run: bool = options["dry_run"] |
| 34 | + for pi_profile in UserProfile.objects.filter(is_pi=True): |
| 35 | + try: |
| 36 | + logger.info(f"processing projects for PI: {pi_profile.user.username}") |
| 37 | + projects_for_pi = Project.objects.filter(pi=pi_profile.user, status__name__in=["Active", "New"]) |
| 38 | + projects_without_grants = {} |
| 39 | + projects_without_publications = {} |
| 40 | + |
| 41 | + for project in projects_for_pi: |
| 42 | + logger.info(f"processing project: {project}") |
| 43 | + if Grant.objects.filter(project=project).count() == 0: |
| 44 | + projects_without_grants[project.title] = f"{CENTER_BASE_URL.strip('/')}/project/{project.pk}/" |
| 45 | + |
| 46 | + if Publication.objects.filter(project=project).count() == 0: |
| 47 | + projects_without_publications[project.title] = ( |
| 48 | + f"{CENTER_BASE_URL.strip('/')}/project/{project.pk}/" |
| 49 | + ) |
| 50 | + |
| 51 | + grant_notification_context = { |
| 52 | + "pi_first_name": pi_profile.user.first_name, |
| 53 | + "project_dict": projects_without_grants, |
| 54 | + "signature": EMAIL_SIGNATURE, |
| 55 | + } |
| 56 | + logger.info(f"grant nudge context: {grant_notification_context}") |
| 57 | + if projects_without_grants and not dry_run: |
| 58 | + logger.info(f"sending grant nudge to {pi_profile.username}") |
| 59 | + send_email_template( |
| 60 | + "Please update grant info for HPC project", |
| 61 | + "email/request_to_add_grants.txt", |
| 62 | + grant_notification_context, |
| 63 | + EMAIL_SENDER, |
| 64 | + pi_profile.user.email, |
| 65 | + ) |
| 66 | + |
| 67 | + publication_notification_context = { |
| 68 | + "pi_first_name": pi_profile.user.first_name, |
| 69 | + "project_dict": projects_without_publications, |
| 70 | + "signature": EMAIL_SIGNATURE, |
| 71 | + } |
| 72 | + logger.info(f"publication nudge context: {publication_notification_context}") |
| 73 | + if projects_without_publications and not dry_run: |
| 74 | + logger.info(f"sending publication nudge to {pi_profile.username}") |
| 75 | + send_email_template( |
| 76 | + "Please update publication info for HPC projects", |
| 77 | + "email/request_to_add_publications.txt", |
| 78 | + publication_notification_context, |
| 79 | + EMAIL_SENDER, |
| 80 | + pi_profile.user.email, |
| 81 | + ) |
| 82 | + except Exception: |
| 83 | + logger.exception("Exception occurred with traceback:", exc_info=True) |
0 commit comments