calebasse/calebasse/statistics/views.py @ 243e22a9
| 330dad49 | Mikaël Ates | # -*- coding: utf-8 -*-
|
|
| 3bd3181d | Mikaël Ates | import urllib
|
|
| 010f4dd3 | Mikaël Ates | from datetime import datetime
|
|
from django.core.files import File
|
|||
from django.http import HttpResponse
|
|||
| 3bd3181d | Mikaël Ates | from calebasse.cbv import TemplateView, FormView
|
|
from calebasse.statistics import forms
|
|||
from statistics import STATISTICS, Statistic
|
|||
| 330dad49 | Mikaël Ates | ||
| 3bd3181d | Mikaël Ates | class StatisticsHomepageView(TemplateView):
|
|
| 330dad49 | Mikaël Ates | template_name = 'statistics/index.html'
|
|
def get_context_data(self, **kwargs):
|
|||
context = super(StatisticsHomepageView, self).get_context_data(**kwargs)
|
|||
| 3bd3181d | Mikaël Ates | statistics = dict()
|
|
for name, statistic in STATISTICS.iteritems():
|
|||
| 99459c02 | Mikaël Ates | if not statistic.get('services') or \
|
|
self.service.name in statistic.get('services'):
|
|||
statistics.setdefault(statistic['category'], []).append((name,
|
|||
statistic))
|
|||
| 3bd3181d | Mikaël Ates | context['statistics'] = statistics
|
|
return context
|
|||
class StatisticsDetailView(TemplateView):
|
|||
template_name = 'statistics/detail.html'
|
|||
def get_context_data(self, **kwargs):
|
|||
context = super(StatisticsDetailView, self).get_context_data(**kwargs)
|
|||
name = kwargs.get('name')
|
|||
inputs = dict()
|
|||
inputs['service'] = self.service
|
|||
inputs['start_date'] = self.request.GET.get('start_date')
|
|||
inputs['end_date'] = self.request.GET.get('end_date')
|
|||
inputs['participants'] = self.request.GET.get('participants')
|
|||
inputs['patients'] = self.request.GET.get('patients')
|
|||
| 5d0556a0 | Mikaël Ates | inputs['inscriptions'] = self.request.GET.get('inscriptions')
|
|
inputs['no_synthesis'] = self.request.GET.get('no_synthesis')
|
|||
| 3bd3181d | Mikaël Ates | statistic = Statistic(name, inputs)
|
|
context['dn'] = statistic.display_name
|
|||
| 8970af36 | Mikaël Ates | context['data_tables_set'] = statistic.get_data()
|
|
| 330dad49 | Mikaël Ates | return context
|
|
| 3bd3181d | Mikaël Ates | ||
class StatisticsFormView(FormView):
|
|||
template_name = 'statistics/form.html'
|
|||
| 093dbd3c | Mikaël Ates | def dispatch(self, request, **kwargs):
|
|
self.name = kwargs.get('name')
|
|||
return super(StatisticsFormView, self).dispatch(request, **kwargs)
|
|||
def get_form_class(self):
|
|||
if self.name == 'annual_activity':
|
|||
return forms.AnnualActivityForm
|
|||
| 9a85b134 | Mikaël Ates | elif self.name == 'patients_synthesis':
|
|
return forms.PatientsSynthesisForm
|
|||
| cb424fd8 | Mikaël Ates | elif self.name == 'patients_details':
|
|
return forms.PatientsTwoDatesForm
|
|||
| b55bb03e | Mikaël Ates | elif self.name in ('active_patients_by_state_only',
|
|
| 42d5f295 | Mikaël Ates | 'patients_protection', 'patients_contact'):
|
|
| cb424fd8 | Mikaël Ates | return forms.OneDateForm
|
|
| 5d0556a0 | Mikaël Ates | elif self.name == 'patients_per_worker_for_period':
|
|
return forms.PatientsPerWorkerForPeriodForm
|
|||
| cb424fd8 | Mikaël Ates | elif self.name in ('active_patients_with_act', 'closed_files',
|
|
| 36850a88 | Mikaël Ates | 'patients_synthesis', 'acts_synthesis',
|
|
| 966a24cf | Mikaël Ates | 'acts_synthesis_cmpp', 'mises', 'deficiencies'):
|
|
| cb424fd8 | Mikaël Ates | return forms.TwoDatesForm
|
|
| 2c033fb5 | Mikaël Ates | else:
|
|
| cb424fd8 | Mikaël Ates | return forms.ParticipantsPatientsTwoDatesForm
|
|
| 2c033fb5 | Mikaël Ates | ||
| 010f4dd3 | Mikaël Ates | def form_valid(self, form):
|
|
if 'display_or_export' in form.data:
|
|||
inputs = dict()
|
|||
inputs['service'] = self.service
|
|||
inputs['start_date'] = form.data.get('start_date')
|
|||
inputs['end_date'] = form.data.get('end_date')
|
|||
inputs['participants'] = form.data.get('participants')
|
|||
inputs['patients'] = form.data.get('patients')
|
|||
| 5d0556a0 | Mikaël Ates | inputs['inscriptions'] = form.data.get('inscriptions')
|
|
inputs['no_synthesis'] = 'no_synthesis' in form.data
|
|||
| 093dbd3c | Mikaël Ates | statistic = Statistic(self.name, inputs)
|
|
| 010f4dd3 | Mikaël Ates | path = statistic.get_file()
|
|
content = File(file(path))
|
|||
response = HttpResponse(content, 'text/csv')
|
|||
response['Content-Length'] = content.size
|
|||
dest_filename = "%s--%s.csv" \
|
|||
% (datetime.now().strftime('%Y-%m-%d--%H:%M:%S'),
|
|||
statistic.display_name)
|
|||
response['Content-Disposition'] = \
|
|||
'attachment; filename="%s"' % dest_filename
|
|||
return response
|
|||
return super(StatisticsFormView, self).form_valid(form)
|
|||
| 3bd3181d | Mikaël Ates | def get_success_url(self):
|
|
qs = urllib.urlencode(self.request.POST)
|
|||
target = '../../detail/%s?%s' % (self.kwargs.get('name'), qs)
|
|||
return target
|