workout/gym/views.py

576 lines
19 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-30 20:25:11 +02:00
import plotly
import plotly.graph_objs as go
2018-03-04 18:45:31 +01:00
from django.contrib.auth.mixins import LoginRequiredMixin
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
2019-06-23 15:54:59 +02:00
from gym.forms import RoundForm
2018-03-04 21:45:50 +01:00
from gym.mixins import SessionResetMixin, QuickActionsMixin
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
class RoomListView(LoginRequiredMixin, SessionResetMixin, generic.ListView):
2019-06-23 15:39:13 +02:00
queryset = Room.objects.all().order_by("name")
context_object_name = "rooms"
template_name = "gym/rooms.html"
2018-03-03 14:24:57 +01:00
2019-06-23 15:39:13 +02:00
class RoomDetailView(
LoginRequiredMixin, QuickActionsMixin, SessionResetMixin, generic.DetailView
):
2018-03-03 18:16:27 +01:00
model = Room
2019-06-23 15:39:13 +02:00
context_object_name = "room"
template_name = "gym/room.html"
2018-03-03 18:16:27 +01:00
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
2018-04-02 14:34:27 +02:00
quick_actions = [
2018-03-04 21:45:50 +01:00
{
2019-06-23 15:39:13 +02:00
"url": reverse("rooms-list"),
"category": "secondary",
"display": "Liste des salles",
2018-03-04 21:45:50 +01:00
},
{
2019-06-23 15:39:13 +02:00
"url": "{}?room={}".format(reverse("session-start"), self.object.pk),
"category": "primary",
"display": "Commencer une séance",
2018-03-04 21:45:50 +01:00
},
2018-04-02 14:34:27 +02:00
{
2019-06-23 15:39:13 +02:00
"url": "{}?room={}".format(reverse("equipment-create"), self.object.pk),
"category": "success",
"display": "Ajouter une machine",
2018-04-02 14:34:27 +02:00
},
2018-03-04 21:45:50 +01:00
]
2018-04-02 14:34:27 +02:00
if self.object.latitude and self.object.longitude:
from user_agents import parse
2019-06-23 15:39:13 +02:00
user_agent = parse(self.request.META.get("HTTP_USER_AGENT"))
if user_agent.is_mobile:
url = f"waze://?ll={self.object.latitude},{self.object.longitude}&navigate=yes"
2019-06-23 15:39:13 +02:00
name = "Waze"
quick_actions.append(
{
"url": f"here-location://{self.object.latitude},{self.object.longitude}",
"category": "info",
"display": "Here WeGo",
"icon": "fas fa-location-arrow",
}
)
else:
2019-06-23 15:39:13 +02:00
url = (
f"https://www.google.com/maps/place/{self.object.latitude},{self.object.longitude}"
f"/@{self.object.latitude},{self.object.longitude},18z"
)
name = "Google Maps"
quick_actions.append(
{
"url": url,
"category": "info",
"display": name,
"icon": "fas fa-location-arrow",
}
)
2018-04-02 14:34:27 +02:00
return quick_actions
2018-03-04 09:23:35 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["sessions"] = self.object.sessions.all().order_by("-start")
2018-03-04 09:23:35 +01:00
return context
2018-03-03 18:16:27 +01:00
class EquipmentDetailView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView):
2018-03-03 18:16:27 +01:00
model = Equipment
2019-06-23 15:39:13 +02:00
context_object_name = "equipment"
template_name = "gym/equipment.html"
session = None
def get_quick_actions(self):
lst = []
if self.session:
2019-06-23 15:39:13 +02:00
lst.extend(
[
{
"url": reverse("session-detail", args=(self.session.pk,)),
"category": "secondary",
"display": "Retourner à la séance",
},
{
"url": "{}?equipment={}".format(
reverse("round-create"), self.object.pk
),
"category": "primary",
"display": "Commencer une série",
},
]
)
else:
lst.append(
{
2019-06-23 15:39:13 +02:00
"url": reverse("room-detail", args=(self.object.room.pk,)),
"category": "secondary",
"display": "Retourner à la salle",
}
)
lst.extend(
[
{
"url": "{}?equipment={}".format(
reverse("setting-create"), self.object.pk
),
"category": "success",
"display": "Ajouter un réglage",
},
{
2019-06-23 15:39:13 +02:00
"url": "{}?equipment={}".format(
reverse("theoretical-max-create"), self.object.pk
),
"category": "success",
"display": "Ajouter un max théorique",
},
]
)
if self.request.user.is_staff:
2019-06-23 15:39:13 +02:00
lst.append(
{
"url": reverse(
"admin:gym_equipment_change", args=(self.object.pk,)
),
"category": "warning",
"display": "Éditer la machine",
}
)
return lst
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
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:
2019-06-23 15:39:13 +02:00
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
2018-03-04 21:45:50 +01:00
class EquipmentCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
2018-03-03 18:16:27 +01:00
model = Equipment
2019-06-23 15:39:13 +02:00
fields = ["room", "name", "default_work_form", "unit", "default_repetition_number"]
template_name = "gym/equipment_edit.html"
2018-03-03 18:16:27 +01:00
room = None
2018-03-04 09:23:35 +01:00
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("room-detail", args=(self.room.pk,)),
"category": "secondary",
"display": "Retourner à la salle",
}
2018-03-04 21:45:50 +01:00
]
2018-03-04 09:23:35 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["room"] = self.room
2018-03-04 09:23:35 +01:00
return context
2018-03-03 18:16:27 +01:00
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.pk})
2018-03-03 18:16:27 +01:00
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
default_room_id = self.request.GET.get("room")
2018-03-03 18:16:27 +01:00
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()
2019-06-23 15:39:13 +02:00
initial["room"] = self.room
2018-03-03 18:16:27 +01:00
return initial
2018-03-04 21:45:50 +01:00
class SettingCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
2018-03-03 18:16:27 +01:00
model = Setting
2019-06-23 15:39:13 +02:00
fields = ["equipment", "name", "value"]
template_name = "gym/setting_edit.html"
2018-03-03 18:16:27 +01:00
equipment = None
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
}
2018-03-04 21:45:50 +01:00
]
2018-03-03 18:16:27 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["title"] = "Ajouter"
context["equipment"] = self.equipment
2018-03-03 18:16:27 +01:00
return context
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
default_equipment_id = self.request.GET.get("equipment")
2018-03-03 18:16:27 +01:00
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()
2019-06-23 15:39:13 +02:00
initial["equipment"] = self.equipment
2018-03-03 18:16:27 +01:00
return initial
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})
2018-03-03 18:16:27 +01:00
2018-03-04 21:45:50 +01:00
class SettingUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
2018-03-03 18:16:27 +01:00
model = Setting
2019-06-23 15:39:13 +02:00
fields = ["equipment", "name", "value"]
template_name = "gym/setting_edit.html"
2018-03-03 18:16:27 +01:00
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.object.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
}
2018-03-04 21:45:50 +01:00
]
2018-03-03 18:16:27 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["edit"] = True
context["title"] = "Modifier"
context["equipment"] = self.object.equipment
2018-03-03 18:16:27 +01:00
return context
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})
2018-03-03 18:16:27 +01:00
2018-03-04 18:45:31 +01:00
class SettingDeleteView(LoginRequiredMixin, generic.DeleteView):
2018-03-03 18:16:27 +01:00
model = Setting
2019-06-23 15:39:13 +02:00
template_name = "gym/confirm_delete.html"
2018-03-13 09:06:28 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
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
)
)
2018-03-13 09:06:28 +01:00
return context
2018-03-03 18:16:27 +01:00
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})
2018-03-03 18:16:27 +01:00
2019-06-23 15:39:13 +02:00
class TheoreticalMaxCreateView(
LoginRequiredMixin, QuickActionsMixin, generic.CreateView
):
2018-03-04 09:23:35 +01:00
model = TheoreticalMax
2019-06-23 15:39:13 +02:00
fields = ["equipment", "date", "value"]
template_name = "gym/theoretical_max_edit.html"
2018-03-04 09:23:35 +01:00
equipment = None
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
}
2018-03-04 21:45:50 +01:00
]
2018-03-04 09:23:35 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["title"] = "Ajouter"
context["equipment"] = self.equipment
2018-03-04 09:23:35 +01:00
return context
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
default_equipment_id = self.request.GET.get("equipment")
2018-03-04 09:23:35 +01:00
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()
2019-06-23 15:39:13 +02:00
initial["equipment"] = self.equipment
initial["date"] = datetime.date.today()
2018-03-04 09:23:35 +01:00
return initial
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("theoretical-max-list", kwargs={"pk": self.object.equipment.pk})
2018-03-04 09:23:35 +01:00
2018-03-30 20:25:11 +02:00
class TheoreticalMaxListView(LoginRequiredMixin, QuickActionsMixin, generic.ListView):
model = TheoreticalMax
2019-06-23 15:39:13 +02:00
template_name = "gym/theoretical_max_list.html"
context_object_name = "maxs"
2018-03-30 20:25:11 +02:00
equipment = None
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
pk = self.kwargs.pop("pk")
2018-03-30 20:25:11 +02:00
self.equipment = get_object_or_404(Equipment, pk=pk)
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
2019-06-23 15:39:13 +02:00
return TheoreticalMax.objects.filter(equipment=self.equipment).order_by("date")
2018-03-30 20:25:11 +02:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
2018-03-30 20:25:11 +02:00
},
{
2019-06-23 15:39:13 +02:00
"url": "{}?equipment={}".format(
reverse("theoretical-max-create"), self.equipment.pk
),
"category": "success",
"display": "Ajouter un max théorique",
},
2018-03-30 20:25:11 +02:00
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2018-03-30 20:54:58 +02:00
data = [
go.Scatter(
x=[max.date for max in self.get_queryset()],
y=[max.value for max in self.get_queryset()],
2019-06-23 15:39:13 +02:00
)
2018-03-30 20:54:58 +02:00
]
layout = go.Layout(
title="Évolution du max théorique",
yaxis={"title": "Charge ({})".format(self.equipment.unit.name)},
xaxis={"title": "Date"},
)
2019-06-23 15:39:13 +02:00
figure = {"data": data, "layout": layout}
graph = plotly.offline.plot(figure, auto_open=False, output_type="div")
2018-03-30 20:25:11 +02:00
2019-06-23 15:39:13 +02:00
context["graph"] = graph
context["equipment"] = self.equipment
2018-03-30 20:25:11 +02:00
return context
2018-03-04 21:45:50 +01:00
class SessionCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
2018-03-03 18:16:27 +01:00
model = Session
2019-06-23 15:39:13 +02:00
fields = ["room", "start", "default_theoretical_max_percentage"]
template_name = "gym/session_edit.html"
2018-03-03 18:16:27 +01:00
room = None
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
room_pk = self.room.pk
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("room-detail", args=(room_pk,)),
"category": "secondary",
"display": "Retourner à la salle",
}
2018-03-04 21:45:50 +01:00
]
2018-03-03 18:16:27 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["title"] = "Démarrer"
context["room"] = self.room
2018-03-03 18:16:27 +01:00
return context
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("session-detail", kwargs={"pk": self.object.pk})
2018-03-03 18:16:27 +01:00
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
self.room = get_object_or_404(Room, pk=self.request.GET.get("room"))
2018-03-03 18:16:27 +01:00
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial()
2019-06-23 15:39:13 +02:00
initial["room"] = self.room
initial["start"] = datetime.datetime.now()
2018-03-03 18:16:27 +01:00
return initial
2018-03-03 14:24:57 +01:00
2018-03-04 21:45:50 +01:00
class SessionDetailView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView):
2018-03-03 18:16:27 +01:00
model = Session
2019-06-23 15:39:13 +02:00
context_object_name = "session"
template_name = "gym/session_detail.html"
2018-03-03 18:33:55 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
session = self.get_object()
2019-06-23 15:39:13 +02:00
context["used_equipments"] = {
k: Round.objects.filter(equipment=k, session=session).count()
for k in Equipment.objects.filter(rounds__session=self.get_object())
}
return context
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
room_pk = self.get_object().room.pk
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("room-detail", args=(room_pk,)),
"category": "secondary",
"display": "Retourner à la salle",
2018-03-04 21:45:50 +01:00
},
{
2019-06-23 15:39:13 +02:00
"url": "{}?room={}".format(reverse("equipment-create"), room_pk),
"category": "success",
"display": "Ajouter une machine",
2018-03-04 21:45:50 +01:00
},
]
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
request.session["session_pk"] = self.get_object().pk
return super().dispatch(request, *args, **kwargs)
2018-03-03 18:33:55 +01:00
2018-03-04 21:45:50 +01:00
class RoundCreateView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
2018-03-03 18:33:55 +01:00
model = Round
2019-06-23 15:39:13 +02:00
template_name = "gym/round_edit.html"
2018-03-03 18:33:55 +01:00
equipment = None
2018-03-13 09:24:12 +01:00
session = None
2019-06-23 15:54:59 +02:00
form_class = RoundForm
2018-03-03 18:33:55 +01:00
2018-03-04 21:45:50 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
}
2018-03-04 21:45:50 +01:00
]
2018-03-03 18:33:55 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["title"] = "Commencer"
context["equipment"] = self.equipment
2018-03-03 18:33:55 +01:00
return context
def dispatch(self, request, *args, **kwargs):
2019-06-23 15:39:13 +02:00
self.equipment = get_object_or_404(
Equipment, pk=self.request.GET.get("equipment")
)
session_pk = self.request.session.get("session_pk")
2018-03-13 09:24:12 +01:00
if session_pk:
self.session = Session.objects.get(pk=session_pk)
2018-03-03 18:33:55 +01:00
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
2018-03-13 09:24:12 +01:00
theoretical_max_percentage = self.session.default_theoretical_max_percentage
2018-03-03 18:33:55 +01:00
initial = super().get_initial()
2019-06-23 15:39:13 +02:00
initial["equipment"] = self.equipment
initial["session"] = self.session
last_round = self.equipment.rounds.filter(
session=self.session
).last() # type: Round
initial["theoretical_max_percentage"] = theoretical_max_percentage
if last_round:
2019-06-23 15:39:13 +02:00
initial["repetition_number"] = last_round.repetition_number
initial["work_form"] = last_round.work_form
initial["chosen_weight"] = last_round.chosen_weight
else:
2019-06-23 15:39:13 +02:00
initial["repetition_number"] = (
self.equipment.default_repetition_number or 12
)
initial["work_form"] = self.equipment.default_work_form
proposed_weight = 0
theoretical_max = self.equipment.last_theoretical_max
if theoretical_max:
2019-06-23 15:39:13 +02:00
proposed_weight = (
theoretical_max.value * theoretical_max_percentage / 100
)
initial["chosen_weight"] = proposed_weight
2018-03-03 18:33:55 +01:00
return initial
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})
2018-03-13 09:06:28 +01:00
class RoundUpdateView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = Round
2019-06-23 15:39:13 +02:00
fields = [
"equipment",
"session",
"theoretical_max_percentage",
"chosen_weight",
"repetition_number",
"work_form",
"notes",
]
template_name = "gym/round_edit.html"
2018-03-13 09:06:28 +01:00
def get_quick_actions(self):
return [
{
2019-06-23 15:39:13 +02:00
"url": reverse("equipment-detail", args=(self.object.equipment.pk,)),
"category": "secondary",
"display": "Retourner à la machine",
}
2018-03-13 09:06:28 +01:00
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
context["edit"] = True
context["title"] = "Modifier"
context["equipment"] = self.object.equipment
2018-03-13 09:06:28 +01:00
return context
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})
2018-03-13 09:06:28 +01:00
class RoundDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Round
2019-06-23 15:39:13 +02:00
template_name = "gym/confirm_delete.html"
2018-03-13 09:06:28 +01:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-23 15:39:13 +02:00
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,
)
)
2018-03-13 09:06:28 +01:00
return context
def get_success_url(self):
2019-06-23 15:39:13 +02:00
return reverse("equipment-detail", kwargs={"pk": self.object.equipment.pk})