From f9ba5b9444c9228ef517d547c5c9a5caf4fd75b3 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 4 Mar 2018 09:23:35 +0100 Subject: [PATCH] Implement theoretical max --- Pipfile | 2 +- Pipfile.lock | 2 +- gym/models.py | 7 +- gym/static/gym/round.js | 8 ++ gym/templates/gym/base.html | 3 + gym/templates/gym/equipment.html | 51 ++++++++++--- gym/templates/gym/equipment_edit.html | 5 ++ gym/templates/gym/room.html | 4 +- gym/templates/gym/round_edit.html | 9 ++- gym/templates/gym/session_detail.html | 12 ++- gym/templates/gym/setting_edit.html | 2 +- gym/templates/gym/theoretical_max_edit.html | 31 ++++++++ gym/urls.py | 1 + gym/views.py | 84 +++++++++++++++++++-- muscu/settings.py | 2 + 15 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 gym/static/gym/round.js create mode 100644 gym/templates/gym/theoretical_max_edit.html diff --git a/Pipfile b/Pipfile index da3e4c9..97178fc 100644 --- a/Pipfile +++ b/Pipfile @@ -7,7 +7,7 @@ name = "pypi" [packages] -django = "*" +Django = "*" djangorestframework = "*" "django-bootstrap4" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 425954d..efcd38d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "c3ee9bacf90669123493994e4f194606cd953134dd819c73684629335c5a7ee6" + "sha256": "236f27f0ee84bf5dfc608ea235c7edb929fd32228bc5e98b7f269cc52d0fc312" }, "host-environment-markers": { "implementation_name": "cpython", diff --git a/gym/models.py b/gym/models.py index cd18ada..a38d59c 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('-date').first() + return self.theoretical_maxs.order_by('-date', '-pk').first() def __str__(self): return f'{self.name} ({self.room.name})' @@ -70,7 +70,7 @@ class TheoreticalMax(models.Model): value = models.FloatField('valeur') def __str__(self): - return f'{self.value}kg le {self.date}' + return f'{self.value} le {self.date}' class Session(models.Model): @@ -88,6 +88,9 @@ class Session(models.Model): ) notes = models.TextField('notes', blank=True) + def __str__(self): + return f'{self.room} {self.start:%d/%m/%Y %H:%M}' + class Round(models.Model): class Meta: diff --git a/gym/static/gym/round.js b/gym/static/gym/round.js new file mode 100644 index 0000000..4c2d99e --- /dev/null +++ b/gym/static/gym/round.js @@ -0,0 +1,8 @@ +$(document).ready(function () { + console.log(max_theorique); + $('#id_theoretical_max_percentage').change(function(event) { + console.log(event); + var proposed_weight = max_theorique / 100 * Number($('#id_theoretical_max_percentage').val()); + $('#id_chosen_weight').val(proposed_weight); + }) +}); \ No newline at end of file diff --git a/gym/templates/gym/base.html b/gym/templates/gym/base.html index db8a458..9205593 100644 --- a/gym/templates/gym/base.html +++ b/gym/templates/gym/base.html @@ -33,5 +33,8 @@ + +{% block js %} +{% endblock %} \ No newline at end of file diff --git a/gym/templates/gym/equipment.html b/gym/templates/gym/equipment.html index 67d05f9..8c18120 100644 --- a/gym/templates/gym/equipment.html +++ b/gym/templates/gym/equipment.html @@ -1,4 +1,5 @@ {% extends 'gym/base.html' %} +{% load humanize %} {% block content %}
@@ -8,25 +9,51 @@
+
+
+
+

Max théorique

+ {% if equipment.last_theoretical_max %} +
    +
  • + {{ equipment.last_theoretical_max }} +
  • +
+ {% else %} +

Pas de max théorique pour le moment.

{% endif %}

Réglages

- + {% if equipment.settings.all %} + + {% else %} +

Pas de réglage pour le moment.

+ {% endif %}
{% endblock %} \ No newline at end of file diff --git a/gym/templates/gym/equipment_edit.html b/gym/templates/gym/equipment_edit.html index 1225189..1cdcf11 100644 --- a/gym/templates/gym/equipment_edit.html +++ b/gym/templates/gym/equipment_edit.html @@ -7,6 +7,11 @@

{% block title %}Ajouter une machine{% endblock %}

+
+ +
diff --git a/gym/templates/gym/room.html b/gym/templates/gym/room.html index 0595a7d..5edd5d3 100644 --- a/gym/templates/gym/room.html +++ b/gym/templates/gym/room.html @@ -23,12 +23,12 @@
- {% if room.sessions.all %} + {% if sessions %}

Séances

    - {% for session in room.sessions.all %} + {% for session in sessions %} {{ session.start }} {% endfor %}
diff --git a/gym/templates/gym/round_edit.html b/gym/templates/gym/round_edit.html index c950846..271e5ec 100644 --- a/gym/templates/gym/round_edit.html +++ b/gym/templates/gym/round_edit.html @@ -1,5 +1,7 @@ {% extends 'gym/base.html' %} {% load bootstrap4 %} +{% load static %} +{% load l10n %} {% block content %}
@@ -10,7 +12,7 @@
{% if equipment %} - Retourner à la machine + Retourner à la machine {% endif %}
@@ -28,4 +30,9 @@
+{% endblock %} + +{% block js %} + + {% endblock %} \ No newline at end of file diff --git a/gym/templates/gym/session_detail.html b/gym/templates/gym/session_detail.html index 7ce476c..a94c4ce 100644 --- a/gym/templates/gym/session_detail.html +++ b/gym/templates/gym/session_detail.html @@ -10,7 +10,7 @@
@@ -39,4 +39,14 @@
+
+
+

