Use session to store session_pk instead of passing it with url params

This commit is contained in:
Gabriel Augendre 2018-03-04 09:48:51 +01:00
parent f9ba5b9444
commit 936a00bf68
No known key found for this signature in database
GPG key ID: F360212F958357D4
6 changed files with 36 additions and 55 deletions

View file

@ -16,12 +16,12 @@
<a href="{% url 'room-detail' equipment.room.pk %}" <a href="{% url 'room-detail' equipment.room.pk %}"
class="btn btn-secondary">Retourner à la salle</a> class="btn btn-secondary">Retourner à la salle</a>
{% endif %} {% endif %}
<a href="{% url 'setting-create' %}?equipment={{ equipment.pk }}{% if session %}&session={{ session }}{% endif %}" <a href="{% url 'setting-create' %}?equipment={{ equipment.pk }}"
class="btn btn-success">Ajouter un réglage</a> class="btn btn-success">Ajouter un réglage</a>
<a href="{% url 'theoretical-max-create' %}?equipment={{ equipment.pk }}{% if session %}&session={{ session }}{% endif %}" <a href="{% url 'theoretical-max-create' %}?equipment={{ equipment.pk }}"
class="btn btn-success">Ajouter un max théorique</a> class="btn btn-success">Ajouter un max théorique</a>
{% if session %} {% if session %}
<a href="{% url 'round-create' %}?equipment={{ equipment.pk }}&session={{ session }}" <a href="{% url 'round-create' %}?equipment={{ equipment.pk }}"
class="btn btn-primary">Commencer une série</a> class="btn btn-primary">Commencer une série</a>
{% endif %} {% endif %}
</div> </div>
@ -46,7 +46,7 @@
{% if equipment.settings.all %} {% if equipment.settings.all %}
<ul class="list-group"> <ul class="list-group">
{% for setting in equipment.settings.all %} {% for setting in equipment.settings.all %}
<a href="{% url 'setting-edit' setting.pk %}?session={{ session }}" <a href="{% url 'setting-edit' setting.pk %}"
class="list-group-item">{{ setting.name }} class="list-group-item">{{ setting.name }}
: {{ setting.value }}</a> : {{ setting.value }}</a>
{% endfor %} {% endfor %}

View file

@ -12,7 +12,7 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
{% if equipment %} {% if equipment %}
<a href="{% url 'equipment-detail' equipment.pk %}?session={{ session.pk }}" class="btn btn-secondary">Retourner à la machine</a> <a href="{% url 'equipment-detail' equipment.pk %}" class="btn btn-secondary">Retourner à la machine</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -10,7 +10,7 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<a href="{% url 'room-detail' session.room.pk %}" class="btn btn-secondary">Retourner à la salle</a> <a href="{% url 'room-detail' session.room.pk %}" class="btn btn-secondary">Retourner à la salle</a>
<a href="{% url 'equipment-create' %}?room={{ session.room.pk }}&session={{ session.pk }}" class="btn btn-success">Ajouter une machine</a> <a href="{% url 'equipment-create' %}?room={{ session.room.pk }}" class="btn btn-success">Ajouter une machine</a>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
@ -34,7 +34,7 @@
<h2>Machines</h2> <h2>Machines</h2>
<ul class="list-group"> <ul class="list-group">
{% for equipment in session.room.equipments.all %} {% for equipment in session.room.equipments.all %}
<a href="{% url 'equipment-detail' equipment.pk %}?session={{ session.pk }}" class="list-group-item">{{ equipment.name }}</a> <a href="{% url 'equipment-detail' equipment.pk %}" class="list-group-item">{{ equipment.name }}</a>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View file

