alert create including filters
This commit is contained in:
@@ -12,7 +12,7 @@ def create_alert(manager, rower, measured,period=7, emailalert=True,
|
|||||||
# check if manager is coach of rower. If not return 0
|
# check if manager is coach of rower. If not return 0
|
||||||
if manager.rower != rower:
|
if manager.rower != rower:
|
||||||
if rower not in coach_getcoachees(manager.rower):
|
if rower not in coach_getcoachees(manager.rower):
|
||||||
return 0
|
return 0,'You are not allowed to create this alert'
|
||||||
|
|
||||||
m = Condition(
|
m = Condition(
|
||||||
metric = measured['metric'],
|
metric = measured['metric'],
|
||||||
@@ -36,7 +36,8 @@ def create_alert(manager, rower, measured,period=7, emailalert=True,
|
|||||||
alert.save()
|
alert.save()
|
||||||
|
|
||||||
if 'filter' in kwargs:
|
if 'filter' in kwargs:
|
||||||
for f in filter:
|
filters = kwargs['filter']
|
||||||
|
for f in filters:
|
||||||
m = Condition(
|
m = Condition(
|
||||||
metric = f['metric'],
|
metric = f['metric'],
|
||||||
value1 = f['value1'],
|
value1 = f['value1'],
|
||||||
@@ -49,7 +50,7 @@ def create_alert(manager, rower, measured,period=7, emailalert=True,
|
|||||||
alert.filter.add(m)
|
alert.filter.add(m)
|
||||||
|
|
||||||
|
|
||||||
return m.id
|
return alert.id,'Your alert was created'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ def alert_add_filters(alert,filter):
|
|||||||
|
|
||||||
alert.filter.add(m)
|
alert.filter.add(m)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
# get alert stats
|
# get alert stats
|
||||||
# nperiod = 0: current period, i.e. next_run - n days to today
|
# nperiod = 0: current period, i.e. next_run - n days to today
|
||||||
|
|||||||
@@ -1030,6 +1030,19 @@ class ConditionEditForm(ModelForm):
|
|||||||
model = Condition
|
model = Condition
|
||||||
fields = ['metric','condition','value1','value2']
|
fields = ['metric','condition','value1','value2']
|
||||||
|
|
||||||
|
class BaseConditionFormSet(BaseFormSet):
|
||||||
|
def clean(self):
|
||||||
|
if any(self.errors):
|
||||||
|
return
|
||||||
|
|
||||||
|
for form in self.forms:
|
||||||
|
if form.cleaned_data:
|
||||||
|
metric = form.cleaned_data['metric']
|
||||||
|
condition = form.cleaned_data['condition']
|
||||||
|
value1 = form.cleaned_data['value1']
|
||||||
|
value2 = form.cleaned_data['value2']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rowchoices = []
|
rowchoices = []
|
||||||
for key,value in mytypes.workouttypes:
|
for key,value in mytypes.workouttypes:
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
E408191@CZ27LT9RCGN72.13140:1565793987
|
|
||||||
@@ -6,22 +6,63 @@
|
|||||||
{% block main %}
|
{% block main %}
|
||||||
<h1>Alert Edit</h1>
|
<h1>Alert Edit</h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
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.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
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).
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
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.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<form action="" method="post">
|
||||||
<ul class="main-content">
|
<ul class="main-content">
|
||||||
<li class="grid_2">
|
<li class="grid_2">
|
||||||
|
<h2>Alert</h2>
|
||||||
<p>
|
<p>
|
||||||
<form action="" method="post">
|
{{ formset.management_form }}
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<table>
|
<table>
|
||||||
{{ form.as_table }}
|
{{ form.as_table }}
|
||||||
{{ measuredform.as_table }}
|
{{ measuredform.as_table }}
|
||||||
</table>
|
</table>
|
||||||
<input type="submit" value="Save">
|
<input type="submit" value="Save">
|
||||||
</form>
|
|
||||||
</p>
|
</p>
|
||||||
</li>
|
</li>
|
||||||
|
{% for filter_form in formset %}
|
||||||
|
<li class="grid_2">
|
||||||
|
<div class="fav-formset">
|
||||||
|
<h2>Filter {{ forloop.counter }}</h2>
|
||||||
|
<table width=100%>
|
||||||
|
{{ filter_form.as_table }}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<!-- Include formset plugin - including jQuery dependency -->
|
||||||
|
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
|
||||||
|
<script src="/static/js/jquery.formset.js"></script>
|
||||||
|
<script>
|
||||||
|
$('.fav-formset').formset({
|
||||||
|
addText: '<div> </div><div>add filter</div>',
|
||||||
|
deleteText: '<div><p> </p></div><div>remove</div>'
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
|
|
||||||
<p>Rower: {{ rower.user.first_name }}</p>
|
<p>Rower: {{ rower.user.first_name }}</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<a href="/rowers/alerts/">Try out Alerts</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|||||||
@@ -4348,11 +4348,14 @@ def alerts_view(request,userid=0):
|
|||||||
redirect_field_name=None)
|
redirect_field_name=None)
|
||||||
def alert_create_view(request,userid=0):
|
def alert_create_view(request,userid=0):
|
||||||
r = getrequestrower(request,userid=userid)
|
r = getrequestrower(request,userid=userid)
|
||||||
|
FilterFormSet = formset_factory(ConditionEditForm, formset=BaseConditionFormSet,extra=1)
|
||||||
|
filter_formset = FilterFormSet()
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = AlertEditForm(request.POST)
|
form = AlertEditForm(request.POST)
|
||||||
measuredform = ConditionEditForm(request.POST)
|
measuredform = ConditionEditForm(request.POST)
|
||||||
if form.is_valid() and measuredform.is_valid():
|
filter_formset = FilterFormSet(request.POST)
|
||||||
|
if form.is_valid() and measuredform.is_valid() and filter_formset.is_valid():
|
||||||
ad = form.cleaned_data
|
ad = form.cleaned_data
|
||||||
measured = measuredform.cleaned_data
|
measured = measuredform.cleaned_data
|
||||||
|
|
||||||
@@ -4362,11 +4365,31 @@ def alert_create_view(request,userid=0):
|
|||||||
workouttype = ad['workouttype']
|
workouttype = ad['workouttype']
|
||||||
name = ad['name']
|
name = ad['name']
|
||||||
|
|
||||||
result = create_alert(request.user,r,measured,period=period,emailalert=emailalert,
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
result,message = create_alert(request.user,r,measured,period=period,emailalert=emailalert,
|
||||||
reststrokes=reststrokes,workouttype=workouttype,
|
reststrokes=reststrokes,workouttype=workouttype,
|
||||||
|
filter = filters,
|
||||||
name=name)
|
name=name)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
|
messages.info(request,message)
|
||||||
|
|
||||||
url = reverse('alert_edit_view',kwargs={'id':result})
|
url = reverse('alert_edit_view',kwargs={'id':result})
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
else:
|
else:
|
||||||
@@ -4391,6 +4414,7 @@ def alert_create_view(request,userid=0):
|
|||||||
return render(request,'alert_create.html',
|
return render(request,'alert_create.html',
|
||||||
{
|
{
|
||||||
'breadcrumbs':breadcrumbs,
|
'breadcrumbs':breadcrumbs,
|
||||||
|
'formset': filter_formset,
|
||||||
'rower':r,
|
'rower':r,
|
||||||
'form':form,
|
'form':form,
|
||||||
'measuredform':measuredform,
|
'measuredform':measuredform,
|
||||||
@@ -4411,6 +4435,8 @@ def alert_edit_view(request,id=0,userid=0):
|
|||||||
form = AlertEditForm(instance=alert)
|
form = AlertEditForm(instance=alert)
|
||||||
measuredform = ConditionEditForm(instance=alert.measured)
|
measuredform = ConditionEditForm(instance=alert.measured)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
breadcrumbs = [
|
breadcrumbs = [
|
||||||
{
|
{
|
||||||
'url':'/rowers/analysis',
|
'url':'/rowers/analysis',
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ from rowers.models import (
|
|||||||
)
|
)
|
||||||
from rowers.models import (
|
from rowers.models import (
|
||||||
FavoriteForm,BaseFavoriteFormSet,SiteAnnouncement,BasePlannedSessionFormSet,
|
FavoriteForm,BaseFavoriteFormSet,SiteAnnouncement,BasePlannedSessionFormSet,
|
||||||
get_course_timezone
|
get_course_timezone,BaseConditionFormSet,
|
||||||
)
|
)
|
||||||
from rowers.metrics import rowingmetrics,defaultfavoritecharts,nometrics
|
from rowers.metrics import rowingmetrics,defaultfavoritecharts,nometrics
|
||||||
from rowers import metrics as metrics
|
from rowers import metrics as metrics
|
||||||
|
|||||||
Reference in New Issue
Block a user