Extract duplicated code into a mixin module

This commit is contained in:
Gabriel Augendre 2018-03-04 20:23:45 +01:00
parent 214bbf4299
commit 45d5879596
No known key found for this signature in database
GPG key ID: F360212F958357D4
3 changed files with 13 additions and 20 deletions

6
gym/mixins.py Normal file
View 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)

View file

@ -4,7 +4,7 @@
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col-12"> <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> </div>
<div class="row"> <div class="row">

View file

@ -5,32 +5,21 @@ from django.shortcuts import get_object_or_404
from django.urls import reverse from django.urls import reverse
from django.views import generic from django.views import generic
from gym.mixins import SessionResetMixin
from gym.models import Room, Equipment, Setting, Session, Round, TheoreticalMax 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') queryset = Room.objects.all().order_by('name')
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_pk' in request.session:
del request.session['session_pk']
return super().dispatch(request, *args, **kwargs) class RoomDetailView(LoginRequiredMixin, SessionResetMixin, generic.DetailView):
class RoomDetailView(LoginRequiredMixin, 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_pk' 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')
@ -182,9 +171,7 @@ class SessionCreateView(LoginRequiredMixin, generic.CreateView):
return context return context
def get_success_url(self): def get_success_url(self):
session_pk = self.object.pk return reverse('session-detail', kwargs={'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'))
@ -203,9 +190,9 @@ class SessionDetailView(LoginRequiredMixin, 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): def dispatch(self, request, *args, **kwargs):
request.session['session_pk'] = self.get_object().pk 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): class RoundCreateView(LoginRequiredMixin, generic.CreateView):