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 consulta.views |
---|
8 | # |
---|
9 | # Vistas correspondientes a la aplicación 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 | import json |
---|
16 | from django.shortcuts import render |
---|
17 | from django.core.urlresolvers import reverse_lazy |
---|
18 | from django.http import JsonResponse |
---|
19 | from django.views.generic import CreateView, ListView, TemplateView, DeleteView, DetailView, UpdateView |
---|
20 | from django.contrib.messages.views import SuccessMessageMixin |
---|
21 | from .models import Consulta, Pregunta, TipoPregunta, Opcion |
---|
22 | from .forms import ConsultaForm, ConsultaPreguntaForm |
---|
23 | |
---|
24 | class ConsultaIndex(TemplateView): |
---|
25 | """! |
---|
26 | Clase que gestiona la vista principal de la consulta |
---|
27 | |
---|
28 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
29 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
30 | @date 15-02-2017 |
---|
31 | @version 1.0.0 |
---|
32 | """ |
---|
33 | template_name = "consulta.index.html" |
---|
34 | |
---|
35 | |
---|
36 | class ConsultaCreate(SuccessMessageMixin,CreateView): |
---|
37 | """! |
---|
38 | Clase que gestiona la creación de consultas |
---|
39 | |
---|
40 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
41 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
42 | @date 15-02-2017 |
---|
43 | @version 1.0.0 |
---|
44 | """ |
---|
45 | model = Consulta |
---|
46 | form_class = ConsultaPreguntaForm |
---|
47 | template_name = "consulta.create.html" |
---|
48 | success_message = "Se creó la consulta con éxito" |
---|
49 | success_url = reverse_lazy('consulta_index') |
---|
50 | |
---|
51 | def get_context_data(self, **kwargs): |
---|
52 | """! |
---|
53 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
54 | |
---|
55 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
56 | @copyright GNU/GPLv2 |
---|
57 | @date 16-02-2017 |
---|
58 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
59 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
60 | @return Retorna los datos de contexto |
---|
61 | """ |
---|
62 | if 'tipo_pregunta_modal' in self.request.POST and 'texto_pregunta_modal' in self.request.POST: |
---|
63 | post_data = dict(self.request.POST.lists()) |
---|
64 | if ((len(post_data['tipo_pregunta_modal'])>0 and len(post_data['texto_pregunta_modal'])>0) and (len(post_data['texto_pregunta_modal']) == len(post_data['tipo_pregunta_modal']))): |
---|
65 | valores = {} |
---|
66 | for i in range(len(post_data['tipo_pregunta_modal'])): |
---|
67 | valores[i] = {'texto_pregunta':post_data['texto_pregunta_modal'][i],'tipo_pregunta':post_data['tipo_pregunta_modal'][i]} |
---|
68 | kwargs['opciones'] = json.dumps(valores) |
---|
69 | return super(ConsultaCreate, self).get_context_data(**kwargs) |
---|
70 | |
---|
71 | |
---|
72 | def form_valid(self,form): |
---|
73 | """! |
---|
74 | Metodo que valida si el formulario es valido |
---|
75 | |
---|
76 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
77 | @copyright GNU/GPLv2 |
---|
78 | @date 15-02-2017 |
---|
79 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
80 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
81 | @return Retorna el formulario validado |
---|
82 | """ |
---|
83 | |
---|
84 | post_data = dict(self.request.POST.lists()) |
---|
85 | |
---|
86 | ## Se crea el objeto de la consulta |
---|
87 | self.object = form.save(commit=False) |
---|
88 | self.object.nombre_consulta = form.cleaned_data['nombre_consulta'] |
---|
89 | self.object.activa = form.cleaned_data['activa'] |
---|
90 | self.object.save() |
---|
91 | |
---|
92 | ## Se crea la pregunta que se pide por defecto |
---|
93 | tipo = TipoPregunta.objects.get(pk=form.cleaned_data['tipo_pregunta']) |
---|
94 | pregunta = Pregunta() |
---|
95 | pregunta.texto_pregunta = form.cleaned_data['texto_pregunta'] |
---|
96 | pregunta.tipo_pregunta = tipo |
---|
97 | pregunta.consulta = self.object |
---|
98 | pregunta.save() |
---|
99 | |
---|
100 | ## Si se agregaron más preguntas se crean |
---|
101 | if 'tipo_pregunta_modal' in self.request.POST and 'texto_pregunta_modal' in self.request.POST: |
---|
102 | self.create_questions(self.object,post_data) |
---|
103 | |
---|
104 | return super(ConsultaCreate, self).form_valid(form) |
---|
105 | |
---|
106 | |
---|
107 | def create_questions(self,objeto,data): |
---|
108 | """! |
---|
109 | Metodo para crear preguntas adicionales |
---|
110 | |
---|
111 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
112 | @copyright GNU/GPLv2 |
---|
113 | @date 16-02-2017 |
---|
114 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
115 | @param objeto <b>{object}</b> Objeto de la consulta |
---|
116 | @param data <b>{dict}</b> Diccionario con los valores a guardar |
---|
117 | """ |
---|
118 | if len(data['texto_pregunta_modal']) == len(data['tipo_pregunta_modal']): |
---|
119 | print data['texto_pregunta_modal'] |
---|
120 | for i in range(len(data['texto_pregunta_modal'])): |
---|
121 | tipo = TipoPregunta.objects.get(pk=data['tipo_pregunta_modal'][i]) |
---|
122 | pregunta = Pregunta() |
---|
123 | pregunta.texto_pregunta = data['texto_pregunta_modal'][i] |
---|
124 | pregunta.tipo_pregunta = tipo |
---|
125 | pregunta.consulta = objeto |
---|
126 | pregunta.save() |
---|
127 | |
---|
128 | class ConsultaList(ListView): |
---|
129 | """! |
---|
130 | Clase que gestiona la lista de consultas |
---|
131 | |
---|
132 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
133 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
134 | @date 15-02-2017 |
---|
135 | @version 1.0.0 |
---|
136 | """ |
---|
137 | model = Consulta |
---|
138 | template_name = "consulta.list.html" |
---|
139 | |
---|
140 | |
---|
141 | class ConsultaDetail(DetailView): |
---|
142 | """! |
---|
143 | Clase que gestiona el detalle de una consulta |
---|
144 | |
---|
145 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
146 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
147 | @date 17-02-2017 |
---|
148 | @version 1.0.0 |
---|
149 | """ |
---|
150 | model = Consulta |
---|
151 | template_name = "consulta.detail.html" |
---|
152 | |
---|
153 | def get_context_data(self, **kwargs): |
---|
154 | """! |
---|
155 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
156 | |
---|
157 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
158 | @copyright GNU/GPLv2 |
---|
159 | @date 17-02-2017 |
---|
160 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
161 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
162 | @return Retorna los datos de contexto |
---|
163 | """ |
---|
164 | kwargs['preguntas'] = Pregunta.objects.filter(consulta_id=kwargs['object'].id).all() |
---|
165 | return super(ConsultaDetail, self).get_context_data(**kwargs) |
---|
166 | |
---|
167 | class ConsultaDelete(SuccessMessageMixin,DeleteView): |
---|
168 | """! |
---|
169 | Clase que gestiona la lista de consultas |
---|
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 15-02-2017 |
---|
174 | @version 1.0.0 |
---|
175 | """ |
---|
176 | model = Consulta |
---|
177 | template_name = "consulta.delete.html" |
---|
178 | success_message = "Se eliminó la consulta con éxito" |
---|
179 | success_url = reverse_lazy('consulta_index') |
---|
180 | |
---|
181 | |
---|
182 | class ConsultaUpdate(SuccessMessageMixin,UpdateView): |
---|
183 | """! |
---|
184 | Clase que gestiona la actualización de consultas |
---|
185 | |
---|
186 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
187 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
188 | @date 17-02-2017 |
---|
189 | @version 1.0.0 |
---|
190 | """ |
---|
191 | model = Consulta |
---|
192 | form_class = ConsultaForm |
---|
193 | template_name = "consulta.update.html" |
---|
194 | success_message = "Se actualizó la consulta con éxito" |
---|
195 | success_url = reverse_lazy('consulta_list') |
---|
196 | |
---|
197 | |
---|
198 | def get_context_data(self, **kwargs): |
---|
199 | """! |
---|
200 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
201 | |
---|
202 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
203 | @copyright GNU/GPLv2 |
---|
204 | @date 17-02-2017 |
---|
205 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
206 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
207 | @return Retorna los datos de contexto |
---|
208 | """ |
---|
209 | preguntas = Pregunta.objects.filter(consulta_id=self.object.id).all() |
---|
210 | kwargs['preguntas'] = preguntas |
---|
211 | return super(ConsultaUpdate, self).get_context_data(**kwargs) |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | class OpcionesCreate(CreateView): |
---|
216 | """! |
---|
217 | Clase que gestiona la creación de opciones |
---|
218 | |
---|
219 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
220 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
221 | @date 20-02-2017 |
---|
222 | @version 1.0.0 |
---|
223 | """ |
---|
224 | model = Opcion |
---|
225 | fields = ['texto_opcion'] |
---|
226 | template_name = "consulta.create.html" |
---|
227 | success_url = reverse_lazy('consulta_index') |
---|
228 | |
---|
229 | |
---|
230 | def form_valid(self,form): |
---|
231 | """! |
---|
232 | Metodo que valida si el formulario es valido |
---|
233 | |
---|
234 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
235 | @copyright GNU/GPLv2 |
---|
236 | @date 20-02-2017 |
---|
237 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
238 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
239 | @return Retorna el formulario validado |
---|
240 | """ |
---|
241 | post_data = dict(self.request.POST.lists()) |
---|
242 | pregunta = Pregunta.objects.get(id=int(self.kwargs['pk'])) |
---|
243 | |
---|
244 | ## Se guarda la primera opcion |
---|
245 | self.object = form.save(commit=False) |
---|
246 | self.object.texto_opcion = form.cleaned_data['texto_opcion'] |
---|
247 | self.object.pregunta = pregunta |
---|
248 | self.object.save() |
---|
249 | |
---|
250 | ## Se guardan las demás opciones si existen |
---|
251 | for i in range(len(post_data['texto_opcion'])-1): |
---|
252 | opcion = Opcion() |
---|
253 | opcion.texto_opcion = post_data['texto_opcion'][i] |
---|
254 | opcion.pregunta = pregunta |
---|
255 | opcion.save() |
---|
256 | |
---|
257 | if self.request.is_ajax(): |
---|
258 | return JsonResponse({"code":True}) |
---|
259 | |
---|
260 | return super(OpcionesCreate, self).form_valid(form) |
---|
261 | |
---|
262 | def form_invalid(self,form): |
---|
263 | """! |
---|
264 | Metodo que valida si el formulario es invalido |
---|
265 | |
---|
266 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
267 | @copyright GNU/GPLv2 |
---|
268 | @date 20-02-2017 |
---|
269 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
270 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
271 | @return Retorna el formulario inválido |
---|
272 | """ |
---|
273 | if self.request.is_ajax(): |
---|
274 | return JsonResponse({"code":False,'errors':form.errors}) |
---|
275 | return super(OpcionesCreate, self).form_invalid(form) |
---|
276 | |
---|
277 | |
---|
278 | class OpcionesUpdate(UpdateView): |
---|
279 | """! |
---|
280 | Clase que gestiona la actualización de las opciones |
---|
281 | |
---|
282 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
283 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
284 | @date 20-02-2017 |
---|
285 | @version 1.0.0 |
---|
286 | """ |
---|
287 | model = Opcion |
---|
288 | fields = ['texto_opcion'] |
---|
289 | template_name = "consulta.update.html" |
---|
290 | success_url = reverse_lazy('consulta_list') |
---|
291 | |
---|
292 | |
---|
293 | def post(self, request): |
---|
294 | """! |
---|
295 | Metodo que sobreescribe la acción por POST |
---|
296 | |
---|
297 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
298 | @copyright GNU/GPLv2 |
---|
299 | @date 20-02-2017 |
---|
300 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
301 | @param request <b>{object}</b> Objeto que contiene la petición |
---|
302 | @return Retorna los datos de contexto |
---|
303 | """ |
---|
304 | post_data = dict(self.request.POST.lists()) |
---|
305 | for i in range(len(post_data['texto_opcion'])): |
---|
306 | opcion = Opcion.objects.filter(id=int(post_data['texto_opcion_id'][i])) |
---|
307 | if(opcion): |
---|
308 | opcion = opcion.get() |
---|
309 | opcion.texto_opcion = post_data['texto_opcion'][i] |
---|
310 | opcion.save() |
---|
311 | |
---|
312 | if self.request.is_ajax(): |
---|
313 | return JsonResponse({"code":True}) |
---|
314 | |
---|
315 | class OpcionesDelete(DeleteView): |
---|
316 | """! |
---|
317 | Clase que gestiona el borrado de una opción |
---|
318 | |
---|
319 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
320 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
321 | @date 20-02-2017 |
---|
322 | @version 1.0.0 |
---|
323 | """ |
---|
324 | model = Opcion |
---|
325 | template_name = "consulta.update.html" |
---|
326 | success_url = reverse_lazy('consulta_list') |
---|
327 | |
---|
328 | |
---|
329 | def post(self, request, pk): |
---|
330 | """! |
---|
331 | Metodo que sobreescribe la acción por POST |
---|
332 | |
---|
333 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
334 | @copyright GNU/GPLv2 |
---|
335 | @date 20-02-2017 |
---|
336 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
337 | @param request <b>{object}</b> Objeto que contiene la petición |
---|
338 | @param pk <b>{int}</b> Recibe el id para filtrar |
---|
339 | @return Retorna los datos de contexto |
---|
340 | """ |
---|
341 | opcion = Opcion.objects.filter(id=int(pk)) |
---|
342 | if(opcion): |
---|
343 | opcion = opcion.get() |
---|
344 | opcion.delete() |
---|
345 | return JsonResponse({'success':True}) |
---|
346 | return JsonResponse({'success':False,'mensaje':'Opción inválida'}) |
---|
347 | |
---|
348 | |
---|
349 | class PreguntaDelete(DeleteView): |
---|
350 | """! |
---|
351 | Clase que gestiona el borrado de una pregunta |
---|
352 | |
---|
353 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
354 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
355 | @date 20-02-2017 |
---|
356 | @version 1.0.0 |
---|
357 | """ |
---|
358 | model = Pregunta |
---|
359 | template_name = "consulta.update.html" |
---|
360 | success_url = reverse_lazy('consulta_list') |
---|
361 | |
---|
362 | |
---|
363 | def post(self, request, pk): |
---|
364 | """! |
---|
365 | Metodo que sobreescribe la acción por POST |
---|
366 | |
---|
367 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
368 | @copyright GNU/GPLv2 |
---|
369 | @date 20-02-2017 |
---|
370 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
371 | @param request <b>{object}</b> Objeto que contiene la petición |
---|
372 | @param pk <b>{int}</b> Recibe el id para filtrar |
---|
373 | @return Retorna los datos de contexto |
---|
374 | """ |
---|
375 | pregunta = Pregunta.objects.filter(id=int(pk)) |
---|
376 | if(pregunta): |
---|
377 | pregunta = pregunta.get() |
---|
378 | ## Si tiene opciones, se buscan y se borran |
---|
379 | if(pregunta.tipo_pregunta_id==1): |
---|
380 | opciones = Opcion.objects.filter(pregunta_id=pregunta.id) |
---|
381 | if (opciones): |
---|
382 | for item in opciones.all(): |
---|
383 | item.delete() |
---|
384 | pregunta.delete() |
---|
385 | return JsonResponse({'success':True}) |
---|
386 | return JsonResponse({'success':False,'mensaje':'Pregunta inválida'}) |
---|
387 | |
---|
388 | |
---|
389 | class PreguntaCreate(CreateView): |
---|
390 | """! |
---|
391 | Clase que gestiona la creación de preguntas |
---|
392 | |
---|
393 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
394 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
395 | @date 21-02-2017 |
---|
396 | @version 1.0.0 |
---|
397 | """ |
---|
398 | model = Pregunta |
---|
399 | fields = ['texto_pregunta','tipo_pregunta'] |
---|
400 | template_name = "consulta.create.html" |
---|
401 | success_url = reverse_lazy('consulta_index') |
---|
402 | |
---|
403 | |
---|
404 | def form_valid(self,form): |
---|
405 | """! |
---|
406 | Metodo que valida si el formulario es valido |
---|
407 | |
---|
408 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
409 | @copyright GNU/GPLv2 |
---|
410 | @date 21-02-2017 |
---|
411 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
412 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
413 | @return Retorna el formulario validado |
---|
414 | """ |
---|
415 | post_data = dict(self.request.POST.lists()) |
---|
416 | consulta = Consulta.objects.get(id=int(self.kwargs['pk'])) |
---|
417 | |
---|
418 | ## Se guarda la primera opcion |
---|
419 | self.object = form.save(commit=False) |
---|
420 | self.object.texto_opcion = form.cleaned_data['texto_pregunta'] |
---|
421 | self.object.tipo_pregunta = form.cleaned_data['tipo_pregunta'] |
---|
422 | self.object.consulta = consulta |
---|
423 | self.object.save() |
---|
424 | |
---|
425 | ## Se guardan las demás opciones si existen |
---|
426 | for i in range(len(post_data['texto_pregunta'])-1): |
---|
427 | tipo_pregunta = TipoPregunta.objects.get(id=int(post_data['tipo_pregunta'][i])) |
---|
428 | pregunta = Pregunta() |
---|
429 | pregunta.texto_opcion = post_data['texto_pregunta'][i] |
---|
430 | pregunta.tipo_pregunta = post_data['tipo_pregunta'][i] |
---|
431 | pregunta.consulta = consulta |
---|
432 | pregunta.save() |
---|
433 | |
---|
434 | if self.request.is_ajax(): |
---|
435 | return JsonResponse({"code":True}) |
---|
436 | |
---|
437 | return super(OpcionesCreate, self).form_valid(form) |
---|
438 | |
---|
439 | def form_invalid(self,form): |
---|
440 | """! |
---|
441 | Metodo que valida si el formulario es invalido |
---|
442 | |
---|
443 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
444 | @copyright GNU/GPLv2 |
---|
445 | @date 21-02-2017 |
---|
446 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
447 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
448 | @return Retorna el formulario inválido |
---|
449 | """ |
---|
450 | if self.request.is_ajax(): |
---|
451 | return JsonResponse({"code":False,'errors':form.errors}) |
---|
452 | return super(OpcionesCreate, self).form_invalid(form) |
---|
453 | |
---|