1
|
from datetime import datetime, timedelta
|
2
|
from dateutil.relativedelta import relativedelta
|
3
|
|
4
|
from django.views.generic import list as list_cbv, edit, base # ListView
|
5
|
# from django.views.generic.edit import # CreateView, DeleteView, UpdateView
|
6
|
from django.shortcuts import get_object_or_404
|
7
|
from django.http import Http404
|
8
|
|
9
|
from calebasse.ressources.models import Service
|
10
|
|
11
|
class ServiceViewMixin(object):
|
12
|
service = None
|
13
|
date = None
|
14
|
popup = False
|
15
|
|
16
|
def dispatch(self, request, **kwargs):
|
17
|
self.popup = request.GET.get('popup')
|
18
|
if 'service' in kwargs:
|
19
|
self.service = get_object_or_404(Service, slug=kwargs['service'])
|
20
|
if 'date' in kwargs:
|
21
|
try:
|
22
|
self.date = datetime.strptime(kwargs.get('date'),
|
23
|
'%Y-%m-%d').date()
|
24
|
except (TypeError, ValueError):
|
25
|
raise Http404
|
26
|
return super(ServiceViewMixin, self).dispatch(request, **kwargs)
|
27
|
|
28
|
def get_context_data(self, **kwargs):
|
29
|
context = super(ServiceViewMixin, self).get_context_data(**kwargs)
|
30
|
context['popup'] = self.popup
|
31
|
if self.service is not None:
|
32
|
context['service'] = self.service.slug
|
33
|
context['service_name'] = self.service.name
|
34
|
|
35
|
if self.date is not None:
|
36
|
context['date'] = self.date
|
37
|
context['previous_day'] = self.date + relativedelta(days=-1)
|
38
|
context['next_day'] = self.date + relativedelta(days=1)
|
39
|
context['previous_month'] = self.date + relativedelta(months=-1)
|
40
|
context['next_month'] = self.date + relativedelta(months=1)
|
41
|
return context
|
42
|
|
43
|
class TemplateView(ServiceViewMixin, base.TemplateView):
|
44
|
pass
|
45
|
|
46
|
class ListView(ServiceViewMixin, list_cbv.ListView):
|
47
|
pass
|
48
|
|
49
|
class CreateView(ServiceViewMixin, edit.CreateView):
|
50
|
pass
|
51
|
|
52
|
class DeleteView(ServiceViewMixin, edit.DeleteView):
|
53
|
pass
|
54
|
|
55
|
class UpdateView(ServiceViewMixin, edit.UpdateView):
|
56
|
pass
|