Private
Public Access
1
0

Merge branch 'feature/autocycles' into develop

This commit is contained in:
Sander Roosendaal
2018-10-27 17:12:07 +02:00
5 changed files with 225 additions and 17 deletions

View File

@@ -934,7 +934,8 @@ def a_week_from_now():
# models related to training planning - draft
# Do we need a separate class TestTarget?
class TrainingTarget(models.Model):
rower = models.ForeignKey(Rower)
rower = models.ForeignKey(Rower,related_name='targetathlete')
manager = models.ForeignKey(Rower,related_name='targetmanager',null=True)
name = models.CharField(max_length=150,blank=True)
date = models.DateField(
default=half_year_from_now)
@@ -982,7 +983,8 @@ class TrainingTargetForm(ModelForm):
class TrainingPlan(models.Model):
rower = models.ForeignKey(Rower)
rower = models.ForeignKey(Rower,related_name='planathlete')
manager = models.ForeignKey(Rower,related_name='planmanager',null=True)
name = models.CharField(max_length=150,blank=True)
target = models.ForeignKey(TrainingTarget,blank=True,null=True)
startdate = models.DateField(default=timezone.now)

View File

@@ -12,14 +12,16 @@
The training plan target is: {{ plan.target.name }} on {{ plan.target.date }}.
{% endif %}
</p>
{% if plan|mayeditplan:request %}
<p><a href="/rowers/editplan/{{ plan.id }}">Edit the plan</a></p>
{% endif %}
<h2>Plan Macro, Meso and Micro Cycles</h2>
<ul class="cd-accordion-menu animated">
<!-- Start Macrocycle For Loop -->
{% for key, macrocycle in cycles.items %}
<li class="has-children" id="macros">
<li class="has-kids" id="macros">
<input type="checkbox" name="macro-selector" id="macro-selector-{{ macrocycle.0.id }}">
<label for="macro-selector-{{ macrocycle.0.id }}">Macro Cycle {{ macrocycle.0.name }} ({{ macrocycle.0.startdate }} - {{ macrocycle.0.enddate }})</label>
<ul>
@@ -39,10 +41,12 @@
</tr>
<tr>
<td colspan="4">
{% if macrocycle.0|mayeditplan:request %}
<a href="/rowers/macrocycle/{{ macrocycle.0.id }}/">edit</a>
/
<a href="/rowers/deletemacrocycle/{{ macrocycle.0.id }}/">delete</a>
/
{% endif %}
<a href='/rowers/sessions/{{ macrocycle.0.startdate|date:"Y-m-d" }}/{{ macrocycle.0.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>
@@ -95,10 +99,16 @@
</tr>
<tr>
<td colspan="4">
{% if macrocycle.0|mayeditplan:request %}
<a href="/rowers/macrocycle/{{ macrocycle.0.id }}/">edit</a>
/
<a href="/rowers/deletemacrocycle/{{ macrocycle.0.id }}/">delete</a>
/
<a href="/rowers/macrocycle/{{ macrocycle.0.id }}/planbymonths/user/{{ rower.user.id }}">
Replan by Months
</a>
/
{% endif %}
<a href='/rowers/sessions/{{ macrocycle.0.startdate|date:"Y-m-d" }}/{{ macrocycle.0.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>
@@ -117,7 +127,7 @@
</div>
{% endif %}
</li>
<li class="has-children" id="mesos">
<li class="has-kids" id="mesos">
<input type="checkbox" name="meso-selector" id="meso-selector-{{ macrocycle.0.id }}">
<label for="meso-selector-{{ macrocycle.0.id }}">Meso Cycles</label>
<ul>
@@ -140,10 +150,12 @@
</tr>
<tr>
<td colspan="4">
{% if mesocycle.0|mayeditplan:request %}
<a href="/rowers/mesocycle/{{ mesocycle.0.id }}/">edit</a>
/
<a href="/rowers/deletemesocycle/{{ mesocycle.0.id }}/">delete</a>
/
{% endif %}
<a href='/rowers/sessions/{{ mesocycle.0.startdate|date:"Y-m-d" }}/{{ mesocycle.0.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>
@@ -197,10 +209,16 @@
</tr>
<tr>
<td colspan="4">
{% if mesocycle.0|mayeditplan:request %}
<a href="/rowers/mesocycle/{{ mesocycle.0.id }}/">edit</a>
/
<a href="/rowers/deletemesocycle/{{ mesocycle.0.id }}/">delete</a>
/
<a href="/rowers/mesocycle/{{ mesocycle.0.id }}/planbyweeks/user/{{ rower.user.id }}">
Replan by Weeks
</a>
/
{% endif %}
<a href='/rowers/sessions/{{ mesocycle.0.startdate|date:"Y-m-d" }}/{{ mesocycle.0.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>
@@ -219,7 +237,7 @@
</div>
{% endif %}
</li>
<li class="has-children" id="micros">
<li class="has-kids" id="micros">
<input type="checkbox" name="micro-selector"
id="micro-selector-{{ macrocycle.0.id }}-{{ mesocycle.0.id }}">
<label
@@ -247,10 +265,12 @@
</tr>
<tr>
<td colspan="4">
{% if microcycle|mayeditplan:request %}
<a href="/rowers/microcycle/{{ microcycle.id }}/">edit</a>
/
<a href="/rowers/deletemicrocycle/{{ microcycle.id }}/">delete</a>
/
{% endif %}
<a href='/rowers/sessions/{{ microcycle.startdate|date:"Y-m-d" }}/{{ microcycle.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>
@@ -306,10 +326,12 @@
</tr>
<tr>
<td colspan="4">
{% if microcycle|mayeditplan:request %}
<a href="/rowers/microcycle/{{ microcycle.id }}">edit</a>
/
<a href="/rowers/deletemicrocycle/{{ microcycle.id }}">delete</a>
/
{% endif %}
<a href='/rowers/sessions/{{ microcycle.startdate|date:"Y-m-d" }}/{{ microcycle.enddate|date:"Y-m-d" }}/user/{{ rower.user.id }}'>sessions</a>
</td>
</tr>