Séries

+ +
+
{% endblock %} \ No newline at end of file diff --git a/gym/templates/gym/setting_edit.html b/gym/templates/gym/setting_edit.html index cf80636..d27e666 100644 --- a/gym/templates/gym/setting_edit.html +++ b/gym/templates/gym/setting_edit.html @@ -10,7 +10,7 @@
{% if equipment %} - Retourner à la machine + Retourner à la machine {% endif %}
diff --git a/gym/templates/gym/theoretical_max_edit.html b/gym/templates/gym/theoretical_max_edit.html new file mode 100644 index 0000000..62111f9 --- /dev/null +++ b/gym/templates/gym/theoretical_max_edit.html @@ -0,0 +1,31 @@ +{% extends 'gym/base.html' %} +{% load bootstrap4 %} + +{% block content %} +
+
+

{% block title %}{{ title }} un max théorique{% 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/urls.py b/gym/urls.py index a050973..c93d96a 100644 --- a/gym/urls.py +++ b/gym/urls.py @@ -15,6 +15,7 @@ urlpatterns = [ 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('theoretical-max/add/', views.TheoreticalMaxCreateView.as_view(), name='theoretical-max-create'), # path('session//delete', views.SessionDeleteView.as_view(), name='session-delete'), ] diff --git a/gym/views.py b/gym/views.py index e096e83..ca6d1f0 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, Round +from gym.models import Room, Equipment, Setting, Session, Round, TheoreticalMax class RoomListView(generic.ListView): @@ -18,6 +18,11 @@ class RoomDetailView(generic.DetailView): context_object_name = 'room' template_name = 'gym/room.html' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['sessions'] = self.object.sessions.all().order_by('-start') + return context + class EquipmentDetailView(generic.DetailView): model = Equipment @@ -26,9 +31,7 @@ class EquipmentDetailView(generic.DetailView): 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) + self.session = self.request.GET.get('session') return super().dispatch(request, *args, **kwargs) @@ -43,15 +46,26 @@ class EquipmentCreateView(generic.CreateView): fields = ['room', 'name'] template_name = 'gym/equipment_edit.html' room = None + session = None + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['room'] = self.room + return context def get_success_url(self): - return reverse('equipment-detail', kwargs={'pk': self.object.pk}) + url = reverse('equipment-detail', kwargs={'pk': self.object.pk}) + if self.session: + return "{}?session={}".format(url, self.session) + return url def dispatch(self, request, *args, **kwargs): default_room_id = self.request.GET.get('room') if default_room_id: self.room = get_object_or_404(Room, pk=default_room_id) + self.session = self.request.GET.get('session') + return super().dispatch(request, *args, **kwargs) def get_initial(self): @@ -66,11 +80,13 @@ class SettingCreateView(generic.CreateView): fields = ['equipment', 'name', 'value'] template_name = 'gym/setting_edit.html' equipment = None + session = None def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['title'] = 'Ajouter' context['equipment'] = self.equipment + context['session'] = self.session return context def dispatch(self, request, *args, **kwargs): @@ -78,6 +94,8 @@ class SettingCreateView(generic.CreateView): if default_equipment_id: self.equipment = get_object_or_404(Equipment, pk=default_equipment_id) + self.session = self.request.GET.get('session') + return super().dispatch(request, *args, **kwargs) def get_initial(self): @@ -87,23 +105,36 @@ class SettingCreateView(generic.CreateView): return initial def get_success_url(self): - return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) + url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) + if self.session: + return '{}?session={}'.format(url, self.session) + return url class SettingUpdateView(generic.UpdateView): model = Setting fields = ['equipment', 'name', 'value'] template_name = 'gym/setting_edit.html' + session = None + + def dispatch(self, request, *args, **kwargs): + self.session = self.request.GET.get('session') + + return super().dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['edit'] = True context['title'] = 'Modifier' context['equipment'] = self.object.equipment + context['session'] = self.request.GET.get('session') return context def get_success_url(self): - return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) + url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) + if self.session: + return '{}?session={}'.format(url, self.session) + return url class SettingDeleteView(generic.DeleteView): @@ -115,6 +146,43 @@ class SettingDeleteView(generic.DeleteView): return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) +class TheoreticalMaxCreateView(generic.CreateView): + model = TheoreticalMax + fields = ['equipment', 'date', 'value'] + template_name = 'gym/theoretical_max_edit.html' + equipment = None + session = None + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['title'] = 'Ajouter' + context['equipment'] = self.equipment + context['session'] = self.session + return context + + def dispatch(self, request, *args, **kwargs): + default_equipment_id = self.request.GET.get('equipment') + if default_equipment_id: + self.equipment = get_object_or_404(Equipment, pk=default_equipment_id) + + self.session = self.request.GET.get('session') + + return super().dispatch(request, *args, **kwargs) + + def get_initial(self): + initial = super().get_initial() + initial['equipment'] = self.equipment + initial['date'] = datetime.date.today() + + return initial + + def get_success_url(self): + url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) + if self.session: + return '{}?session={}'.format(url, self.session) + return url + + class SessionCreateView(generic.CreateView): model = Session fields = ['room', 'start'] @@ -150,7 +218,7 @@ class SessionDetailView(generic.DetailView): class RoundCreateView(generic.CreateView): model = Round - fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'notes'] + fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes'] template_name = 'gym/round_edit.html' equipment = None session = None diff --git a/muscu/settings.py b/muscu/settings.py index 1f27932..07da814 100644 --- a/muscu/settings.py +++ b/muscu/settings.py @@ -35,6 +35,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.humanize', 'gym', 'bootstrap4', ] @@ -114,3 +115,4 @@ USE_TZ = True # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')