|
1
|
# -*- coding: utf-8 -*-
|
|
2
|
import urllib
|
|
3
|
|
|
4
|
from datetime import datetime
|
|
5
|
|
|
6
|
from django.core.files import File
|
|
7
|
from django.http import HttpResponse
|
|
8
|
|
|
9
|
from calebasse.cbv import TemplateView, FormView
|
|
10
|
from calebasse.statistics import forms
|
|
11
|
|
|
12
|
from statistics import STATISTICS, Statistic
|
|
13
|
|
|
14
|
|
|
15
|
class StatisticsHomepageView(TemplateView):
|
|
16
|
template_name = 'statistics/index.html'
|
|
17
|
|
|
18
|
def get_context_data(self, **kwargs):
|
|
19
|
context = super(StatisticsHomepageView, self).get_context_data(**kwargs)
|
|
20
|
statistics = dict()
|
|
21
|
for name, statistic in STATISTICS.iteritems():
|
|
22
|
if not statistic.get('services') or \
|
|
23
|
self.service.name in statistic.get('services'):
|
|
24
|
statistics.setdefault(statistic['category'], []).append((name,
|
|
25
|
statistic))
|
|
26
|
context['statistics'] = statistics
|
|
27
|
return context
|
|
28
|
|
|
29
|
|
|
30
|
class StatisticsDetailView(TemplateView):
|
|
31
|
template_name = 'statistics/detail.html'
|
|
32
|
|
|
33
|
def get_context_data(self, **kwargs):
|
|
34
|
context = super(StatisticsDetailView, self).get_context_data(**kwargs)
|
|
35
|
name = kwargs.get('name')
|
|
36
|
inputs = dict()
|
|
37
|
inputs['service'] = self.service
|
|
38
|
inputs['start_date'] = self.request.GET.get('start_date')
|
|
39
|
inputs['end_date'] = self.request.GET.get('end_date')
|
|
40
|
inputs['participants'] = self.request.GET.get('participants')
|
|
41
|
inputs['patients'] = self.request.GET.get('patients')
|
|
42
|
statistic = Statistic(name, inputs)
|
|
43
|
context['dn'] = statistic.display_name
|
|
44
|
context['data_tables_set'] = statistic.get_data()
|
|
45
|
return context
|
|
46
|
|
|
47
|
|
|
48
|
class StatisticsFormView(FormView):
|
|
49
|
template_name = 'statistics/form.html'
|
|
50
|
|
|
51
|
def dispatch(self, request, **kwargs):
|
|
52
|
self.name = kwargs.get('name')
|
|
53
|
return super(StatisticsFormView, self).dispatch(request, **kwargs)
|
|
54
|
|
|
55
|
def get_form_class(self):
|
|
56
|
if self.name == 'annual_activity':
|
|
57
|
return forms.AnnualActivityForm
|
|
58
|
elif self.name == 'patients_details':
|
|
59
|
return forms.PatientsTwoDatesForm
|
|
60
|
elif self.name in ('active_patients_by_state_only',
|
|
61
|
'patients_protection'):
|
|
62
|
return forms.OneDateForm
|
|
63
|
elif self.name in ('active_patients_with_act', 'closed_files',
|
|
64
|
'patients_synthesis', 'acts_synthesis',
|
|
65
|
'acts_synthesis_cmpp', 'mises', 'deficiencies'):
|
|
66
|
return forms.TwoDatesForm
|
|
67
|
else:
|
|
68
|
return forms.ParticipantsPatientsTwoDatesForm
|
|
69
|
|
|
70
|
def form_valid(self, form):
|
|
71
|
if 'display_or_export' in form.data:
|
|
72
|
inputs = dict()
|
|
73
|
inputs['service'] = self.service
|
|
74
|
inputs['start_date'] = form.data.get('start_date')
|
|
75
|
inputs['end_date'] = form.data.get('end_date')
|
|
76
|
inputs['participants'] = form.data.get('participants')
|
|
77
|
inputs['patients'] = form.data.get('patients')
|
|
78
|
statistic = Statistic(self.name, inputs)
|
|
79
|
path = statistic.get_file()
|
|
80
|
content = File(file(path))
|
|
81
|
response = HttpResponse(content, 'text/csv')
|
|
82
|
response['Content-Length'] = content.size
|
|
83
|
dest_filename = "%s--%s.csv" \
|
|
84
|
% (datetime.now().strftime('%Y-%m-%d--%H:%M:%S'),
|
|
85
|
statistic.display_name)
|
|
86
|
response['Content-Disposition'] = \
|
|
87
|
'attachment; filename="%s"' % dest_filename
|
|
88
|
return response
|
|
89
|
return super(StatisticsFormView, self).form_valid(form)
|
|
90
|
|
|
91
|
def get_success_url(self):
|
|
92
|
qs = urllib.urlencode(self.request.POST)
|
|
93
|
target = '../../detail/%s?%s' % (self.kwargs.get('name'), qs)
|
|
94
|
return target
|