Implement rounds

This commit is contained in:
Gabriel Augendre 2018-03-03 18:33:55 +01:00
parent b80c7c0319
commit 359f4e6fab
No known key found for this signature in database
GPG key ID: F360212F958357D4
6 changed files with 95 additions and 5 deletions

View file

@ -31,7 +31,7 @@ class Equipment(models.Model):
@property @property
def last_theoretical_max(self): def last_theoretical_max(self):
return self.theoretical_maxs.order_by('-start').first() return self.theoretical_maxs.order_by('-date').first()
def __str__(self): def __str__(self):
return f'{self.name} ({self.room.name})' return f'{self.name} ({self.room.name})'

View file

@ -8,8 +8,14 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<a href="{% url 'room-detail' equipment.room.pk %}" class="btn btn-secondary">Retourner à la salle</a> <a href="{% url 'room-detail' equipment.room.pk %}"
<a href="{% url 'setting-create' %}?equipment={{ equipment.pk }}" class="btn btn-success">Ajouter un réglage</a> class="btn btn-secondary">Retourner à la salle</a>
<a href="{% url 'setting-create' %}?equipment={{ equipment.pk }}"
class="btn btn-success">Ajouter un réglage</a>
{% if session %}
<a href="{% url 'round-create' %}?equipment={{ equipment.pk }}&session={{ session.pk }}"
class="btn btn-success">Commencer une série</a>
{% endif %}
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View file

@ -0,0 +1,31 @@
{% extends 'gym/base.html' %}
{% load bootstrap4 %}
{% block content %}
<div class="row">
<div class="col-12">
<h1>{% block title %}{{ title }} une série{% endblock %}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
{% if equipment %}
<a href="{% url 'equipment-detail' equipment.pk %}" class="btn btn-secondary">Retourner à la machine</a>
{% endif %}
</div>
</div>
<div class="row">
<div class="col-12">
<form action="" method="post">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-success">Valider</button>
{% if edit %}
<a href="{% url 'setting-delete' setting.pk %}" class="btn btn-danger">Supprimer</a>
{% endif %}
{% endbuttons %}
</form>
</div>
</div>
{% endblock %}

View file

@ -34,7 +34,7 @@
<h2>Machines</h2> <h2>Machines</h2>
<ul class="list-group"> <ul class="list-group">
{% for equipment in session.room.equipments.all %} {% for equipment in session.room.equipments.all %}
<a href="{% url 'equipment-detail' equipment.pk %}" class="list-group-item">{{ equipment.name }}</a> <a href="{% url 'equipment-detail' equipment.pk %}?session={{ session.pk }}" class="list-group-item">{{ equipment.name }}</a>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View file

@ -14,6 +14,7 @@ urlpatterns = [
path('setting/<int:pk>/delete', views.SettingDeleteView.as_view(), name='setting-delete'), path('setting/<int:pk>/delete', views.SettingDeleteView.as_view(), name='setting-delete'),
path('session/start/', views.SessionCreateView.as_view(), name='session-start'), path('session/start/', views.SessionCreateView.as_view(), name='session-start'),
path('session/<int:pk>/', views.SessionDetailView.as_view(), name='session-detail'), path('session/<int:pk>/', views.SessionDetailView.as_view(), name='session-detail'),
path('round/add/', views.RoundCreateView.as_view(), name='round-create'),
# path('session/<int:pk>/delete', views.SessionDeleteView.as_view(), name='session-delete'), # path('session/<int:pk>/delete', views.SessionDeleteView.as_view(), name='session-delete'),
] ]

View file

@ -4,7 +4,7 @@ from django.shortcuts import get_object_or_404
from django.urls import reverse from django.urls import reverse
from django.views import generic from django.views import generic
from gym.models import Room, Equipment, Setting, Session from gym.models import Room, Equipment, Setting, Session, Round
class RoomListView(generic.ListView): class RoomListView(generic.ListView):
@ -23,6 +23,19 @@ class EquipmentDetailView(generic.DetailView):
model = Equipment model = Equipment
context_object_name = 'equipment' context_object_name = 'equipment'
template_name = 'gym/equipment.html' template_name = 'gym/equipment.html'
session = None
def dispatch(self, request, *args, **kwargs):
default_session_id = self.request.GET.get('session')
if default_session_id:
self.session = get_object_or_404(Room, pk=default_session_id)
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['session'] = self.session
return context
class EquipmentCreateView(generic.CreateView): class EquipmentCreateView(generic.CreateView):
@ -133,3 +146,42 @@ class SessionDetailView(generic.DetailView):
model = Session model = Session
context_object_name = 'session' context_object_name = 'session'
template_name = 'gym/session_detail.html' template_name = 'gym/session_detail.html'
class RoundCreateView(generic.CreateView):
model = Round
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'notes']
template_name = 'gym/round_edit.html'
equipment = None
session = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Commencer'
context['equipment'] = self.equipment
context['session'] = self.session
return context
def dispatch(self, request, *args, **kwargs):
self.equipment = get_object_or_404(Equipment, pk=self.request.GET.get('equipment'))
self.session = get_object_or_404(Equipment, pk=self.request.GET.get('session'))
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
theoretical_max_percentage = 65
initial = super().get_initial()
initial['equipment'] = self.equipment
initial['session'] = self.session
initial['repetition_number'] = 3
initial['theoretical_max_percentage'] = theoretical_max_percentage
proposed_weight = 0
theoretical_max = self.equipment.last_theoretical_max
if theoretical_max:
proposed_weight = theoretical_max.value * theoretical_max_percentage / 100
initial['chosen_weight'] = proposed_weight
return initial
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})