diff --git a/gym/models.py b/gym/models.py index b1c329f..cd18ada 100644 --- a/gym/models.py +++ b/gym/models.py @@ -31,7 +31,7 @@ class Equipment(models.Model): @property def last_theoretical_max(self): - return self.theoretical_maxs.order_by('-start').first() + return self.theoretical_maxs.order_by('-date').first() def __str__(self): return f'{self.name} ({self.room.name})' diff --git a/gym/templates/gym/equipment.html b/gym/templates/gym/equipment.html index f6a93cf..67d05f9 100644 --- a/gym/templates/gym/equipment.html +++ b/gym/templates/gym/equipment.html @@ -8,8 +8,14 @@
- Retourner à la salle - Ajouter un réglage + Retourner à la salle + Ajouter un réglage + {% if session %} + Commencer une série + {% endif %}
diff --git a/gym/templates/gym/round_edit.html b/gym/templates/gym/round_edit.html new file mode 100644 index 0000000..c950846 --- /dev/null +++ b/gym/templates/gym/round_edit.html @@ -0,0 +1,31 @@ +{% extends 'gym/base.html' %} +{% load bootstrap4 %} + +{% block content %} +
+
+

{% block title %}{{ title }} une série{% endblock %}

+
+
+
+
+ {% if equipment %} + Retourner à la machine + {% endif %} +
+
+
+
+
+ {% csrf_token %} + {% bootstrap_form form %} + {% buttons %} + + {% if edit %} + Supprimer + {% endif %} + {% endbuttons %} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/gym/templates/gym/session_detail.html b/gym/templates/gym/session_detail.html index 4ddf0e5..7ce476c 100644 --- a/gym/templates/gym/session_detail.html +++ b/gym/templates/gym/session_detail.html @@ -34,7 +34,7 @@

Machines

diff --git a/gym/urls.py b/gym/urls.py index 11ce67d..a050973 100644 --- a/gym/urls.py +++ b/gym/urls.py @@ -14,6 +14,7 @@ urlpatterns = [ path('setting//delete', views.SettingDeleteView.as_view(), name='setting-delete'), path('session/start/', views.SessionCreateView.as_view(), name='session-start'), path('session//', views.SessionDetailView.as_view(), name='session-detail'), + path('round/add/', views.RoundCreateView.as_view(), name='round-create'), # path('session//delete', views.SessionDeleteView.as_view(), name='session-delete'), ] diff --git a/gym/views.py b/gym/views.py index 9ebcdfd..e096e83 100644 --- a/gym/views.py +++ b/gym/views.py @@ -4,7 +4,7 @@ from django.shortcuts import get_object_or_404 from django.urls import reverse 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): @@ -23,6 +23,19 @@ class EquipmentDetailView(generic.DetailView): model = Equipment context_object_name = 'equipment' 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): @@ -133,3 +146,42 @@ class SessionDetailView(generic.DetailView): model = Session context_object_name = 'session' 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})