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