workout/gym/views.py

246 lines
7.8 KiB
Python
Raw Normal View History

2018-03-03 18:16:27 +01:00
import datetime
2018-03-02 12:01:51 +01:00
2018-03-03 18:16:27 +01:00
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import generic
2018-03-03 14:24:57 +01:00
2018-03-04 09:23:35 +01:00
from gym.models import Room, Equipment, Setting, Session, Round, TheoreticalMax
2018-03-03 14:24:57 +01:00
2018-03-03 18:16:27 +01:00
class RoomListView(generic.ListView):
queryset = Room.objects.all().order_by('name')
context_object_name = 'rooms'
template_name = 'gym/rooms.html'
2018-03-03 14:24:57 +01:00
def dispatch(self, request, *args, **kwargs):
2018-03-04 09:50:39 +01:00
if 'session_pk' in request.session:
del request.session['session_pk']
return super().dispatch(request, *args, **kwargs)
2018-03-03 14:24:57 +01:00
2018-03-03 18:16:27 +01:00
class RoomDetailView(generic.DetailView):
model = Room
context_object_name = 'room'
template_name = 'gym/room.html'
def dispatch(self, request, *args, **kwargs):
2018-03-04 09:50:39 +01:00
if 'session_pk' in request.session:
del request.session['session_pk']
return super().dispatch(request, *args, **kwargs)
2018-03-04 09:23:35 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['sessions'] = self.object.sessions.all().order_by('-start')
return context
2018-03-03 18:16:27 +01:00
class EquipmentDetailView(generic.DetailView):
model = Equipment
context_object_name = 'equipment'
template_name = 'gym/equipment.html'
session = None
def dispatch(self, request, *args, **kwargs):
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)
2018-03-03 18:33:55 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.session:
context['session'] = self.session.pk
context['rounds'] = Round.objects.filter(session=self.session, equipment=self.get_object())
2018-03-03 18:33:55 +01:00
return context
2018-03-03 18:16:27 +01:00
class EquipmentCreateView(generic.CreateView):
model = Equipment
fields = ['room', 'name']
template_name = 'gym/equipment_edit.html'
room = None
2018-03-04 09:23:35 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['room'] = self.room
return context
2018-03-03 18:16:27 +01:00
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.pk})
2018-03-03 18:16:27 +01:00
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)
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial()
initial['room'] = self.room
return initial
class SettingCreateView(generic.CreateView):
model = Setting
fields = ['equipment', 'name', 'value']
template_name = 'gym/setting_edit.html'
equipment = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Ajouter'
context['equipment'] = self.equipment
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)
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial()
initial['equipment'] = self.equipment
return initial
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
2018-03-03 18:16:27 +01:00
class SettingUpdateView(generic.UpdateView):
model = Setting
fields = ['equipment', 'name', 'value']
template_name = 'gym/setting_edit.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['edit'] = True
context['title'] = 'Modifier'
context['equipment'] = self.object.equipment
return context
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
2018-03-03 18:16:27 +01:00
class SettingDeleteView(generic.DeleteView):
model = Setting
template_name = 'gym/setting_confirm_delete.html'
context_object_name = 'setting'
def get_success_url(self):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
2018-03-04 09:23:35 +01:00
class TheoreticalMaxCreateView(generic.CreateView):
model = TheoreticalMax
fields = ['equipment', 'date', 'value']
template_name = 'gym/theoretical_max_edit.html'
equipment = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Ajouter'
context['equipment'] = self.equipment
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)
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):
return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
2018-03-04 09:23:35 +01:00
2018-03-03 18:16:27 +01:00
class SessionCreateView(generic.CreateView):
model = Session
fields = ['room', 'start']
template_name = 'gym/session_edit.html'
room = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Démarrer'
context['room'] = self.room
return context
def get_success_url(self):
session_pk = self.object.pk
self.request.session['session_pk'] = session_pk
return reverse('session-detail', kwargs={'pk': session_pk})
2018-03-03 18:16:27 +01:00
def dispatch(self, request, *args, **kwargs):
self.room = get_object_or_404(Room, pk=self.request.GET.get('room'))
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial()
initial['room'] = self.room
initial['start'] = datetime.datetime.now()
return initial
2018-03-03 14:24:57 +01:00
2018-03-03 18:16:27 +01:00
class SessionDetailView(generic.DetailView):
model = Session
context_object_name = 'session'
template_name = 'gym/session_detail.html'
2018-03-03 18:33:55 +01:00
def get(self, request, *args, **kwargs):
request.session['session_pk'] = self.get_object().pk
return super().get(request, *args, **kwargs)
2018-03-03 18:33:55 +01:00
class RoundCreateView(generic.CreateView):
model = Round
2018-03-04 09:23:35 +01:00
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes']
2018-03-03 18:33:55 +01:00
template_name = 'gym/round_edit.html'
equipment = None
session_pk = None
2018-03-03 18:33:55 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Commencer'
context['equipment'] = self.equipment
return context
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')
2018-03-03 18:33:55 +01:00
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_pk
2018-03-03 18:33:55 +01:00
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})