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 participacion.views |
---|
8 | # |
---|
9 | # Vistas correspondientes a la aplicación participacion |
---|
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, redirect |
---|
17 | from django.core.urlresolvers import reverse_lazy |
---|
18 | from django.http import JsonResponse |
---|
19 | from django.views.generic import FormView, TemplateView |
---|
20 | from .forms import ParticipacionSelectForm, ParticipacionSearchForm |
---|
21 | from .models import RespuestaAbierta, RespuestaOpciones, RespuestaSino |
---|
22 | from consulta.models import Pregunta, Opcion |
---|
23 | import requests |
---|
24 | |
---|
25 | class ParticipacionIndex(FormView): |
---|
26 | """! |
---|
27 | Clase que gestiona la vista principal de la participación |
---|
28 | |
---|
29 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
30 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
31 | @date 22-02-2017 |
---|
32 | @version 1.0.0 |
---|
33 | """ |
---|
34 | template_name = "participacion.index.html" |
---|
35 | form_class = ParticipacionSelectForm |
---|
36 | |
---|
37 | def get_success_url(self): |
---|
38 | """! |
---|
39 | Metodo que permite definir la url de dirección al ser válido el formulario |
---|
40 | |
---|
41 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
42 | @copyright GNU/GPLv2 |
---|
43 | @date 22-02-2017 |
---|
44 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
45 | @return Retorna la url |
---|
46 | """ |
---|
47 | consulta = self.request.POST['consultas'] |
---|
48 | return reverse_lazy('participacion_busqueda', |
---|
49 | kwargs={'pk': consulta}) |
---|
50 | |
---|
51 | |
---|
52 | class ParticipacionSearch(FormView): |
---|
53 | """! |
---|
54 | Clase que gestiona la busqueda para la participación |
---|
55 | |
---|
56 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
57 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
58 | @date 23-02-2017 |
---|
59 | @version 1.0.0 |
---|
60 | """ |
---|
61 | template_name = "participacion.search.html" |
---|
62 | form_class = ParticipacionSearchForm |
---|
63 | |
---|
64 | def get_success_url(self): |
---|
65 | """! |
---|
66 | Metodo que permite definir la url de dirección al ser válido el formulario |
---|
67 | |
---|
68 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
69 | @copyright GNU/GPLv2 |
---|
70 | @date 23-02-2017 |
---|
71 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
72 | @return Retorna la url |
---|
73 | """ |
---|
74 | entes = self.request.POST['entes'] |
---|
75 | return reverse_lazy('participacion_entes', kwargs={'pk': self.kwargs['pk'],'id_ente':entes}) |
---|
76 | |
---|
77 | class ParticipacionCreate(TemplateView): |
---|
78 | """! |
---|
79 | Clase que gestiona la vista principal de la participación |
---|
80 | |
---|
81 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
82 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
83 | @date 22-02-2017 |
---|
84 | @version 1.0.0 |
---|
85 | """ |
---|
86 | template_name = "participacion.create.html" |
---|
87 | |
---|
88 | def get_context_data(self, **kwargs): |
---|
89 | """! |
---|
90 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
91 | |
---|
92 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
93 | @copyright GNU/GPLv2 |
---|
94 | @date 23-02-2017 |
---|
95 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
96 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
97 | @return Retorna los datos de contexto |
---|
98 | """ |
---|
99 | valores = {} |
---|
100 | for pregunta in Pregunta.objects.filter(consulta_id=kwargs['pk']).all(): |
---|
101 | label = '<label>'+pregunta.texto_pregunta+'</label>' |
---|
102 | campo = '' |
---|
103 | if pregunta.tipo_pregunta.id == 1: |
---|
104 | campo = '' |
---|
105 | for opcion in Opcion.objects.filter(pregunta_id=pregunta.id).all(): |
---|
106 | campo += '<label for="'+kwargs['pk']+'">'+opcion.texto_opcion+'</label><input type="radio" name="consulta_respuesta_radio_'+str(pregunta.id)+'" id="'+kwargs['pk']+'"value="'+str(opcion.id)+'" class="icheck">' |
---|
107 | elif pregunta.tipo_pregunta.id == 2: |
---|
108 | campo = '' |
---|
109 | for opcion in Opcion.objects.filter(pregunta_id=pregunta.id).all(): |
---|
110 | campo += '<label for="'+kwargs['pk']+'">'+opcion.texto_opcion+'</label><input type="checkbox" name="consulta_respuesta_check_'+kwargs['pk']+'" id="'+kwargs['pk']+'"value="'+str(opcion.id)+'" class="icheck">' |
---|
111 | elif pregunta.tipo_pregunta.id > 2 and pregunta.tipo_pregunta.id < 5: |
---|
112 | campo += '<label for="'+kwargs['pk']+'">Si</label><input type="radio" name="consulta_respuesta_sino_'+str(pregunta.id)+'" id="'+kwargs['pk']+'"value="Si" class="icheck">' |
---|
113 | if(pregunta.tipo_pregunta.id == 3): |
---|
114 | campo += '<label for="'+kwargs['pk']+'">No</label><input type="radio" name="consulta_respuesta_sino_'+str(pregunta.id)+'" id="'+kwargs['pk']+'"value="No" class="icheck">' |
---|
115 | else: |
---|
116 | campo += '<label for="'+kwargs['pk']+'">No</label><input type="radio" name="consulta_respuesta_sino_'+str(pregunta.id)+'" id="'+kwargs['pk']+'"value="No" class="icheck need_justification">' |
---|
117 | campo += '<div id="div_justificar_'+kwargs['pk']+'" style="display:none;"><label>Justifique su Respuesta</label>' |
---|
118 | campo += '<textarea class="form-control" id="respuesta_justificar_'+kwargs['pk']+'" name="consulta_respuesta_justificar_'+str(pregunta.id)+'">' |
---|
119 | campo += '</textarea></div>' |
---|
120 | else: |
---|
121 | campo = '<textarea class="form-control" name="consulta_respuesta_abierta_'+str(pregunta.id)+'"></textarea>' |
---|
122 | valores[pregunta.id] = {'label':label,'field':campo} |
---|
123 | kwargs['preguntas'] = valores |
---|
124 | return super(ParticipacionCreate, self).get_context_data(**kwargs) |
---|
125 | |
---|
126 | def post(self,request,pk): |
---|
127 | """! |
---|
128 | Metodo que sobreescribe el post del formulario |
---|
129 | |
---|
130 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
131 | @copyright GNU/GPLv2 |
---|
132 | @date 20-03-2017 |
---|
133 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
134 | @param request <b>{object}</b> Objeto que instancia la petición |
---|
135 | @param pk <b>{int}</b> Recibe el id de la consulta |
---|
136 | @return Retorna los datos de contexto |
---|
137 | """ |
---|
138 | data = dict(request.POST) |
---|
139 | del data['csrfmiddlewaretoken'] |
---|
140 | if self.request.is_ajax(): |
---|
141 | for key in data.keys(): |
---|
142 | parent_id = key.split("_")[-1] |
---|
143 | if 'sino' in key: |
---|
144 | value = True if data[key][0] == 'Si' else False |
---|
145 | justify_id = 'consulta_respuesta_justificar_'+str(parent_id) |
---|
146 | self.crear_respuesta_sino(parent_id,value,123) |
---|
147 | if(not value and justify_id in data.keys()): |
---|
148 | respuesta = data[justify_id][0] |
---|
149 | self.crear_respuesta_abierta(parent_id,respuesta,123,True) |
---|
150 | elif 'radio' in key or 'check' in key: |
---|
151 | for value in data[key]: |
---|
152 | self.crear_respuesta_opciones(value,123) |
---|
153 | elif 'abierta' in key: |
---|
154 | value = data[key][0] |
---|
155 | self.crear_respuesta_abierta(parent_id,value,123) |
---|
156 | return JsonResponse({"code":True}) |
---|
157 | return redirect(reverse_lazy('participacion_consulta',kwargs={'pk':pk})) |
---|
158 | |
---|
159 | def crear_respuesta_sino(self,parent_id,value,ente): |
---|
160 | """! |
---|
161 | Metodo para crear una respuesta de si/no |
---|
162 | |
---|
163 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
164 | @copyright GNU/GPLv2 |
---|
165 | @date 27-03-2017 |
---|
166 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
167 | @param parent_id <b>{int}</b> Recibe el número del id del padre |
---|
168 | @param value <b>{bool}</b> Recibe el valor de la respuesta |
---|
169 | @param ente <b>{int}</b> Recibe el id del ente |
---|
170 | @return Retorna los datos de contexto |
---|
171 | """ |
---|
172 | parent = Pregunta.objects.get(pk=parent_id) |
---|
173 | respuesta = RespuestaSino() |
---|
174 | respuesta.pregunta = parent |
---|
175 | respuesta.ente_adscrito = ente |
---|
176 | respuesta.respuesta = value |
---|
177 | respuesta.save() |
---|
178 | |
---|
179 | def crear_respuesta_opciones(self,parent_id,ente): |
---|
180 | """! |
---|
181 | Metodo para crear una respuesta de selección simple y múltiple |
---|
182 | |
---|
183 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
184 | @copyright GNU/GPLv2 |
---|
185 | @date 28-03-2017 |
---|
186 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
187 | @param parent_id <b>{int}</b> Recibe el número del id del padre |
---|
188 | @param ente <b>{int}</b> Recibe el id del ente |
---|
189 | @return Retorna los datos de contexto |
---|
190 | """ |
---|
191 | parent = Opcion.objects.get(pk=parent_id) |
---|
192 | respuesta = RespuestaOpciones() |
---|
193 | respuesta.opcion = parent |
---|
194 | respuesta.ente_adscrito = ente |
---|
195 | respuesta.save() |
---|
196 | |
---|
197 | def crear_respuesta_abierta(self,parent_id,value,ente,es_justificacion = False): |
---|
198 | """! |
---|
199 | Metodo para crear una respuesta abierta |
---|
200 | |
---|
201 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
202 | @copyright GNU/GPLv2 |
---|
203 | @date 28-03-2017 |
---|
204 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
205 | @param parent_id <b>{int}</b> Recibe el número del id del padre |
---|
206 | @param value <b>{str}</b> Recibe el valor de la respuesta |
---|
207 | @param ente <b>{int}</b> Recibe el id del ente |
---|
208 | @param es_justificacion <b>{bool}</b> Recibe el párametro que indica si es una justifiación |
---|
209 | @return Retorna los datos de contexto |
---|
210 | """ |
---|
211 | parent = Pregunta.objects.get(pk=parent_id) |
---|
212 | respuesta = RespuestaAbierta() |
---|
213 | respuesta.pregunta = parent |
---|
214 | respuesta.ente_adscrito = ente |
---|
215 | respuesta.texto_respuesta = value |
---|
216 | respuesta.es_justificacion = es_justificacion |
---|
217 | respuesta.save() |
---|
218 | |
---|
219 | |
---|
220 | class ParticipacionEntes(TemplateView): |
---|
221 | """! |
---|
222 | Clase que gestiona la vista de los entes adscritos |
---|
223 | |
---|
224 | @author Argenis Osorio (aosorio at cenditel.gob.ve) |
---|
225 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
226 | @date 07-04-2017 |
---|
227 | @version 1.0.0 |
---|
228 | """ |
---|
229 | template_name = "participacion.entes.html" |
---|
230 | |
---|
231 | def get_context_data(self, **kwargs): |
---|
232 | """! |
---|
233 | Metodo que permite cargar los datos de los entes adscritos |
---|
234 | |
---|
235 | @author Argenis Osorio (aosorio at cenditel.gob.ve) |
---|
236 | @copyright GNU/GPLv2 |
---|
237 | @date 07-04-2017 |
---|
238 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
239 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
240 | @return Retorna los datos de contexto |
---|
241 | """ |
---|
242 | url = "http://192.168.12.152:8080/services_sipes/entes_planificadores/views/servicios_webs?display_id=page_2&parent="+str(kwargs['id_ente']) |
---|
243 | |
---|
244 | r = requests.get(url) |
---|
245 | if not r.status_code == 200: |
---|
246 | print "ERROR:" |
---|
247 | return ('', 'ocurrio error...'), |
---|
248 | data = r.json() |
---|
249 | kwargs['data'] = data |
---|
250 | return super(ParticipacionEntes, self).get_context_data(**kwargs) |
---|