Private
Public Access
1
0

flexall using form

This commit is contained in:
Sander Roosendaal
2018-10-08 13:08:49 +02:00
parent 3a968b45fe
commit 320bc860c5
4 changed files with 153 additions and 220 deletions

View File

@@ -7,12 +7,38 @@ from django.contrib.auth.models import User
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.extras.widgets import SelectDateWidget
from django.utils import timezone,translation
from django.forms import ModelForm
from django.forms import ModelForm, Select
import dataprep
import types
import datetime
from django.forms import formset_factory
from utils import landingpages
from metrics import axes
class SelectWidget(Select):
"""
Subclass of Django's select widget that allows disabling options.
"""
def __init__(self, *args, **kwargs):
self._disabled_choices = []
super(SelectWidget, self).__init__(*args, **kwargs)
@property
def disabled_choices(self):
return self._disabled_choices
@disabled_choices.setter
def disabled_choices(self, other):
self._disabled_choices = other
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
option_dict = super(SelectWidget, self).create_option(
name, value, label, selected, index, subindex=subindex, attrs=attrs
)
if value in self.disabled_choices:
option_dict['attrs']['disabled'] = 'disabled'
return option_dict
# login form
class LoginForm(forms.Form):
@@ -29,6 +55,7 @@ class EmailForm(forms.Form):
message = forms.CharField()
# Upload the CrewNerd Summary CSV
class CNsummaryForm(forms.Form):
file = forms.FileField(required=True,validators=[must_be_csv])
@@ -919,3 +946,59 @@ class VirtualRaceSelectForm(forms.Form):
self.fields['country'] = forms.ChoiceField(
choices = get_countries(),initial='All'
)
class FlexAxesForm(forms.Form):
axchoices = (
(ax[0],ax[1]) for ax in axes if ax[0] not in ['cumdist','None']
)
yaxchoices = (
(ax[0], ax[1]) for ax in axes if ax[0] not in ['cumdist','distance','time']
)
yaxchoices2 = (
(ax[0], ax[1]) for ax in axes if ax[0] not in ['cumdist','distance','time']
)
xaxis = forms.ChoiceField(
choices=axchoices,label='X-Axis',widget=SelectWidget,required=True)
yaxis1 = forms.ChoiceField(
choices=yaxchoices,label='Left Axis',widget=SelectWidget,required=True)
yaxis2 = forms.ChoiceField(
choices=yaxchoices2,label='Right Axis',widget=SelectWidget,required=True)
def __init__(self,request,*args,**kwargs):
super(FlexAxesForm, self).__init__(*args, **kwargs)
rower = Rower.objects.get(user=request.user)
axchoicespro = (
('',ax[1]) if ax[4] == 'pro' and ax[0] else (ax[0],ax[1]) for ax in axes
)
axchoicesbasicx = []
axchoicesbasicy = []
for ax in axes:
if ax[4] != 'pro' and ax[0] != 'cumdist':
if ax[0] != 'None':
axchoicesbasicx.insert(0,(ax[0],ax[1]))
if ax[0] not in ['cumdist','distance','time']:
axchoicesbasicy.insert(0,(ax[0],ax[1]))
else:
if ax[0] != 'None':
axchoicesbasicx.insert(0,('None',ax[1]+' (PRO)'))
if ax[0] not in ['cumdist','distance','time']:
axchoicesbasicy.insert(0,('None',ax[1]+' (PRO)'))
if rower.rowerplan == 'basic':
self.fields['xaxis'].choices = axchoicesbasicx
self.fields['yaxis1'].choices = axchoicesbasicy
self.fields['yaxis2'].choices = axchoicesbasicy

View File

