Extract duplicated code into a mixin module
This commit is contained in:
parent
214bbf4299
commit
45d5879596
3 changed files with 13 additions and 20 deletions
6
gym/mixins.py
Normal file
6
gym/mixins.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
class SessionResetMixin:
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if 'session_pk' in request.session:
|
||||
del request.session['session_pk']
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
|
@ -4,7 +4,7 @@
|
|||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h1>{% block title %}{{ equipment.name }} ({{ equipment.room.name }}){% endblock %}</h1>
|
||||
<h1>{% block title %}{{ equipment.name }} <small class="text-muted">({{ equipment.room.name }})</small>{% endblock %}</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
|
25
gym/views.py
25
gym/views.py
|
@ -5,32 +5,21 @@ from django.shortcuts import get_object_or_404
|
|||
from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
from gym.mixins import SessionResetMixin
|
||||
from gym.models import Room, Equipment, Setting, Session, Round, TheoreticalMax
|
||||
|
||||
|
||||
class RoomListView(LoginRequiredMixin, generic.ListView):
|
||||
class RoomListView(LoginRequiredMixin, SessionResetMixin, generic.ListView):
|
||||
queryset = Room.objects.all().order_by('name')
|
||||
context_object_name = 'rooms'
|
||||
template_name = 'gym/rooms.html'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if 'session_pk' in request.session:
|
||||
del request.session['session_pk']
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class RoomDetailView(LoginRequiredMixin, generic.DetailView):
|
||||
class RoomDetailView(LoginRequiredMixin, SessionResetMixin, generic.DetailView):
|
||||
model = Room
|
||||
context_object_name = 'room'
|
||||
template_name = 'gym/room.html'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if 'session_pk' in request.session:
|
||||
del request.session['session_pk']
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['sessions'] = self.object.sessions.all().order_by('-start')
|
||||
|
@ -182,9 +171,7 @@ class SessionCreateView(LoginRequiredMixin, generic.CreateView):
|
|||
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})
|
||||
return reverse('session-detail', kwargs={'pk': self.object.pk})
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.room = get_object_or_404(Room, pk=self.request.GET.get('room'))
|
||||
|
@ -203,9 +190,9 @@ class SessionDetailView(LoginRequiredMixin, generic.DetailView):
|
|||
context_object_name = 'session'
|
||||
template_name = 'gym/session_detail.html'
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
request.session['session_pk'] = self.get_object().pk
|
||||
return super().get(request, *args, **kwargs)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class RoundCreateView(LoginRequiredMixin, generic.CreateView):
|
||||
|
|
Loading…
Reference in a new issue