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
|
statistics.setdefault(statistic['category'], []).append((name,
|
23
|
statistic['display_name']))
|
24
|
context['statistics'] = statistics
|
25
|
return context
|
26
|
|
27
|
|
28
|
class StatisticsDetailView(TemplateView):
|
29
|
template_name = 'statistics/detail.html'
|
30
|
|
31
|
def get_context_data(self, **kwargs):
|
32
|
context = super(StatisticsDetailView, self).get_context_data(**kwargs)
|
33
|
name = kwargs.get('name')
|
34
|
inputs = dict()
|
35
|
inputs['service'] = self.service
|
36
|
inputs['start_date'] = self.request.GET.get('start_date')
|
37
|
inputs['end_date'] = self.request.GET.get('end_date')
|
38
|
inputs['participants'] = self.request.GET.get('participants')
|
39
|
inputs['patients'] = self.request.GET.get('patients')
|
40
|
statistic = Statistic(name, inputs)
|
41
|
context['dn'] = statistic.display_name
|
42
|
context['data_tables'] = statistic.get_data()
|
43
|
return context
|
44
|
|
45
|
|
46
|
class StatisticsFormView(FormView):
|
47
|
form_class = forms.StatForm
|
48
|
template_name = 'statistics/form.html'
|
49
|
success_url = '..'
|
50
|
|
51
|
def form_valid(self, form):
|
52
|
if 'display_or_export' in form.data:
|
53
|
name = self.kwargs.get('name')
|
54
|
inputs = dict()
|
55
|
inputs['service'] = self.service
|
56
|
inputs['start_date'] = form.data.get('start_date')
|
57
|
inputs['end_date'] = form.data.get('end_date')
|
58
|
inputs['participants'] = form.data.get('participants')
|
59
|
inputs['patients'] = form.data.get('patients')
|
60
|
statistic = Statistic(name, inputs)
|
61
|
path = statistic.get_file()
|
62
|
content = File(file(path))
|
63
|
response = HttpResponse(content, 'text/csv')
|
64
|
response['Content-Length'] = content.size
|
65
|
dest_filename = "%s--%s.csv" \
|
66
|
% (datetime.now().strftime('%Y-%m-%d--%H:%M:%S'),
|
67
|
statistic.display_name)
|
68
|
response['Content-Disposition'] = \
|
69
|
'attachment; filename="%s"' % dest_filename
|
70
|
return response
|
71
|
return super(StatisticsFormView, self).form_valid(form)
|
72
|
|
73
|
def get_success_url(self):
|
74
|
qs = urllib.urlencode(self.request.POST)
|
75
|
target = '../../detail/%s?%s' % (self.kwargs.get('name'), qs)
|
76
|
return target
|