From 45d5879596fd7a3df9aba99f3e928976027248e6 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 4 Mar 2018 20:23:45 +0100 Subject: [PATCH] Extract duplicated code into a mixin module --- gym/mixins.py | 6 ++++++ gym/templates/gym/equipment.html | 2 +- gym/views.py | 25 ++++++------------------- 3 files changed, 13 insertions(+), 20 deletions(-) create mode 100644 gym/mixins.py diff --git a/gym/mixins.py b/gym/mixins.py new file mode 100644 index 0000000..99ade3b --- /dev/null +++ b/gym/mixins.py @@ -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) diff --git a/gym/templates/gym/equipment.html b/gym/templates/gym/equipment.html index 531c3e9..02d8d65 100644 --- a/gym/templates/gym/equipment.html +++ b/gym/templates/gym/equipment.html @@ -4,7 +4,7 @@ {% block content %}
-

{% block title %}{{ equipment.name }} ({{ equipment.room.name }}){% endblock %}

+

{% block title %}{{ equipment.name }} ({{ equipment.room.name }}){% endblock %}

diff --git a/gym/views.py b/gym/views.py index f315820..6d01275 100644 --- a/gym/views.py +++ b/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):