1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | Sistema de Consulta Pública |
---|
4 | |
---|
5 | Copyleft (@) 2017 CENDITEL nodo Mérida - https://planificacion.cenditel.gob.ve/trac/wiki/ModeladoTopicos_2017 |
---|
6 | """ |
---|
7 | ## @package base.functions |
---|
8 | # |
---|
9 | # Clases genéricas de la consulta |
---|
10 | # @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
11 | # @author <a href='http://www.cenditel.gob.ve'>Centro Nacional de Desarrollo e Investigación en TecnologÃas Libres |
---|
12 | # (CENDITEL) nodo Mérida - Venezuela</a> |
---|
13 | # @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
14 | # @version 1.0 |
---|
15 | from __future__ import unicode_literals |
---|
16 | from django.http import HttpResponseServerError |
---|
17 | from django.contrib.auth.models import User |
---|
18 | from consulta.models import Consulta,TipoPregunta, Pregunta |
---|
19 | from participacion.models import RespuestaAbierta |
---|
20 | from procesamiento.models import Procesamiento |
---|
21 | from consulta_publica.settings import PROCESAMIENTO_PATH |
---|
22 | from .models import Entidad, Municipio, Parroquia |
---|
23 | from users.models import Perfil, Universidades |
---|
24 | import requests |
---|
25 | import copy |
---|
26 | import os |
---|
27 | import shutil |
---|
28 | |
---|
29 | def cargar_tipo_pregunta(): |
---|
30 | """! |
---|
31 | Función que permite cargar los tipos de preguntas que existen |
---|
32 | |
---|
33 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
34 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
35 | @date 15-02-2017 |
---|
36 | @return Devuelve una tupla con los tipos de pregunta |
---|
37 | """ |
---|
38 | |
---|
39 | lista = ('', 'Seleccione...'), |
---|
40 | |
---|
41 | try: |
---|
42 | for tipo_pregunta in TipoPregunta.objects.all(): |
---|
43 | lista += (tipo_pregunta.id, tipo_pregunta.tipo), |
---|
44 | except Exception as e: |
---|
45 | pass |
---|
46 | |
---|
47 | return lista |
---|
48 | |
---|
49 | def cargar_consulta(): |
---|
50 | """! |
---|
51 | Función que permite cargar las consultas |
---|
52 | |
---|
53 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
54 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
55 | @date 22-02-2017 |
---|
56 | @return Devuelve una tupla con las consultas |
---|
57 | """ |
---|
58 | |
---|
59 | lista = ('', 'Seleccione...'), |
---|
60 | |
---|
61 | try: |
---|
62 | for consulta in Consulta.objects.filter(activa=True).all(): |
---|
63 | lista += (consulta.id, consulta.nombre_consulta), |
---|
64 | except Exception as e: |
---|
65 | pass |
---|
66 | |
---|
67 | return lista |
---|
68 | |
---|
69 | |
---|
70 | def cargar_procesamiento(pk=None): |
---|
71 | """! |
---|
72 | Función que permite cargar los procesamientos |
---|
73 | |
---|
74 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
75 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
76 | @date 28-03-2017 |
---|
77 | @return Devuelve una tupla con los perfiles de procesamiento |
---|
78 | """ |
---|
79 | |
---|
80 | lista = ('', 'Seleccione...'), |
---|
81 | |
---|
82 | Pro = Procesamiento.objects.filter(consulta_id=pk) if pk else Procesamiento.objects |
---|
83 | try: |
---|
84 | for procesamiento in Pro.all(): |
---|
85 | lista += (procesamiento.id, procesamiento.procesamiento_dir), |
---|
86 | except Exception as e: |
---|
87 | pass |
---|
88 | |
---|
89 | return lista |
---|
90 | |
---|
91 | |
---|
92 | def dump_exception(): |
---|
93 | """! |
---|
94 | Función para captar los errores e imprimirlos |
---|
95 | |
---|
96 | @author Jorge Redondo (jredondo at cenditel.gob.ve) |
---|
97 | @copyright GNU/GPLv2 |
---|
98 | @param request <b>{object}</b> Objeto que mantiene la peticion |
---|
99 | @return Retorna una respuesta http con el error |
---|
100 | """ |
---|
101 | import sys,traceback |
---|
102 | exc_type, exc_value, exc_traceback = sys.exc_info() |
---|
103 | print "*** print_tb:" |
---|
104 | traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) |
---|
105 | print "*** print_exception:" |
---|
106 | traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) |
---|
107 | return HttpResponseServerError(str(exc_value)) |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | def createPreprocessingFiles(id): |
---|
112 | """! |
---|
113 | Crea los archivos necesarios del preprocesamiento a partir de las respuestas |
---|
114 | abiertas de una consulta. |
---|
115 | |
---|
116 | La función creará el directorio consulta_publica/static/procesamiento_files/nombre_consulta/orig |
---|
117 | con un conjunto de archivos de texto que representan cada una de las repuestas |
---|
118 | abiertas de la consulta. Por ejemplo: |
---|
119 | . |
---|
120 | miconsulta |
---|
121 | |---orig |
---|
122 | |--- respuesta_5.txt |
---|
123 | |--- respuesta_6.txt |
---|
124 | |
---|
125 | En caso de existir el directorio al momento de ejecutarse esta función será |
---|
126 | borrado. |
---|
127 | |
---|
128 | @author Antonio Araujo (aaraujo at cenditel.gob.ve) |
---|
129 | @copyright GNU/GPLv2 |
---|
130 | @param id Identificador de la consulta para obtener respuestas abiertas |
---|
131 | """ |
---|
132 | |
---|
133 | try: |
---|
134 | # obtener la consulta con id pasado como argumento |
---|
135 | consulta = Consulta.objects.get(id=id) |
---|
136 | |
---|
137 | ## Se crea un nombre sin espacios en blanco |
---|
138 | nombre = "_".join(consulta.nombre_consulta.split(" ")) |
---|
139 | |
---|
140 | # crear un directorio con el nombre de la consulta en consulta_publica/static/procesamiento_files/ |
---|
141 | processingDirectory = PROCESAMIENTO_PATH + '/' + nombre + '/orig' |
---|
142 | |
---|
143 | # borrar directorio en el caso de existir |
---|
144 | if os.path.isdir(processingDirectory) : |
---|
145 | shutil.rmtree(processingDirectory) |
---|
146 | os.makedirs(processingDirectory) |
---|
147 | |
---|
148 | # obtener las preguntas |
---|
149 | for pregunta in Pregunta.objects.filter(consulta_id=consulta.id).all() : |
---|
150 | if pregunta.tipo_pregunta_id==5 : |
---|
151 | # ubicar la respuesta |
---|
152 | for respuesta in RespuestaAbierta.objects.filter(pregunta_id=pregunta.id).all(): |
---|
153 | # escribir archivo a disco |
---|
154 | filePath = processingDirectory + '/' + 'respuesta_' + str(respuesta.id)+".txt" |
---|
155 | fo = open(filePath, "w") |
---|
156 | fo.write(respuesta.texto_respuesta.encode('utf8')); |
---|
157 | fo.close() |
---|
158 | |
---|
159 | return True |
---|
160 | |
---|
161 | except Exception as e: |
---|
162 | print "La consulta con identificado "+ str(id) + " no existe" |
---|
163 | print e |
---|
164 | return False |
---|
165 | pass |
---|
166 | |
---|
167 | def validate_dir(pk): |
---|
168 | """! |
---|
169 | Función que permite cargar las consultas con directorios creados |
---|
170 | |
---|
171 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
172 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
173 | @date 17-04-2017 |
---|
174 | @param pk {int} Recibe id de la consulta |
---|
175 | @return Devuelve una tupla con las consultas que tienen directorios |
---|
176 | """ |
---|
177 | |
---|
178 | consulta = Consulta.objects.filter(pk=pk) |
---|
179 | if consulta: |
---|
180 | consulta = consulta.get() |
---|
181 | nombre = "_".join(consulta.nombre_consulta.split(" ")) |
---|
182 | consulta_dir = PROCESAMIENTO_PATH+"/"+nombre |
---|
183 | if os.path.exists(consulta_dir): |
---|
184 | return True |
---|
185 | return False |
---|
186 | else: |
---|
187 | return False |
---|
188 | |
---|
189 | def cargar_entidad(): |
---|
190 | """! |
---|
191 | Función que permite cargar todas las entidades |
---|
192 | |
---|
193 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
194 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
195 | @date 20-04-2017 |
---|
196 | @return Devuelve una tupla con las entidades |
---|
197 | """ |
---|
198 | |
---|
199 | lista = ('', 'Seleccione...'), |
---|
200 | |
---|
201 | try: |
---|
202 | for entidad in Entidad.objects.all(): |
---|
203 | lista += (entidad.codigo, entidad.nombre), |
---|
204 | except Exception as e: |
---|
205 | pass |
---|
206 | |
---|
207 | return lista |
---|
208 | |
---|
209 | |
---|
210 | def cargar_municipios(): |
---|
211 | """! |
---|
212 | Función que permite cargar todas los municipios |
---|
213 | |
---|
214 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
215 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
216 | @date 20-04-2017 |
---|
217 | @return Devuelve una tupla con los municipios |
---|
218 | """ |
---|
219 | |
---|
220 | lista = ('', 'Seleccione...'), |
---|
221 | |
---|
222 | try: |
---|
223 | for municipio in Municipio.objects.all(): |
---|
224 | lista += (municipio.codigo, municipio.nombre), |
---|
225 | except Exception as e: |
---|
226 | pass |
---|
227 | |
---|
228 | return lista |
---|
229 | |
---|
230 | |
---|
231 | def cargar_parroquias(): |
---|
232 | """! |
---|
233 | Función que permite cargar todas las parroquias |
---|
234 | |
---|
235 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
236 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
237 | @date 20-04-2017 |
---|
238 | @return Devuelve una tupla con las parroquias |
---|
239 | """ |
---|
240 | |
---|
241 | lista = ('', 'Seleccione...'), |
---|
242 | |
---|
243 | try: |
---|
244 | for parroquia in Parroquia.objects.all(): |
---|
245 | lista += (parroquia.codigo, parroquia.nombre), |
---|
246 | except Exception as e: |
---|
247 | pass |
---|
248 | |
---|
249 | return lista |
---|
250 | |
---|
251 | |
---|
252 | def validate_cedula(cedula): |
---|
253 | """! |
---|
254 | Función que permite validar la cedula |
---|
255 | |
---|
256 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
257 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
258 | @date 20-04-2017 |
---|
259 | @param cedula {str} Recibe el número de cédula |
---|
260 | @return Devuelve verdadero o falso |
---|
261 | """ |
---|
262 | |
---|
263 | cedula = Perfil.objects.filter(cedula=cedula) |
---|
264 | if cedula: |
---|
265 | return True |
---|
266 | else: |
---|
267 | return False |
---|
268 | |
---|
269 | def validate_email(email): |
---|
270 | """! |
---|
271 | Función que permite validar la cedula |
---|
272 | |
---|
273 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
274 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
275 | @date 20-04-2017 |
---|
276 | @param cedula {str} Recibe el número de cédula |
---|
277 | @return Devuelve verdadero o falso |
---|
278 | """ |
---|
279 | |
---|
280 | email = User.objects.filter(email=email) |
---|
281 | if email: |
---|
282 | return True |
---|
283 | else: |
---|
284 | return False |
---|
285 | |
---|
286 | def cargar_preguntas(id): |
---|
287 | """! |
---|
288 | Función que permite cargar preguntas asignadas a una consulta |
---|
289 | |
---|
290 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
291 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
292 | @date 31-05-2017 |
---|
293 | @return Devuelve una tupla con las consultas |
---|
294 | """ |
---|
295 | |
---|
296 | lista = ('', 'Seleccione...'), |
---|
297 | |
---|
298 | try: |
---|
299 | for pregunta in Pregunta.objects.filter(consulta_id=id).all(): |
---|
300 | lista += (pregunta.id, pregunta.texto_pregunta), |
---|
301 | except Exception as e: |
---|
302 | pass |
---|
303 | |
---|
304 | return lista |
---|
305 | |
---|
306 | def cargar_universidades(): |
---|
307 | """! |
---|
308 | Función que permite cargar todos los datos de las universidades |
---|
309 | |
---|
310 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
311 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
312 | @date 08-09-2017 |
---|
313 | @return Devuelve una tupla con las consultas |
---|
314 | """ |
---|
315 | |
---|
316 | lista = ('', 'Seleccione...'), |
---|
317 | |
---|
318 | try: |
---|
319 | for universidad in Universidades.objects.all(): |
---|
320 | lista += (universidad.id, universidad.nombre), |
---|
321 | except Exception as e: |
---|
322 | pass |
---|
323 | |
---|
324 | return lista |
---|