View File

@@ -10,7 +10,8 @@ register = template.Library()
from rowers.utils import calculate_age
from rowers.models import (
course_length,WorkoutComment,
TrainingMacroCycle,TrainingMesoCycle, TrainingMicroCycle
TrainingMacroCycle,TrainingMesoCycle, TrainingMicroCycle,
Rower
)
from rowers.plannedsessions import (
race_can_register, race_can_submit,race_rower_status
@@ -19,7 +20,7 @@ from rowers.plannedsessions import (
from rowers import c2stuff, runkeeperstuff
from rowers.c2stuff import c2_open
from rowers.runkeeperstuff import runkeeper_open
from rowers.models import checkaccessuser
from rowers.mytypes import otwtypes
from rowers.utils import NoTokenError
@@ -182,6 +183,29 @@ def may_edit(workout,request):
return mayedit
@register.filter
def mayeditplan(obj,request):
if obj is None:
return False
if hasattr(obj,'plan'):
return mayeditplan(obj.plan,request)
if hasattr(obj,'manager'):
if obj.manager is not None:
return request.user == obj.manager.user
rr = Rower.objects.get(user=request.user)
if checkaccessuser(request.user,obj.rower) and rr.rowerplan not in ['basic','pro']:
mayedit = True
return mayedit
@register.filter(name='times')
def times(number):
return range(number)

View File

@@ -441,6 +441,10 @@ urlpatterns = [
name='macrocycle_update_view'),
url(r'^mesocycle/(?P<pk>\d+)/$',views.TrainingMesoCycleUpdate.as_view(),
name='mesocycle_update_view'),
url(r'^macrocycle/(?P<id>\d+)/planbymonths/$',views.planmacrocyclebymonth),
url(r'^macrocycle/(?P<id>\d+)/planbymonths/user/(?P<userid>\d+)/$',views.planmacrocyclebymonth),
url(r'^mesocycle/(?P<id>\d+)/planbyweeks/$',views.planmesocyclebyweek),
url(r'^mesocycle/(?P<id>\d+)/planbyweeks/user/(?P<userid>\d+)/$',views.planmesocyclebyweek),
url(r'^microcycle/(?P<pk>\d+)/$',views.TrainingMicroCycleUpdate.as_view(),
name='microcycle_update_view'),
url(r'^deletetarget/(?P<id>\d+)/$',views.rower_delete_trainingtarget),

View File

@@ -9017,7 +9017,6 @@ def workout_flexchart3_view(request,*args,**kwargs):
else:
print flexaxesform.errors
print xparam,yparam1,yparam2
if not promember:
for name,d in rowingmetrics:
@@ -16089,6 +16088,7 @@ def rower_create_trainingplan(request,userid=0):
therower = getrequestrower(request,userid=userid)
theuser = therower.user
themanager = getrower(request.user)
if request.method == 'POST' and 'date' in request.POST:
targetform = TrainingTargetForm(request.POST)
@@ -16100,6 +16100,7 @@ def rower_create_trainingplan(request,userid=0):
t = TrainingTarget(rower=therower,
name=name,
date=date,
manager=themanager,
notes=notes)
t.save()
@@ -16116,6 +16117,7 @@ def rower_create_trainingplan(request,userid=0):
name=name,
rower=therower,
target=target,
manager=themanager,
startdate=startdate,
enddate=enddate,
)
@@ -16164,7 +16166,7 @@ def rower_delete_trainingtarget(request,id=0):
except TrainingPlan.DoesNotExist:
raise Http404("Training Plan Does Not Exist")
if checkaccessuser(request.user,target.rower):
if checkaccessuser(request.user,target.manager):
target.delete()
messages.info(request,"We have deleted the training target")
else:
@@ -16184,7 +16186,7 @@ def rower_delete_trainingplan(request,id=0):
except TrainingPlan.DoesNotExist:
raise Http404("Training Plan Does Not Exist")
if checkaccessuser(request.user,plan.rower):
if checkaccessuser(request.user,plan.manager):
plan.delete()
messages.info(request,"We have deleted the training plan")
else:
@@ -16201,7 +16203,7 @@ class TrainingPlanDelete(DeleteView):
def get_object(self, *args, **kwargs):
obj = super(TrainingPlanDelete, self).get_object(*args, **kwargs)
if not checkaccessuser(self.request.user,obj.rower):
if not checkaccessuser(self.request.user,obj.manager):
raise PermissionDenied('You are not allowed to delete this training plan')
return obj
@@ -16267,7 +16269,7 @@ class MicroCycleDelete(DeleteView):
def get_object(self, *args, **kwargs):
obj = super(MicroCycleDelete, self).get_object(*args, **kwargs)
if not checkaccessuser(self.request.user,obj.plan.plan.plan.rower):
if not checkaccessuser(self.request.user,obj.plan.plan.plan.manager):
raise PermissionDenied('You are not allowed to delete this training plan cycle')
return obj
@@ -16328,7 +16330,8 @@ class MesoCycleDelete(DeleteView):
def get_object(self, *args, **kwargs):
obj = super(MesoCycleDelete, self).get_object(*args, **kwargs)
if not checkaccessuser(self.request.user,obj.plan.plan.rower):
if not checkaccessuser(self.request.user,obj.plan.plan.manager):
raise PermissionDenied('You are not allowed to delete this training plan cycle')
return obj
@@ -16481,15 +16484,12 @@ class MacroCycleDelete(DeleteView):
def get_object(self, *args, **kwargs):
obj = super(MacroCycleDelete, self).get_object(*args, **kwargs)
if not checkaccessuser(self.request.user,obj.plan.rower):
if not checkaccessuser(self.request.user,obj.plan.manager):
raise PermissionDenied('You are not allowed to delete this training plan cycle')
return obj
@user_passes_test(hasplannedsessions,login_url="/rowers/promembership",
message="This functionality requires a Coach or Self-Coach plan",
redirect_field_name=None)
def rower_trainingplan_view(request,
id=0,
userid=0,
@@ -16725,6 +16725,9 @@ class TrainingMacroCycleUpdate(UpdateView):
def get_object(self, *args, **kwargs):
obj = super(TrainingMacroCycleUpdate, self).get_object(*args, **kwargs)
if obj.plan.manager is not None and self.request.user.rower != obj.plan.manager:
raise PermissionDenied('You are not allowed to edit this training plan cycle')
if not checkaccessuser(self.request.user,obj.plan.rower):
raise PermissionDenied('You are not allowed to edit this training plan cycle')
else:
@@ -16797,6 +16800,9 @@ class TrainingMesoCycleUpdate(UpdateView):
def get_object(self, *args, **kwargs):
obj = super(TrainingMesoCycleUpdate, self).get_object(*args, **kwargs)
r = obj.plan.plan.rower
if obj.plan.plan.manager is not None and self.request.user.rower != obj.plan.plan.manager:
raise PermissionDenied('You are not allowed to edit this training plan cycle')
if not checkaccessuser(self.request.user,r):
raise PermissionDenied('You are not allowed to edit this training plan cycle')
else:
@@ -16875,6 +16881,9 @@ class TrainingMicroCycleUpdate(UpdateView):
def get_object(self, *args, **kwargs):
obj = super(TrainingMicroCycleUpdate, self).get_object(*args, **kwargs)
r = obj.plan.plan.plan.rower
if obj.plan.plan.plan.manager is not None and self.request.user.rower != obj.plan.plan.plan.manager:
raise PermissionDenied('You are not allowed to edit this training plan cycle')
if not checkaccessuser(self.request.user,r):
raise PermissionDenied('You are not allowed to edit this training plan cycle')
else:
@@ -16889,6 +16898,41 @@ class TrainingPlanUpdate(UpdateView):
template_name = 'trainingplan_edit.html'
form_class = TrainingPlanForm
# extra parameters
def get_context_data(self, **kwargs):
context = super(TrainingPlanUpdate, self).get_context_data(**kwargs)
if 'userid' in kwargs:
userid = kwargs['userid']
else:
userid=0
breadcrumbs = [
{
'url':reverse(plannedsessions_view,
kwargs={'userid':userid}),
'name': 'Plan'
},
{
'url':reverse(rower_trainingplan_view,
kwargs={'userid':userid,
'id':self.object.id}),
'name': self.object.name
},
{
'url':reverse('trainingplan_update_view',
kwargs={'pk':self.object.pk}),
'name': 'Edit'
}
]
context['active'] = 'nav-plan'
context['breadcrumbs'] = breadcrumbs
context['rower'] = getrequestrower(self.request,userid=userid)
return context
def get_success_url(self):
return reverse(rower_create_trainingplan)
@@ -16896,11 +16940,16 @@ class TrainingPlanUpdate(UpdateView):
form.instance.user = self.request.user
form.instance.post_date = datetime.datetime.now()
plan = form.save()
plan.manager = self.request.user.rower
plan.save()
macrocyclecheckdates(plan)
return super(TrainingPlanUpdate, self).form_valid(form)
def get_object(self, *args, **kwargs):
obj = super(TrainingPlanUpdate, self).get_object(*args, **kwargs)
if obj.manager is not None and self.request.user.rower != obj.manager.user:
raise PermissionDenied('You are not allowed to edit this training plan cycle')
if not checkaccessuser(self.request.user,obj.rower):
raise PermissionDenied('You are not allowed to edit this training plan cycle')
return obj
@@ -16921,7 +16970,114 @@ class TrainingTargetUpdate(UpdateView):
def get_object(self, *args, **kwargs):
obj = super(TrainingTargetUpdate, self).get_object(*args, **kwargs)
if obj.manager is not None and self.request.user.rower != obj.manager.user:
raise PermissionDenied('You are not allowed to edit this training plan cycle')
if not checkaccessuser(self.request.user,obj.rower):
raise PermissionDenied('You are not allowed to edit this training plan target')
return obj
def allsundays(startdate,enddate):
d = startdate
d += timedelta(days = 6 - d.weekday()) # first Sunday
while d<enddate:
yield d
d += timedelta(days=7)
@user_passes_test(hasplannedsessions,login_url="/rowers/promembership",
message="This functionality requires a Coach or Self-Coach plan",
redirect_field_name=None)
def planmesocyclebyweek(request,id=0,userid=0):
try:
cycle = TrainingMesoCycle.objects.get(id=id)
except TrainingMesoCycle.DoesNotExist:
raise Http404("Training Cycle does not exist")
if not checkaccessuser(request.user,cycle.plan.plan.manager):
raise PermissionDenied("You are not allowed to do this")
micros = TrainingMicroCycle.objects.filter(plan=cycle)
for m in micros:
m.delete()
cycle.type = 'userdefined'
cycle.save()
#we're still here. We have permission
sundays = [s for s in allsundays(cycle.startdate,cycle.enddate)]
for i in range(len(sundays)-1):
monday = sundays[i]+timedelta(days=1)
nextsunday = sundays[i+1]
if i == 0 and monday > cycle.startdate:
monday = cycle.startdate
if nextsunday < cycle.enddate and i == len(sundays)-2:
nextsunday = cycle.enddate
micro = TrainingMicroCycle(startdate = monday,
enddate = nextsunday,
plan = cycle,
name = 'Week %s' % sundays[i+1].isocalendar()[1],
type = 'userdefined')
micro.save()
micros = TrainingMicroCycle.objects.filter(plan=cycle)
url = reverse(rower_trainingplan_view,
kwargs = {'userid':str(userid),
'id':str(cycle.plan.plan.id),
'thismicroid':str(micros[0].id)})
return HttpResponseRedirect(url)
def allmonths(startdate,enddate):
d = startdate
while d<enddate:
yield d
d = datetime.date(d.year+(d.month / 12),((d.month % 12) + 1),1)
@user_passes_test(hasplannedsessions,login_url="/rowers/promembership",
message="This functionality requires a Coach or Self-Coach plan",
redirect_field_name=None)
def planmacrocyclebymonth(request,id=0,userid=0):
try:
cycle = TrainingMacroCycle.objects.get(id=id)
except TrainingMacroCycle.DoesNotExist:
raise Http404("Training Cycle does not exist")
if not checkaccessuser(request.user,cycle.plan.manager):
raise PermissionDenied("You are not allowed to do this")
mesos = TrainingMesoCycle.objects.filter(plan=cycle)
for m in mesos:
m.delete()
cycle.type = 'userdefined'
cycle.save()
#we're still here. We have permission
monthstarts = [d for d in allmonths(cycle.startdate,cycle.enddate)]
monthstarts.append(cycle.enddate)
for i in range(len(monthstarts)-1):
firstday = monthstarts[i]
lastday = monthstarts[i+1]-timedelta(days=1)
if lastday < cycle.enddate and i == len(monthstarts)-2:
lastday = cycle.enddate
meso = TrainingMesoCycle(startdate = firstday,
enddate = lastday,
plan = cycle,
name = '%s' % firstday.strftime("%B"),
type = 'userdefined')
meso.save()
mesos = TrainingMesoCycle.objects.filter(plan=cycle)
url = reverse(rower_trainingplan_view,
kwargs = {'userid':str(userid),
'id':str(cycle.plan.id),
'thismesoid':str(mesos[0].id)})
return HttpResponseRedirect(url)