Project

General

Profile

Download (1.92 KB) Statistics
| Branch: | Tag: | Revision:
e04509f6 Benjamin Dauvergne
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

5e0f0c15 Benjamin Dauvergne
from django.views.generic import list as list_cbv, edit, base # ListView
# from django.views.generic.edit import # CreateView, DeleteView, UpdateView
1097fd0a Benjamin Dauvergne
from django.shortcuts import get_object_or_404
from django.http import Http404
5e0f0c15 Benjamin Dauvergne
1097fd0a Benjamin Dauvergne
from calebasse.ressources.models import Service
5e0f0c15 Benjamin Dauvergne
class ServiceViewMixin(object):
1097fd0a Benjamin Dauvergne
service = None
date = None
popup = False

def dispatch(self, request, **kwargs):
self.popup = request.GET.get('popup')
f73e93e0 Benjamin Dauvergne
if 'service' in kwargs:
self.service = get_object_or_404(Service, slug=kwargs['service'])
if 'date' in kwargs:
1097fd0a Benjamin Dauvergne
try:
f73e93e0 Benjamin Dauvergne
self.date = datetime.strptime(kwargs.get('date'),
1097fd0a Benjamin Dauvergne
'%Y-%m-%d').date()
except (TypeError, ValueError):
raise Http404
return super(ServiceViewMixin, self).dispatch(request, **kwargs)

5e0f0c15 Benjamin Dauvergne
def get_context_data(self, **kwargs):
context = super(ServiceViewMixin, self).get_context_data(**kwargs)
1097fd0a Benjamin Dauvergne
context['popup'] = self.popup
if self.service is not None:
context['service'] = self.service.slug
context['service_name'] = self.service.name

if self.date is not None:
context['date'] = self.date
context['previous_day'] = self.date + relativedelta(days=-1)
context['next_day'] = self.date + relativedelta(days=1)
context['previous_month'] = self.date + relativedelta(months=-1)
context['next_month'] = self.date + relativedelta(months=1)
5e0f0c15 Benjamin Dauvergne
return context

class TemplateView(ServiceViewMixin, base.TemplateView):
pass

class ListView(ServiceViewMixin, list_cbv.ListView):
pass

class CreateView(ServiceViewMixin, edit.CreateView):
pass

class DeleteView(ServiceViewMixin, edit.DeleteView):
pass

class UpdateView(ServiceViewMixin, edit.UpdateView):
pass