@ -10,7 +10,7 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
{% if equipment %} {% if equipment %}
<a href="{% url 'equipment-detail' equipment.pk %}{% if session %}?session={{ session }}{% endif %}" class="btn btn-secondary">Retourner à la machine</a> <a href="{% url 'equipment-detail' equipment.pk %}" class="btn btn-secondary">Retourner à la machine</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -10,7 +10,7 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
{% if equipment %} {% if equipment %}
<a href="{% url 'equipment-detail' equipment.pk %}{% if session %}?session={{ session }}{% endif %}" class="btn btn-secondary">Retourner à la machine</a> <a href="{% url 'equipment-detail' equipment.pk %}" class="btn btn-secondary">Retourner à la machine</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -12,12 +12,24 @@ class RoomListView(generic.ListView):
context_object_name = 'rooms' context_object_name = 'rooms'
template_name = 'gym/rooms.html' template_name = 'gym/rooms.html'
def dispatch(self, request, *args, **kwargs):
if 'session' in request.session:
del request.session['session_pk']
return super().dispatch(request, *args, **kwargs)
class RoomDetailView(generic.DetailView): class RoomDetailView(generic.DetailView):
model = Room model = Room
context_object_name = 'room' context_object_name = 'room'
template_name = 'gym/room.html' template_name = 'gym/room.html'
def dispatch(self, request, *args, **kwargs):
if 'session' in request.session:
del request.session['session_pk']
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['sessions'] = self.object.sessions.all().order_by('-start') context['sessions'] = self.object.sessions.all().order_by('-start')
@ -28,16 +40,10 @@ class EquipmentDetailView(generic.DetailView):
model = Equipment model = Equipment
context_object_name = 'equipment' context_object_name = 'equipment'
template_name = 'gym/equipment.html' template_name = 'gym/equipment.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): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['session'] = self.session context['session'] = self.request.session.get('session_pk')
return context return context
@ -46,7 +52,6 @@ class EquipmentCreateView(generic.CreateView):
fields = ['room', 'name'] fields = ['room', 'name']
template_name = 'gym/equipment_edit.html' template_name = 'gym/equipment_edit.html'
room = None room = None
session = None
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
@ -54,18 +59,13 @@ class EquipmentCreateView(generic.CreateView):
return context return context
def get_success_url(self): def get_success_url(self):
url = reverse('equipment-detail', kwargs={'pk': self.object.pk}) return 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): def dispatch(self, request, *args, **kwargs):
default_room_id = self.request.GET.get('room') default_room_id = self.request.GET.get('room')
if default_room_id: if default_room_id:
self.room = get_object_or_404(Room, pk=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) return super().dispatch(request, *args, **kwargs)
def get_initial(self): def get_initial(self):
@ -80,13 +80,11 @@ class SettingCreateView(generic.CreateView):
fields = ['equipment', 'name', 'value'] fields = ['equipment', 'name', 'value']
template_name = 'gym/setting_edit.html' template_name = 'gym/setting_edit.html'
equipment = None equipment = None
session = None
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = 'Ajouter' context['title'] = 'Ajouter'
context['equipment'] = self.equipment context['equipment'] = self.equipment
context['session'] = self.session
return context return context
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
@ -94,8 +92,6 @@ class SettingCreateView(generic.CreateView):
if default_equipment_id: if default_equipment_id:
self.equipment = get_object_or_404(Equipment, pk=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) return super().dispatch(request, *args, **kwargs)
def get_initial(self): def get_initial(self):
@ -105,36 +101,23 @@ class SettingCreateView(generic.CreateView):
return initial return initial
def get_success_url(self): def get_success_url(self):
url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
if self.session:
return '{}?session={}'.format(url, self.session)
return url
class SettingUpdateView(generic.UpdateView): class SettingUpdateView(generic.UpdateView):
model = Setting model = Setting
fields = ['equipment', 'name', 'value'] fields = ['equipment', 'name', 'value']
template_name = 'gym/setting_edit.html' 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): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['edit'] = True context['edit'] = True
context['title'] = 'Modifier' context['title'] = 'Modifier'
context['equipment'] = self.object.equipment context['equipment'] = self.object.equipment
context['session'] = self.request.GET.get('session')
return context return context
def get_success_url(self): def get_success_url(self):
url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
if self.session:
return '{}?session={}'.format(url, self.session)
return url
class SettingDeleteView(generic.DeleteView): class SettingDeleteView(generic.DeleteView):
@ -151,13 +134,11 @@ class TheoreticalMaxCreateView(generic.CreateView):
fields = ['equipment', 'date', 'value'] fields = ['equipment', 'date', 'value']
template_name = 'gym/theoretical_max_edit.html' template_name = 'gym/theoretical_max_edit.html'
equipment = None equipment = None
session = None
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = 'Ajouter' context['title'] = 'Ajouter'
context['equipment'] = self.equipment context['equipment'] = self.equipment
context['session'] = self.session
return context return context
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
@ -165,8 +146,6 @@ class TheoreticalMaxCreateView(generic.CreateView):
if default_equipment_id: if default_equipment_id:
self.equipment = get_object_or_404(Equipment, pk=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) return super().dispatch(request, *args, **kwargs)
def get_initial(self): def get_initial(self):
@ -177,10 +156,7 @@ class TheoreticalMaxCreateView(generic.CreateView):
return initial return initial
def get_success_url(self): def get_success_url(self):
url = reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk})
if self.session:
return '{}?session={}'.format(url, self.session)
return url
class SessionCreateView(generic.CreateView): class SessionCreateView(generic.CreateView):
@ -196,7 +172,9 @@ class SessionCreateView(generic.CreateView):
return context return context
def get_success_url(self): def get_success_url(self):
return reverse('session-detail', kwargs={'pk': self.object.pk}) session_pk = self.object.pk
self.request.session['session_pk'] = session_pk
return reverse('session-detail', kwargs={'pk': session_pk})
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.room = get_object_or_404(Room, pk=self.request.GET.get('room')) self.room = get_object_or_404(Room, pk=self.request.GET.get('room'))
@ -215,24 +193,27 @@ class SessionDetailView(generic.DetailView):
context_object_name = 'session' context_object_name = 'session'
template_name = 'gym/session_detail.html' template_name = 'gym/session_detail.html'
def get(self, request, *args, **kwargs):
request.session['session_pk'] = self.get_object().pk
return super().get(request, *args, **kwargs)
class RoundCreateView(generic.CreateView): class RoundCreateView(generic.CreateView):
model = Round model = Round
fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes'] fields = ['equipment', 'session', 'theoretical_max_percentage', 'chosen_weight', 'repetition_number', 'notes']
template_name = 'gym/round_edit.html' template_name = 'gym/round_edit.html'
equipment = None equipment = None
session = None session_pk = None
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = 'Commencer' context['title'] = 'Commencer'
context['equipment'] = self.equipment context['equipment'] = self.equipment
context['session'] = self.session
return context return context
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.equipment = get_object_or_404(Equipment, pk=self.request.GET.get('equipment')) self.equipment = get_object_or_404(Equipment, pk=self.request.GET.get('equipment'))
self.session = get_object_or_404(Equipment, pk=self.request.GET.get('session')) self.session_pk = self.request.session.get('session_pk')
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
@ -240,7 +221,7 @@ class RoundCreateView(generic.CreateView):
theoretical_max_percentage = 65 theoretical_max_percentage = 65
initial = super().get_initial() initial = super().get_initial()
initial['equipment'] = self.equipment initial['equipment'] = self.equipment
initial['session'] = self.session initial['session'] = self.session_pk
initial['repetition_number'] = 3 initial['repetition_number'] = 3
initial['theoretical_max_percentage'] = theoretical_max_percentage initial['theoretical_max_percentage'] = theoretical_max_percentage
proposed_weight = 0 proposed_weight = 0