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