@@ -2803,6 +2803,8 @@ def interactive_cum_flex_chart2(theworkouts,promember=0,
),
plot])
layout.sizing_mode = 'scale_width'
script, div = components(layout)
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
@@ -3002,8 +3004,6 @@ def interactive_flex_chart2(id=0,promember=0,
sizing_mode = 'fixed' # 'scale_width' also looks nice with this example
plot = Figure(x_axis_type=x_axis_type,y_axis_type=y_axis_type,
tools=TOOLS,
toolbar_sticky=False

View File

@@ -81,147 +81,40 @@
</div>
<ul class="main-content">
<li class="grid_2">
<form enctype="multipart/form-data" action="{{ formloc }}" method="post">
{% csrf_token %}
<table>
{{ optionsform.as_table }}
</table>
<input type="hidden" name="options" value="options">
<input class="button green small" value="Submit" type="Submit">
</form>
</li>
<li class="grid_2">
<p>Use this form to select a different date range:</p>
<p>
Select start and end date for a date range:
<form enctype="multipart/form-data" action="" method="post">
<table>
{{ form.as_table }}
</table>
{% csrf_token %}
<input name='daterange' class="button green" type="submit" value="Submit">
</form>
</li>
<li>
<li class="grid_4">
<p>Summary for {{ theuser.first_name }} {{ theuser.last_name }}
between {{ startdate|date }} and {{ enddate|date }}</p>
</li>
<li>
<ul class="cd-accordion-menu animated">
<li class="has-children" id="xaxis">
<input type="checkbox" name="group-x" id="group-x">
<label for="group-x">
X-Axis
</label>
<ul>
{% for key, value in axchoicesbasic.items %}
{% if key != 'None' %}
<li>
<a href="/rowers/flexall/{{ key }}/{{ yparam1 }}/{{ yparam2 }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% if promember %}
{% for key, value in axchoicespro.items %}
<li>
<a href="/rowers/flexall/{{ key }}/{{ yparam1 }}/{{ yparam2 }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endfor %}
{% else %}
{% for key, value in axchoicespro.items %}
<li>
<a href="/rowers/promembership">{{ value }}</a>
</li>
{% endfor %}
{% endif %}
</ul>
</li>
</ul>
</li>
<li>
<ul class="cd-accordion-menu animated">
<li class="has-children" id="yparam1">
<input type="checkbox" name="group-y1" id="group-y1">
<label for="group-y1">
Left
</label>
<ul>
{% for key, value in axchoicesbasic.items %}
{% if key not in noylist and key != 'None' %}
<li>
<a href="/rowers/flexall/{{ xparam }}/{{ key }}/{{ yparam2 }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% if promember %}
{% for key, value in axchoicespro.items %}
{% if key not in noylist %}
<li>
<a href="/rowers/flexall/{{ xparam }}/{{ key }}/{{ yparam2 }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% else %}
{% for key, value in axchoicespro.items %}
{% if key not in noylist %}
<li>
<a href="/rowers/promembership">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
</li>
</ul>
</li>
<li>
<ul class="cd-accordion-menu animated">
<li class="has-children" id="yparam2">
<input type="checkbox" name="group-y2" id="group-y2">
<label for="group-y2">
Right
</label>
<ul>
{% for key, value in axchoicesbasic.items %}
{% if key not in noylist and key != 'None' %}
<li>
<a href="/rowers/flexall/{{ xparam }}/{{ yparam1 }}/{{ key }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% if promember %}
{% for key, value in axchoicespro.items %}
{% if key not in noylist %}
<li>
<a href="/rowers/flexall/{{ xparam }}/{{ yparam1 }}/{{ key }}/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d"}}/">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% else %}
{% for key, value in axchoicespro.items %}
{% if key not in noylist %}
<li>
<a href="/rowers/promembership">{{ value }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
</li>
</ul>
</li>
<li class="grid_4">
<div id="id_chart">
{{ the_div|safe }}
</div>
</li>
<li>
<form enctype="multipart/form-data" action="{{ formloc }}" method="post">
<table>
{{ optionsform.as_table }}
</table>
<input type="hidden" name="options" value="options">
</li>
<li>
<table>
{{ form.as_table }}
</table>
</li>
<li>
<table>
{{ flexaxesform.as_table }}
</table>
</li>
<li>
{% csrf_token %}
<input class="button green small" value="Submit" type="Submit">
</form>
</li>
</ul>
{% endblock %}

View File

@@ -43,7 +43,7 @@ from rowers.forms import (
LandingPageForm,PlannedSessionSelectForm,WorkoutSessionSelectForm,
PlannedSessionTeamForm,PlannedSessionTeamMemberForm,
VirtualRaceSelectForm,WorkoutRaceSelectForm,CourseSelectForm,
RaceResultFilterForm,PowerIntervalUpdateForm
RaceResultFilterForm,PowerIntervalUpdateForm,FlexAxesForm,
)
from django.core.urlresolvers import reverse, reverse_lazy
@@ -2448,6 +2448,12 @@ def histo_all(request,theuser=0,
'teams':get_my_teams(request.user),
})
def keyvalue_get_default(key,options,def_options):
try:
return options[key]
except KeyError:
return def_options[key]
# The Flex plot for a large selection of workouts
@login_required()
def cum_flex_data(
@@ -2468,37 +2474,21 @@ def cum_flex_data(
def_options = options
if 'options' in request.session:
options = request.session['options']
try:
modality = options['modality']
rankingonly = options['rankingonly']
includereststrokes = options['includereststrokes']
waterboattype = options['waterboattype']
workstrokesonly = not includereststrokes
theuser = options['theuser']
xparam = options['xparam']
yparam1 = options['yparam1']
yparam2 = options['yparam2']
startdatestring = options['startdatestring']
enddatestring = options['enddatestring']
deltadays = options['deltadays']
except KeyError:
modality = def_options['modality']
rankingonly = def_options['rankingonly']
includereststrokes = def_options['includereststrokes']
waterboattype = def_options['waterboattype']
workstrokesonly = not includereststrokes
theuser = def_options['theuser']
xparam = def_options['xparam']
yparam1 = def_options['yparam1']
yparam2 = def_options['yparam2']
startdatestring = def_options['startdatestring']
enddatestring = def_options['enddatestring']
deltadays = def_options['deltadays']
request.session['options'] = def_options
modality = keyvalue_get_default('modality',options,def_options)
rankingonly = keyvalue_get_default('rankingonly',options,def_options)
includereststrokes = keyvalue_get_default('includereststrokes',options,def_options)
waterboattype = keyvalue_get_default('waterboattype',options,def_options)
workstrokesonly = not includereststrokes
theuser = keyvalue_get_default('theuser',options,def_options)
xparam = keyvalue_get_default('xparam',options,def_options)
yparam1 = keyvalue_get_default('yparam1',options,def_options)
yparam2 = keyvalue_get_default('yparam2',options,def_options)
startdatestring = keyvalue_get_default('startdatestring',options,def_options)
enddatestring = keyvalue_get_default('enddatestring',options,def_options)
if modality == 'all':
modalities = [m[0] for m in types.workouttypes]
@@ -2515,9 +2505,6 @@ def cum_flex_data(
except ParseError:
enddate = timezone.now()
if deltadays>0:
startdate = enddate-datetime.timedelta(days=int(deltadays))
if enddate < startdate:
s = enddate
@@ -2648,10 +2635,10 @@ def cum_flex(request,theuser=0,
# get all indoor rows of in date range
# process form
if request.method == 'POST' and "daterange" in request.POST:
if request.method == 'POST':
form = DateRangeForm(request.POST)
deltaform = DeltaDaysForm(request.POST)
optionsform = StatsOptionsForm()
modalityform = TrendFlexModalForm(request.POST)
flexaxesform = FlexAxesForm(request,request.POST)
if form.is_valid():
startdate = form.cleaned_data['startdate']
enddate = form.cleaned_data['enddate']
@@ -2659,27 +2646,6 @@ def cum_flex(request,theuser=0,
s = enddate
enddate = startdate
startdate = s
elif request.method == 'POST' and "datedelta" in request.POST:
deltaform = DeltaDaysForm(request.POST)
optionsform = TrendFlexModalForm()
if deltaform.is_valid():
deltadays = deltaform.cleaned_data['deltadays']
if deltadays != 0 and isinstance(deltadays, Number):
enddate = timezone.now()
startdate = enddate-datetime.timedelta(days=deltadays)
if startdate > enddate:
s = enddate
enddate = startdate
startdate = s
form = DateRangeForm(initial={
'startdate': startdate,
'enddate': enddate,
})
else:
form = DateRangeForm()
optionsform = TrendFlexModalForm()
elif request.method == 'POST' and 'options' in request.POST:
modalityform = TrendFlexModalForm(request.POST)
if modalityform.is_valid():
modality = modalityform.cleaned_data['modality']
waterboattype = modalityform.cleaned_data['waterboattype']
@@ -2700,29 +2666,37 @@ def cum_flex(request,theuser=0,
'startdate': startdate,
'enddate': enddate,
})
deltaform = DeltaDaysForm()
else:
form = DateRangeForm(initial={
'startdate': startdate,
'enddate': enddate,
})
deltaform = DeltaDaysForm()
if flexaxesform.is_valid():
xparam = flexaxesform.cleaned_data['xaxis']
yparam1 = flexaxesform.cleaned_data['yaxis1']
yparam2 = flexaxesform.cleaned_data['yaxis2']
else:
form = DateRangeForm(initial={
'startdate': startdate,
'enddate': enddate,
})
deltaform = DeltaDaysForm()
modalityform = TrendFlexModalForm()
includereststrokes = False
workstrokesonly = not includereststrokes
modalityform = TrendFlexModalForm(
initial={
'modality':modality,
'waterboattype':waterboattype,
'rankingonly':rankingonly,
}
)
initial = {
'xaxis':xparam,
'yaxis1':yparam1,
'yaxis2':yparam2
}
flexaxesform = FlexAxesForm(request,initial=initial)
negtypes = []
for b in types.boattypes:
if b[0] not in waterboattype:
negtypes.append(b[0])
includereststrokes = False
workstrokesonly = not includereststrokes
script = ''
@@ -2730,21 +2704,8 @@ def cum_flex(request,theuser=0,
js_resources = ''
css_resources = ''
axchoicesbasic = {ax[0]:ax[1] for ax in axes if ax[4]=='basic'}
axchoicespro = {ax[0]:ax[1] for ax in axes if ax[4]=='pro'}
noylist = ["time","distance"]
axchoicesbasic.pop("cumdist")
modalityform = TrendFlexModalForm(
initial={
'modality':modality,
'waterboattype':waterboattype,
'rankingonly':rankingonly,
}
)
options = {
'xparam': xparam,
@@ -2755,7 +2716,6 @@ def cum_flex(request,theuser=0,
'waterboattype':waterboattype,
'startdatestring':startdatestring,
'enddatestring':enddatestring,
'deltadays':deltadays,
'rankingonly':rankingonly,
'includereststrokes':includereststrokes,
}
@@ -2786,15 +2746,12 @@ def cum_flex(request,theuser=0,
'enddate':enddate,
'form':form,
'optionsform':modalityform,
'deltaform':deltaform,
'xparam':xparam,
'yparam1':yparam1,
'yparam2':yparam2,
'promember':promember,
'teams':get_my_teams(request.user),
'axchoicesbasic':axchoicesbasic,
'axchoicespro':axchoicespro,
'noylist':noylist,
'flexaxesform':flexaxesform,
})