Project

General

Profile

Download (4.07 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / statistics / views.py @ adfb9ea4

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
        inputs['inscriptions'] = self.request.GET.get('inscriptions')
43
        inputs['no_synthesis'] = self.request.GET.get('no_synthesis')
44
        statistic = Statistic(name, inputs)
45
        context['dn'] = statistic.display_name
46
        context['data_tables_set'] = statistic.get_data()
47
        return context
48

    
49

    
50
class StatisticsFormView(FormView):
51
    template_name = 'statistics/form.html'
52

    
53
    def dispatch(self, request, **kwargs):
54
        self.name = kwargs.get('name')
55
        return super(StatisticsFormView, self).dispatch(request, **kwargs)
56

    
57
    def get_form_class(self):
58
        if self.name == 'annual_activity':
59
            return forms.AnnualActivityForm
60
        elif self.name == 'patients_synthesis':
61
            return forms.PatientsSynthesisForm
62
        elif self.name == 'patients_details':
63
            return forms.PatientsTwoDatesForm
64
        elif self.name in ('active_patients_by_state_only',
65
                'patients_protection', 'patients_contact'):
66
            return forms.OneDateForm
67
        elif self.name == 'patients_per_worker_for_period':
68
            return forms.PatientsPerWorkerForPeriodForm
69
        elif self.name in ('active_patients_with_act', 'closed_files',
70
                'patients_synthesis', 'acts_synthesis',
71
                'acts_synthesis_cmpp', 'mises', 'deficiencies'):
72
            return forms.TwoDatesForm
73
        else:
74
            return forms.ParticipantsPatientsTwoDatesForm
75

    
76
    def form_valid(self, form):
77
        if 'display_or_export' in form.data:
78
            inputs = dict()
79
            inputs['service'] = self.service
80
            inputs['start_date'] = form.data.get('start_date')
81
            inputs['end_date'] = form.data.get('end_date')
82
            inputs['participants'] = form.data.get('participants')
83
            inputs['patients'] = form.data.get('patients')
84
            inputs['inscriptions'] = form.data.get('inscriptions')
85
            inputs['no_synthesis'] = 'no_synthesis' in form.data
86
            statistic = Statistic(self.name, inputs)
87
            path = statistic.get_file()
88
            content = File(file(path))
89
            response = HttpResponse(content, 'text/csv')
90
            response['Content-Length'] = content.size
91
            dest_filename = "%s--%s.csv" \
92
                % (datetime.now().strftime('%Y-%m-%d--%H:%M:%S'),
93
                statistic.display_name)
94
            response['Content-Disposition'] = \
95
                'attachment; filename="%s"' % dest_filename
96
            return response
97
        return super(StatisticsFormView, self).form_valid(form)
98

    
99
    def get_success_url(self):
100
        qs = urllib.urlencode(self.request.POST)
101
        target = '../../detail/%s?%s' % (self.kwargs.get('name'), qs)
102
        return target
(8-8/8)