Commit 97f67db3 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[reports][management] Pre-commit cleanup

parent ea81206d
......@@ -27,49 +27,66 @@
###############################################################################
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError
from django.core.mail import send_mail
from django.template.loader import render_to_string
from datetime import date
from datetime import datetime
from datetime import time
from datetime import timedelta
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string
from datetime import datetime, time, date, timedelta
from ...models import Report
from .... import __version__
from ...models import Report
import sys
class Command(BaseCommand):
help = 'Send email warning for reports about to expire and cleanup old reports'
help = "Send email warning for reports about to expire and cleanup old reports"
def handle(self, *args, **options):
for expiration_reminder in settings.EXPIRATION_REMINDERS:
expiration_date = date.today() + timedelta(days=expiration_reminder)
reports_about_to_expire = Report.objects.filter(status=Report.LOCKED, expiration_date__range=(datetime.combine(expiration_date, time.min),
datetime.combine(expiration_date, time.max)))
reports_about_to_expire = Report.objects.filter(
status=Report.LOCKED,
expiration_date__range=(
datetime.combine(expiration_date, time.min),
datetime.combine(expiration_date, time.max),
),
)
if reports_about_to_expire:
current_site = Site.objects.get_current()
template_path = 'reports/report_about_to_expire_email.txt'
template_path = "reports/report_about_to_expire_email.txt"
for report in reports_about_to_expire:
subject = "Report %s is about to expire" % \
report.name
subject = "Report %s is about to expire" % report.name
send_mail(subject,
render_to_string(template_path,
{
'report': report,
'beat_version': __version__,
'site': current_site,
}
),
settings.DEFAULT_FROM_EMAIL,
[report.author.email])
self.stdout.write('{} report(s) about to expire'.format(reports_about_to_expire.count()))
send_mail(
subject,
render_to_string(
template_path,
{
"report": report,
"beat_version": __version__,
"site": current_site,
},
),
settings.DEFAULT_FROM_EMAIL,
[report.author.email],
)
self.stdout.write(
"{} report(s) about to expire".format(
reports_about_to_expire.count()
)
)
else:
self.stdout.write('No report(s) about to expire in {} day(s)'.format(expiration_reminder))
self.stdout.write(
"No report(s) about to expire in {} day(s)".format(
expiration_reminder
)
)
call_command('clean_report', '--noinput')
call_command("clean_report", "--noinput")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment