sending alerts in rudimentary form
This commit is contained in:
@@ -62,16 +62,22 @@ def alert_add_filters(alert,filters):
|
|||||||
f.delete()
|
f.delete()
|
||||||
|
|
||||||
for f in filters:
|
for f in filters:
|
||||||
m = Condition(
|
metric = f['metric']
|
||||||
metric = f['metric'],
|
value1 = f['value1']
|
||||||
value1 = f['value1'],
|
value2 = f['value2']
|
||||||
value2 = f['value2'],
|
condition = f['condition']
|
||||||
condition = f['condition']
|
|
||||||
)
|
|
||||||
|
|
||||||
m.save()
|
if condition and metric and value1:
|
||||||
|
m = Condition(
|
||||||
|
metric = f['metric'],
|
||||||
|
value1 = f['value1'],
|
||||||
|
value2 = f['value2'],
|
||||||
|
condition = f['condition']
|
||||||
|
)
|
||||||
|
|
||||||
alert.filter.add(m)
|
m.save()
|
||||||
|
|
||||||
|
alert.filter.add(m)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|||||||
58
rowers/management/commands/processalerts.py
Normal file
58
rowers/management/commands/processalerts.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#!/srv/venv/bin/python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
PY3K = sys.version_info >= (3,0)
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from rowers.models import Alert, Condition, User
|
||||||
|
from rowers.tasks import handle_send_email_alert
|
||||||
|
|
||||||
|
from rowers import alerts
|
||||||
|
|
||||||
|
import django_rq
|
||||||
|
queue = django_rq.get_queue('default')
|
||||||
|
queuelow = django_rq.get_queue('low')
|
||||||
|
queuehigh = django_rq.get_queue('low')
|
||||||
|
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument(
|
||||||
|
'--testing',
|
||||||
|
action='store_true',
|
||||||
|
dest='testing',
|
||||||
|
default=False,
|
||||||
|
help="Run in testing mode, don't send emails",
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
if 'testing' in options:
|
||||||
|
testing = options['testing']
|
||||||
|
else:
|
||||||
|
testing = False
|
||||||
|
|
||||||
|
todaysalerts = Alert.objects.filter(next_run = datetime.date.today(),emailalert=True)
|
||||||
|
|
||||||
|
for alert in todaysalerts:
|
||||||
|
stats = alerts.alert_get_stats(alert)
|
||||||
|
|
||||||
|
# send email
|
||||||
|
handle_send_email_alert(alert.manager.email,
|
||||||
|
alert.manager.first_name,
|
||||||
|
alert.manager.last_name,
|
||||||
|
stats,debug=True)
|
||||||
|
|
||||||
|
# advance next_run
|
||||||
|
if not testing:
|
||||||
|
alert.next_run = alert.next_run + datetime.timedelta(days=alert.period)
|
||||||
|
alert.save()
|
||||||
|
|
||||||
|
if testing:
|
||||||
|
print('{nr} alerts found'.format(nr = len(todaysalerts)))
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS(
|
||||||
|
'Successfully processed alerts'))
|
||||||
@@ -756,6 +756,35 @@ def handle_updatedps(useremail, workoutids, debug=False,**kwargs):
|
|||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@app.task
|
||||||
|
def handle_send_email_alert(
|
||||||
|
useremail, userfirstname, userlastname, stats, **kwargs):
|
||||||
|
|
||||||
|
if 'debug' in kwargs:
|
||||||
|
debug = kwargs['debug']
|
||||||
|
else:
|
||||||
|
debug = False
|
||||||
|
|
||||||
|
subject = "Your rowing performance on rowsandall.com ({startdate} to {enddate})".format(
|
||||||
|
startdate = stats['startdate'],
|
||||||
|
enddate = stats['enddate'],
|
||||||
|
)
|
||||||
|
|
||||||
|
from_email = 'Rowsandall <info@rowsandall.com>'
|
||||||
|
|
||||||
|
d = {
|
||||||
|
'report':stats,
|
||||||
|
'first_name':userfirstname,
|
||||||
|
'last_name':userlastname,
|
||||||
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
|
|
||||||
|
res = send_template_email(from_email,[useremail],subject,
|
||||||
|
'alertemail.html',
|
||||||
|
d,**kwargs)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
def handle_send_email_transaction(
|
def handle_send_email_transaction(
|
||||||
username, useremail, amount, **kwargs):
|
username, useremail, amount, **kwargs):
|
||||||
|
|||||||
20
rowers/templates/alertemail.html
Normal file
20
rowers/templates/alertemail.html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{% extends "emailbase.html" %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<p>Dear <strong>{{ first_name }}</strong>,</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Here is the report for your alert on <a href="{{ siteurl }}">rowsandall.com</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{% for key, value in report.items() %}
|
||||||
|
<p>
|
||||||
|
{{ key }}: {{ value }}
|
||||||
|
</p>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Best Regards, the Rowsandall Team
|
||||||
|
</p>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
Reference in New Issue
Block a user