From 729ed0d4f84d3287d16a9b3afe31f0b3d0720d43 Mon Sep 17 00:00:00 2001
From: Sander Roosendaal
Date: Sat, 17 Aug 2019 16:19:09 +0200
Subject: [PATCH] alert edit including filters
---
rowers/alerts.py | 4 +-
rowers/templates/alert_create.html | 4 +-
rowers/templates/alert_edit.html | 61 ++++++++++++++++++++++-----
rowers/views/analysisviews.py | 66 ++++++++++++++++++++++++++++--
4 files changed, 117 insertions(+), 18 deletions(-)
diff --git a/rowers/alerts.py b/rowers/alerts.py
index 688600fc..5ac22e57 100644
--- a/rowers/alerts.py
+++ b/rowers/alerts.py
@@ -55,12 +55,12 @@ def create_alert(manager, rower, measured,period=7, emailalert=True,
# update alert
-def alert_add_filters(alert,filter):
+def alert_add_filters(alert,filters):
for f in alert.filter.all():
alert.filter.remove(f)
f.delete()
- for f in filter:
+ for f in filters:
m = Condition(
metric = f['metric'],
value1 = f['value1'],
diff --git a/rowers/templates/alert_create.html b/rowers/templates/alert_create.html
index 24099c36..138e66c0 100644
--- a/rowers/templates/alert_create.html
+++ b/rowers/templates/alert_create.html
@@ -1,10 +1,10 @@
{% extends "newbase.html" %}
{% load staticfiles %}
-{% block title %}Planned Session{% endblock %}
+{% block title %}Metric Alert{% endblock %}
{% block main %}
-Alert Edit
+Alert Create
Alerts are useful to give you a regular update on how you are doing. For example, if you are
diff --git a/rowers/templates/alert_edit.html b/rowers/templates/alert_edit.html
index 267abf40..7c15abda 100644
--- a/rowers/templates/alert_edit.html
+++ b/rowers/templates/alert_edit.html
@@ -1,27 +1,66 @@
{% extends "newbase.html" %}
{% load staticfiles %}
-{% block title %}Planned Session{% endblock %}
+{% block title %}Metric Alert{% endblock %}
{% block main %}
-
Alert Edit
+
+ Alerts are useful to give you a regular update on how you are doing. For example, if you are
+ worried about rowing too short, you can set an alert on drive length, and the site will automatically
+ tell you how well you are doing.
+
+
+
+ To set an alert on a minimum drive length, you would select "Drive Length (degree)" as the metric in the
+ form below, then set the condition to ">" (greater than), and value 1 to the minimum drive length
+ that you find acceptable. The value 2 is only relevant for alerts where you want to have a metric
+ between two values. Set the workout type to "Standard Racing Shell", or whatever boat class you
+ want this metric to run for, select the period over which you want to monitor and get regular
+ reports (7 days).
+
+
+
+ Optionally, you can add filters. With filters, the alert considers only those strokes that
+ fulfill all filters. For example, you could set a filter on power between 200 and 300 Watt,
+ to only look at drive length in that power zone.
+
-
-
+
+
+ {% for filter_form in formset %}
+
+
+
+ {% endfor %}
+
+
-
-
+
+
+
+
{% endblock %}
diff --git a/rowers/views/analysisviews.py b/rowers/views/analysisviews.py
index 5d099933..9d744438 100644
--- a/rowers/views/analysisviews.py
+++ b/rowers/views/analysisviews.py
@@ -4430,10 +4430,69 @@ def alert_edit_view(request,id=0,userid=0):
r = getrequestrower(request,userid=userid)
alert = Alert.objects.get(id=id)
+
+ FilterFormSet = formset_factory(ConditionEditForm, formset=BaseConditionFormSet,extra=0)
+ if len(alert.filter.all()) == 0:
+ FilterFormSet = formset_factory(ConditionEditForm, formset=BaseConditionFormSet, extra=1)
+
+ filter_data = [{'metric':m.metric,
+ 'value1':m.value1,
+ 'value2':m.value2,
+ 'condition':m.condition}
+ for m in alert.filter.all()]
-
- form = AlertEditForm(instance=alert)
- measuredform = ConditionEditForm(instance=alert.measured)
+ if request.method == 'POST':
+ form = AlertEditForm(request.POST)
+ measuredform = ConditionEditForm(request.POST)
+ filter_formset = FilterFormSet(request.POST)
+ if form.is_valid() and measuredform.is_valid() and filter_formset.is_valid():
+ ad = form.cleaned_data
+ measured = measuredform.cleaned_data
+
+ period = ad['period']
+ emailalert = ad['emailalert']
+ reststrokes = ad['reststrokes']
+ workouttype = ad['workouttype']
+ name = ad['name']
+
+ m = alert.measured
+ m.metric = measured['metric']
+ m.value1 = measured['value1']
+ m.value2 = measured['value2']
+ m.condition = measured['condition']
+ m.save()
+
+ alert.period = period
+ alert.emailalert = emailalert
+ alert.reststrokes = reststrokes
+ alert.workouttype = workouttype
+ alert.name = name
+ alert.save()
+
+ filters = []
+
+ for filter_form in filter_formset:
+ metric = filter_form.cleaned_data.get('metric')
+ condition = filter_form.cleaned_data.get('condition')
+ value1 = filter_form.cleaned_data.get('value1')
+ value2 = filter_form.cleaned_data.get('value2')
+
+ filters.append(
+ {
+ 'metric':metric,
+ 'condition':condition,
+ 'value1':value1,
+ 'value2':value2,
+ }
+ )
+
+ res = alert_add_filters(alert, filters)
+ messages.info(request,'Alert was changed')
+
+ else:
+ form = AlertEditForm(instance=alert)
+ measuredform = ConditionEditForm(instance=alert.measured)
+ filter_formset = FilterFormSet(initial=filter_data)
@@ -4460,6 +4519,7 @@ def alert_edit_view(request,id=0,userid=0):
'rower':r,
'form':form,
'measuredform':measuredform,
+ 'formset':filter_formset,
})
# alert delete view