Add Round Update view + refactor code

This commit is contained in:
Gabriel Augendre 2018-03-13 09:06:28 +01:00
parent f3e8dd578d
commit 68e18c406a
7 changed files with 123 additions and 24 deletions

View file

@ -0,0 +1,23 @@
# Generated by Django 2.0.2 on 2018-03-13 07:57
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('gym', '0006_auto_20180304_2037'),
]
operations = [
migrations.RemoveField(
model_name='theoreticalmax',
name='unit',
),
migrations.AddField(
model_name='equipment',
name='unit',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='gym.Unit', verbose_name='unité'),
),
]

View file

@ -15,6 +15,17 @@ class Room(models.Model):
return self.name
class Unit(models.Model):
class Meta:
verbose_name = 'unité'
verbose_name_plural = 'unités'
name = models.CharField('nom', max_length=50)
def __str__(self):
return self.name
class Equipment(models.Model):
class Meta:
verbose_name = 'machine'
@ -28,6 +39,12 @@ class Equipment(models.Model):
related_name='equipments',
null=True
)
unit = models.ForeignKey(
verbose_name='unité',
to=Unit,
on_delete=models.PROTECT,
null=True
)
@property
def last_theoretical_max(self):
@ -55,17 +72,6 @@ class Setting(models.Model):
return f'{self.name}={self.value}'
class Unit(models.Model):
class Meta:
verbose_name = 'unité'
verbose_name_plural = 'unités'
name = models.CharField('nom', max_length=50)
def __str__(self):
return self.name
class TheoreticalMax(models.Model):
class Meta:
verbose_name = 'maximum théorique'
@ -79,15 +85,9 @@ class TheoreticalMax(models.Model):
)
date = models.DateField('date')
value = models.FloatField('valeur')
unit = models.ForeignKey(
verbose_name='unité',
to=Unit,
on_delete=models.PROTECT,
null=True
)
def __str__(self):
return f'{self.value} {self.unit.name} le {self.date}'
return f'{self.value} {self.equipment.unit.name} le {self.date}'
class Session(models.Model):

View file

@ -0,0 +1,17 @@
{% extends 'gym/base.html' %}
{% load bootstrap4 %}
{% block title %}Supprimer {{ article }} {{ name }}{% endblock %}
{% block h1 %}Supprimer {{ article }} {{ name }}{% endblock %}
{% block content %}
<div class="row">
<div class="col-12">
<form action="" method="post">
{% csrf_token %}
<p>{{ message|safe }}</p>
<input class="btn btn-danger" type="submit" value="Confirmer"/>
</form>
</div>
</div>
{% endblock %}

View file

@ -81,7 +81,7 @@
{% if rounds %}
<ul class="list-group">
{% for round in rounds %}
<a href="#" class="list-group-item">{{ round.repetition_number }} répétitions
<a href="{% url 'round-edit' round.pk %}" class="list-group-item">{{ round.repetition_number }} répétitions
&times; {{ round.chosen_weight }}</a>
{% endfor %}
</ul>

View file

@ -15,7 +15,7 @@
{% 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>
<a href="{% url 'round-delete' round.pk %}" class="btn btn-danger">Supprimer</a>
{% endif %}
{% endbuttons %}
</form>

View file

@ -11,11 +11,13 @@ urlpatterns = [
path('equipment/<int:pk>/', views.EquipmentDetailView.as_view(), name='equipment-detail'),
path('setting/add/', views.SettingCreateView.as_view(), name='setting-create'),
path('setting/<int:pk>/', views.SettingUpdateView.as_view(), name='setting-edit'),
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/<int:pk>/', views.SessionDetailView.as_view(), name='session-detail'),
path('round/add/', views.RoundCreateView.as_view(), name='round-create'),
path('round/<int:pk>/', views.RoundUpdateView.as_view(), name='round-edit'),
path('round/<int:pk>/delete/', views.RoundDeleteView.as_view(), name='round-delete'),
path('theoretical-max/add/', views.TheoreticalMaxCreateView.as_view(), name='theoretical-max-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

@ -167,8 +167,18 @@ class SettingUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateVie
class SettingDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Setting
template_name = 'gym/setting_confirm_delete.html'
context_object_name = 'setting'
template_name = 'gym/confirm_delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['article'] = 'une'
context['name'] = 'série'
context['message'] = ('Êtes-vous sûr de vouloir supprimer le réglage '
'<code>{}</code> pour la machine "{}" ?'.format(
self.object,
self.object.equipment,
))
return context
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
@ -320,3 +330,50 @@ class RoundCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView)
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
class RoundUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = Round
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes']
template_name = 'gym/round_edit.html'
def get_quick_actions(self):
return [
{
'url': reverse('equipment-detail', args=(self.object.equipment.pk,)),
'category': 'secondary',
'display': 'Retourner à la machine'
},
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['edit'] = True
context['title'] = 'Commencer'
context['equipment'] = self.object.equipment
return context
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
class RoundDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Round
template_name = 'gym/confirm_delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['article'] = 'une'
context['name'] = 'série'
context['message'] = ('Êtes-vous sûr de vouloir supprimer '
'la série <code>{}, {}x{}{}</code> pour la séance "{}" ?'.format(
self.object.equipment,
self.object.repetition_number,
self.object.chosen_weight,
self.object.equipment.unit,
self.object.session
))
return context
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})