Add work forms and some default values

This commit is contained in:
Gabriel Augendre 2018-03-13 09:24:12 +01:00
parent 68e18c406a
commit 9f4834547f
6 changed files with 105 additions and 11 deletions

View file

@ -1,6 +1,6 @@
from django.contrib import admin
from gym.models import Room, Equipment, TheoreticalMax, Session, Round, Unit
from gym.models import Room, Equipment, TheoreticalMax, Session, Round, Unit, WorkForm
@admin.register(Room)
@ -31,3 +31,8 @@ class RoundAdmin(admin.ModelAdmin):
@admin.register(Unit)
class UnitAdmin(admin.ModelAdmin):
pass
@admin.register(WorkForm)
class WorkFormAdmin(admin.ModelAdmin):
pass

View file

@ -0,0 +1,40 @@
# Generated by Django 2.0.2 on 2018-03-13 08:13
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('gym', '0007_auto_20180313_0857'),
]
operations = [
migrations.CreateModel(
name='WorkForm',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=300, verbose_name='nom')),
],
options={
'verbose_name': 'forme de travail',
'verbose_name_plural': 'formes de travail',
},
),
migrations.AddField(
model_name='equipment',
name='default_repetition_number',
field=models.PositiveIntegerField(default=12, verbose_name='nombre de répétitions par défaut'),
),
migrations.AddField(
model_name='equipment',
name='default_work_form',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='gym.WorkForm', verbose_name='forme de travail par défaut'),
),
migrations.AddField(
model_name='round',
name='work_form',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='gym.WorkForm', verbose_name='forme de travail'),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 2.0.2 on 2018-03-13 08:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('gym', '0008_auto_20180313_0913'),
]
operations = [
migrations.AddField(
model_name='session',
name='default_theoretical_max_percentage',
field=models.PositiveIntegerField(default=65, verbose_name='pourcentage par défaut'),
),
]

View file

@ -26,6 +26,17 @@ class Unit(models.Model):
return self.name
class WorkForm(models.Model):
class Meta:
verbose_name = 'forme de travail'
verbose_name_plural = 'formes de travail'
name = models.CharField('nom', max_length=300)
def __str__(self):
return self.name
class Equipment(models.Model):
class Meta:
verbose_name = 'machine'
@ -45,6 +56,13 @@ class Equipment(models.Model):
on_delete=models.PROTECT,
null=True
)
default_work_form = models.ForeignKey(
verbose_name='forme de travail par défaut',
to=WorkForm,
on_delete=models.SET_NULL,
null=True
)
default_repetition_number = models.PositiveIntegerField('nombre de répétitions par défaut', default=12)
@property
def last_theoretical_max(self):
@ -103,6 +121,7 @@ class Session(models.Model):
related_name='sessions',
null=True
)
default_theoretical_max_percentage = models.PositiveIntegerField('pourcentage par défaut', default=65)
notes = models.TextField('notes', blank=True)
def __str__(self):
@ -129,4 +148,10 @@ class Round(models.Model):
on_delete=models.CASCADE,
related_name='rounds'
)
work_form = models.ForeignKey(
verbose_name='forme de travail',
to=WorkForm,
on_delete=models.PROTECT,
null=True
)
notes = models.TextField('notes', blank=True)

View file

@ -15,6 +15,7 @@
<ul>
<li>{{ session.room.name }}</li>
<li>{{ session.start }}</li>
<li>{{ session.default_theoretical_max_percentage }}%</li>
{% if session.notes %}
<li>{{ session.notes }}</li>
{% endif %}

View file

@ -67,7 +67,7 @@ class EquipmentDetailView(LoginRequiredMixin, generic.DetailView):
class EquipmentCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
model = Equipment
fields = ['room', 'name']
fields = ['room', 'name', 'default_work_form', 'default_repetition_number']
template_name = 'gym/equipment_edit.html'
room = None
@ -225,7 +225,7 @@ class TheoreticalMaxCreateView(LoginRequiredMixin, QuickActionsMixin, generic.Cr
class SessionCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
model = Session
fields = ['room', 'start']
fields = ['room', 'start', 'default_theoretical_max_percentage']
template_name = 'gym/session_edit.html'
room = None
@ -287,10 +287,11 @@ class SessionDetailView(LoginRequiredMixin, QuickActionsMixin, generic.DetailVie
class RoundCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
model = Round
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes']
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight',
'repetition_number', 'work_form', 'notes']
template_name = 'gym/round_edit.html'
equipment = None
session_pk = None
session = None
def get_quick_actions(self):
return [
@ -309,16 +310,19 @@ class RoundCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView)
def dispatch(self, request, *args, **kwargs):
self.equipment = get_object_or_404(Equipment, pk=self.request.GET.get('equipment'))
self.session_pk = self.request.session.get('session_pk')
session_pk = self.request.session.get('session_pk')
if session_pk:
self.session = Session.objects.get(pk=session_pk)
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
theoretical_max_percentage = 65
theoretical_max_percentage = self.session.default_theoretical_max_percentage
initial = super().get_initial()
initial['equipment'] = self.equipment
initial['session'] = self.session_pk
initial['repetition_number'] = 12
initial['session'] = self.session
initial['repetition_number'] = self.equipment.default_repetition_number or 12
initial['work_form'] = self.equipment.default_work_form
initial['theoretical_max_percentage'] = theoretical_max_percentage
proposed_weight = 0
theoretical_max = self.equipment.last_theoretical_max
@ -334,7 +338,8 @@ class RoundCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView)
class RoundUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = Round
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes']
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight',
'repetition_number', 'work_form', 'notes']
template_name = 'gym/round_edit.html'
def get_quick_actions(self):
@ -349,7 +354,7 @@ class RoundUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['edit'] = True
context['title'] = 'Commencer'
context['title'] = 'Modifier'
context['equipment'] = self.object.equipment
return context