/
admin_views.py
276 lines (258 loc) · 11.6 KB
/
admin_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
# Work around made due the fact production version has an outdated version of
# tempfile module
import tempfile2 as tempfile
import tarfile
from datetime import datetime, timedelta
import time
from django import forms
from django.contrib import admin
from django.contrib.admin.util import model_ngettext
from django.contrib.admin.views.decorators import staff_member_required
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import connection
from django.db.models import Count
from django.forms import ModelForm
from django.http import HttpResponse,HttpResponseNotFound, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils import simplejson
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext_lazy, ugettext as _
from forms.models import Formulario, tipoFormulario, UnidadeSaude, Ficha
import settings
class FormularioForm(forms.Form):
tipo = forms.ChoiceField()
arquivo = forms.FileField(max_length=300)
descricao = forms.CharField(widget=forms.Textarea())
permitir_insercao_multipla = forms.BooleanField (required=False)
def __init__(self, *args, **kwargs):
super(FormularioForm,self).__init__(*args,**kwargs)
self.fields['tipo'].choices = self.get_type_options()
self.fields['permitir_insercao_multipla'].verbose_name = u"Permitir a inserção de múltiplas entradas desse formulário"
def get_type_options(self):
import_forms = 'from forms.models import *'
exec import_forms
tp_list = tipoFormulario.objects.all()
types = [[type.nome, type.nome] for type in tp_list]
types.reverse()
types.append(['', '--------'])
types.reverse()
return types
class FormularioEditForm(ModelForm):
class Meta:
model = Formulario
exclude = ('nome', 'version','path', 'data_insercao')
class UploadError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def handle_uploaded_file(f):
destination = tempfile.NamedTemporaryFile(suffix='.tar.gz', delete=False)
for chunk in f.chunks():
destination.write(chunk)
destination.seek(0)
destination.close()
tar = tarfile.open(destination.name)
final_path = "%s/%s"%( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'modules'), '.'.join(f.name.split('.')[0:-2]))
dirname, filename = os.path.split(final_path)
prefix, suffix = os.path.splitext(filename)
final_path = tempfile.mkdtemp(suffix, prefix+'_', dirname)
tar.extractall(path=final_path)
tar.close()
os.remove(destination.name)
return final_path
class meta_data:
def __init__(self, v, v_plural=None):
self.verbose_name = v
if not v_plural:
v_plural = v + 's'
self.verbose_name_plural = v_plural
def add_formulario(request, app_label='Forms' ):
if not request.user.is_authenticated():
return HttpResponseRedirect('/admin/')
opts = meta_data(u'Formulário')
if request.method == 'POST':
form = FormularioForm(request.POST, request.FILES)
if form.is_valid():
path = handle_uploaded_file(request.FILES['arquivo'])
pathname, moduleFormName = os.path.split(path)
pathname ='%s/'%(pathname,)
if not pathname in sys.path:
sys.path.append(pathname)
moduleForm = __import__(moduleFormName)
#try:
# moduleForm = __import__(moduleFormName)
#except ImportError:
# return HttpResponseRedirect(settings.SITE_ROOT + '/admin/forms/formulario/')
tipoForm = tipoFormulario.objects.get(nome=form.cleaned_data['tipo'])
tp =form.cleaned_data['permitir_insercao_multipla']
if tp:
tp = True
else:
tp = False
newForm = Formulario(
nome=moduleForm.name,
version=moduleForm.version,
path=path,
tipo_id=tipoForm.id,
descricao=form.cleaned_data['descricao'],
permitir_insercao_multipla = tp
)
newForm.save()
return HttpResponseRedirect(settings.SITE_ROOT + 'admin/forms/formulario/')
else:
form = FormularioForm(auto_id=True)
add = True
return render_to_response('change_form.html',
locals(), RequestContext(request, {}))
def edit_formulario(request, f_id, app_label='Forms'):
if not request.user.is_authenticated():
return HttpResponseRedirect(settings.SITE_ROOT + 'admin/')
f = Formulario.objects.get(id=int(f_id))
if request.method == 'POST':
form = FormularioEditForm(request.POST, instance=f)
if form.is_valid():
tipoForm = tipoFormulario.objects.filter(nome=form.cleaned_data['tipo'])
f.tipo = tipoForm[0]
f.descricao = form.cleaned_data['descricao']
f.save()
return HttpResponseRedirect(settings.SITE_ROOT +'admin/forms/formulario/')
else:
form = FormularioEditForm(instance=f)
opts = meta_data(u'Formulário')
original = form.Meta().model().__str__()
return render_to_response('change_form.html',
locals(), RequestContext(request, {}))
def getMonthList(now, stDate, endDate):
dateRange =[now - timedelta(days=stDate), now-timedelta(days=endDate)]
dt1, dt2 = dateRange
start_month=dt1.month
end_months=(dt2.year-dt1.year)*12 + dt2.month+1
return [datetime(year=yr, month=mn, day=1) for (yr, mn) in (
((m - 1) / 12 + dt1.year, (m - 1) % 12 + 1) for m in range(start_month, end_months)
)]
def log_unidadesaude(request, stDate=365, endDate=0):
if not request.user.is_authenticated():
return HttpResponseRedirect(settings.SITE_ROOT + 'admin/')
"""
Summary report
"""
now= datetime.now()
truncate_date = connection.ops.date_trunc_sql('month', 'data_insercao')
fichas_report = Ficha.objects.filter(
data_insercao__gte=now -timedelta(days=stDate),
data_insercao__lte=now -timedelta(days=endDate)
).\
extra(select={'month': truncate_date}).\
values('unidadesaude__id', 'unidadesaude__nome', 'month').\
annotate(numero_fichas=Count('pk')).\
order_by('-month')
fichas_report = [ dict([
('unidadesaude__nome', l['unidadesaude__nome']),
('unidadesaude__id', l['unidadesaude__id']),
('month', datetime.strptime(l['month'].split(' ')[0], '%Y-%m-%d')),
('numero_fichas', l['numero_fichas'])
]) for l in fichas_report]
columns = getMonthList(now, stDate, endDate)
rows = UnidadeSaude.objects.values('nome', 'id').order_by('nome')
#Fichas Total per US
column_us = [f.nome for f in Formulario.objects.all()]
query_fichas_us_report = Ficha.objects.values('unidadesaude__nome', 'formulario__nome').\
annotate(numero_fichas=Count('pk'))
fichas_us_report = SortedDict().fromkeys([us.nome for us in UnidadeSaude.objects.all()])
for k in fichas_us_report.keys():
fichas_us_report[k]= SortedDict().fromkeys(column_us, 0)
for row in query_fichas_us_report:
fichas_us_report[row['unidadesaude__nome']][row['formulario__nome']] = row['numero_fichas']
#Count projeto Baseline
column_us_baseline = [u"Triagem Baseline Hospital",
u"Triagem Baseline Ambulatório",
u"Consulta Baseline",
u"FollowUp",
u"Exames"]
pacients_baseline = [f.paciente_id for f in Ficha.objects.\
filter(formulario__tipo__nome='Triagem').\
filter(formulario__nome__icontains=u'baseline').only('paciente')]
fichas_projeto_baseline_report = SortedDict().fromkeys([us.nome for us in UnidadeSaude.objects.all()])
for k in fichas_projeto_baseline_report.keys():
fichas_projeto_baseline_report[k]= SortedDict().fromkeys(column_us_baseline, 0)
num = 999
for sub_pacients in [pacients_baseline[i::num] for i in range(num)]:
query_fichas_projeto_baseline_report = Ficha.objects.\
filter(formulario__nome__in= column_us_baseline).\
filter(paciente__in=sub_pacients).\
values('unidadesaude__nome', 'formulario__nome').\
annotate(numero_fichas=Count('pk'))
for row in query_fichas_projeto_baseline_report:
fichas_projeto_baseline_report[row['unidadesaude__nome']][row['formulario__nome']] = \
fichas_projeto_baseline_report[row['unidadesaude__nome']][row['formulario__nome']] +row['numero_fichas']
#Count projeto Implementacao
column_us_implementacao = [u"Triagem Implementação Hospital",
u"Triagem Implementação Ambulatório",
u"Consulta Implementação",
u"FollowUp",
u"Exames"]
pacients_implementacao = [f.paciente_id for f in Ficha.objects.\
filter(formulario__tipo__nome='Triagem').\
filter(formulario__nome__icontains=u'implementa').only('paciente')]
fichas_projeto_implementacao_report = SortedDict().fromkeys([us.nome for us in UnidadeSaude.objects.all()])
for k in fichas_projeto_implementacao_report.keys():
fichas_projeto_implementacao_report[k]= SortedDict().fromkeys(column_us_implementacao, 0)
num = 999
for sub_pacients in [pacients_implementacao[i::num] for i in range(num)]:
query_fichas_projeto_implementacao_report = Ficha.objects.\
filter(formulario__nome__in= column_us_implementacao).\
filter(paciente__in=sub_pacients).\
values('unidadesaude__nome', 'formulario__nome').\
annotate(numero_fichas=Count('pk'))
for row in query_fichas_projeto_implementacao_report:
fichas_projeto_implementacao_report[row['unidadesaude__nome']][row['formulario__nome']] = \
fichas_projeto_implementacao_report[row['unidadesaude__nome']][row['formulario__nome']] +row['numero_fichas']
return render_to_response('admin/unidadesaude_log.html',
locals(), RequestContext(request, {}))
def log_unidadesaude_by_form(request, healthUnit,stDate=365, endDate=0):
if not request.user.is_authenticated():
return HttpResponseRedirect(settings.SITE_ROOT + 'admin/')
now= datetime.now()
try:
us = UnidadeSaude.objects.get(pk=int(healthUnit))
except:
return HttpResponseNotFound('Id de unidade errado')
truncate_date = connection.ops.date_trunc_sql('month', 'forms_ficha.data_insercao')
fichas_report = Ficha.objects.\
filter(
unidadesaude=us,
data_insercao__gte=now -timedelta(days=stDate),
data_insercao__lte=now -timedelta(days=endDate)
).\
extra(select={'month': truncate_date}).\
values('formulario__nome', 'month').\
annotate(numero_fichas=Count('pk')).\
order_by('-month')
fichas_report = [ dict([
('formulario__nome', l['formulario__nome']),
('month', datetime.strptime(l['month'].split(' ')[0], '%Y-%m-%d').strftime('%Y%m')),
('numero_fichas', l['numero_fichas'])
]) for l in fichas_report]
columns = [dt.strftime('%Y%m') for dt in getMonthList(now, stDate, endDate)]
forms = Formulario.objects.all()
rows = [r.nome for r in forms]
table_data = SortedDict().fromkeys(rows)
for k in table_data.keys():
table_data[k] = SortedDict().fromkeys(columns, 0)
for f in fichas_report:
table_data[f['formulario__nome']][f['month']] = f['numero_fichas']
return json_response(table_data)
def json_response(data):
return HttpResponse(
simplejson.dumps(data),
content_type = 'application/javascript; charset=utf8'
)
add_formulario = staff_member_required(add_formulario)
edit_formulario = staff_member_required(edit_formulario)
log_unidadesaude = staff_member_required(log_unidadesaude)
log_unidadesaude_by_form = staff_member_required(log_unidadesaude_by_form)