Skip to content

Notify report due

hypha.apply.projects.management.commands.notify_report_due

Command

Bases: BaseCommand

help class-attribute instance-attribute

help = 'Notify users that they have a report due soon'

add_arguments

add_arguments(parser)
Source code in hypha/apply/projects/management/commands/notify_report_due.py
def add_arguments(self, parser):
    parser.add_argument("days_before", type=int)

handle

handle(*args, **options)
Source code in hypha/apply/projects/management/commands/notify_report_due.py
def handle(self, *args, **options):
    site = ApplyHomePage.objects.first().get_site()

    # Mock a HTTPRequest in order to pass the site settings into the
    # templates
    request = HttpRequest()
    request.META["SERVER_NAME"] = site.hostname
    request.META["SERVER_PORT"] = site.port
    request.META[settings.SECURE_PROXY_SSL_HEADER] = "https"
    request.session = {}
    request._messages = FallbackStorage(request)

    today = timezone.now().date()
    due_date = today + relativedelta(days=options["days_before"])
    for project in Project.objects.in_progress():
        next_report = project.report_config.current_due_report()
        due_soon = next_report.end_date == due_date
        not_notified_today = (
            not next_report.notified or next_report.notified.date() != today
        )
        if due_soon and not_notified_today:
            messenger(
                MESSAGES.REPORT_NOTIFY,
                request=request,
                user=None,
                source=project,
                related=next_report,
            )
            # Notify about the due report
            next_report.notified = timezone.now()
            next_report.save()
            self.stdout.write(self.style.SUCCESS(f"Notified project: {project.id}"))