root / cron_notify_expired.py @ d0777394
History | View | Annotate | Download (1.7 kB)
1 |
from django.core.management import setup_environ |
---|---|
2 |
import settings |
3 |
setup_environ(settings) |
4 |
from django.core.mail import send_mail |
5 |
from flowspy.flowspec.models import * |
6 |
from django.template.loader import render_to_string |
7 |
from django.contrib.sites.models import Site |
8 |
from django.core.urlresolvers import reverse |
9 |
|
10 |
import datetime |
11 |
|
12 |
|
13 |
def notify_expired(): |
14 |
routes = Route.objects.all() |
15 |
for route in routes: |
16 |
if route.status not in ['EXPIRED', 'ADMININACTIVE', 'INACTIVE', 'ERROR']: |
17 |
expiration_days = (route.expires - datetime.date.today()).days |
18 |
if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
|
19 |
try:
|
20 |
fqdn = Site.objects.get_current().domain |
21 |
admin_url = "https://%s%s" % \
|
22 |
(fqdn, |
23 |
"/fod/edit/%s"%route.name)
|
24 |
mail_body = render_to_string("rule_expiration.txt",
|
25 |
{"route": route, 'expiration_days':expiration_days, 'url':admin_url}) |
26 |
days_num = ' days'
|
27 |
expiration_days_text = "%s %s" %('in',expiration_days) |
28 |
if expiration_days == 0: |
29 |
days_num = ' today'
|
30 |
expiration_days_text = ''
|
31 |
if expiration_days == 1: |
32 |
days_num = ' day'
|
33 |
send_mail(settings.EMAIL_SUBJECT_PREFIX + "Rule %s expires %s%s" %
|
34 |
(route.name,expiration_days_text, days_num), |
35 |
mail_body, settings.SERVER_EMAIL, |
36 |
[route.applier.email]) |
37 |
except Exception as e: |
38 |
print e
|
39 |
pass
|
40 |
|
41 |
if __name__ == "__main__": |
42 |
notify_expired() |
43 |
|