[a4764f9] | 1 | # -*- encoding: utf-8 -*- |
---|
| 2 | |
---|
| 3 | import os |
---|
| 4 | import requests |
---|
| 5 | from django.conf import settings |
---|
[94b3e3f] | 6 | from django.contrib import messages |
---|
[b8fffff] | 7 | from django.contrib.auth.mixins import LoginRequiredMixin |
---|
[a4764f9] | 8 | from django.core.files.base import ContentFile |
---|
| 9 | from django.core.files.storage import default_storage |
---|
[94b3e3f] | 10 | from django.core.urlresolvers import reverse_lazy |
---|
[5d88f36] | 11 | from django.http import JsonResponse |
---|
[94b3e3f] | 12 | from django.shortcuts import ( |
---|
[a4764f9] | 13 | redirect |
---|
[94b3e3f] | 14 | ) |
---|
[5d88f36] | 15 | from django.views import View |
---|
[27c0bd4] | 16 | from django.views.generic import ( |
---|
| 17 | ListView, FormView |
---|
| 18 | ) |
---|
| 19 | from django.views.generic.detail import DetailView |
---|
[8ec6ee3] | 20 | |
---|
[8fa24c3] | 21 | from multi_form_view import MultiModelFormView |
---|
[bf47591] | 22 | |
---|
[8fa24c3] | 23 | from .forms import * |
---|
| 24 | from participantes.forms import ( |
---|
| 25 | FormsetParticipanteEvento |
---|
| 26 | ) |
---|
[66158c7] | 27 | from .models import Evento |
---|
[b8fffff] | 28 | from participantes.models import ( |
---|
| 29 | Participante, ParticipanteEvento |
---|
| 30 | ) |
---|
[8fa24c3] | 31 | |
---|
[8ec6ee3] | 32 | |
---|
[a4764f9] | 33 | def handle_uploaded_file(file, name): |
---|
| 34 | with open('%s/%s' % (settings.TMP, name), 'wb+') as destination: |
---|
| 35 | for chunk in file.chunks(): |
---|
| 36 | destination.write(chunk) |
---|
| 37 | |
---|
[b8fffff] | 38 | class RegisterEvent(LoginRequiredMixin, FormView): |
---|
[8fa24c3] | 39 | """! |
---|
| 40 | Muestra el formulario de registro de usuarios |
---|
| 41 | |
---|
| 42 | @author Ing. Leonel P. Hernandez M. (lhernandez at cenditel.gob.ve) |
---|
[5dea5ff] | 43 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[8fa24c3] | 44 | @date 20-11-2017 |
---|
| 45 | @version 1.0.0 |
---|
| 46 | """ |
---|
[94b3e3f] | 47 | |
---|
[8fa24c3] | 48 | template_name = "register.event.html" |
---|
[94b3e3f] | 49 | form_class = EventoForm |
---|
| 50 | form_participante = FormsetParticipanteEvento |
---|
| 51 | success_url = reverse_lazy('base:inicio') |
---|
| 52 | |
---|
| 53 | def get_context_data(self, **kwargs): |
---|
| 54 | context = super(RegisterEvent, self).get_context_data(**kwargs) |
---|
| 55 | if 'form' not in context: |
---|
| 56 | context['form'] = self.form_class() |
---|
| 57 | if 'form2' not in context: |
---|
[45f784a] | 58 | context['form2'] = self.form_participante(queryset=Participante.objects.none()) |
---|
[94b3e3f] | 59 | return context |
---|
| 60 | |
---|
| 61 | def post(self, request, *args, **kwargs): |
---|
[a4764f9] | 62 | file = request.FILES['file'] |
---|
| 63 | handle_uploaded_file(request.FILES['file'], file) |
---|
| 64 | ruta = '%s/%s' % (settings.TMP, file) |
---|
[61f3a75] | 65 | file = open(ruta, 'rb') |
---|
| 66 | files = {'file': file} |
---|
[a4764f9] | 67 | try: |
---|
[11044a5] | 68 | r = requests.post('https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/cargar', verify=False, headers={'Authorization': 'Basic YWRtaW46YWRtaW4='}, files=files) |
---|
[a4764f9] | 69 | nuevo_participante = self.form_participante(request.POST) |
---|
| 70 | consulta_api = r.json()['fileId'] |
---|
| 71 | # elimina el archivo si fue creado en la carpeta tmp |
---|
[61f3a75] | 72 | file.close() |
---|
[a4764f9] | 73 | os.unlink(ruta) |
---|
| 74 | except Exception as e: |
---|
| 75 | print (e) |
---|
[61f3a75] | 76 | file.close() |
---|
| 77 | os.unlink(ruta) |
---|
[a4764f9] | 78 | messages.error(self.request, "Error al concetar al servidor y subir\ |
---|
| 79 | el archivo a la api Murachi") |
---|
| 80 | return redirect(self.success_url) |
---|
| 81 | try: |
---|
| 82 | if self.form_class(request.POST).is_valid() and nuevo_participante.is_valid(): |
---|
[d285c11] | 83 | nuevo_evento = self.form_class(request.POST, request.FILES).save(commit=False) |
---|
[a4764f9] | 84 | nuevo_evento.serial = consulta_api |
---|
| 85 | nuevo_evento.save() |
---|
| 86 | # Control para guardar y asignar participantes al evento |
---|
| 87 | for form in nuevo_participante: |
---|
| 88 | if form.cleaned_data.get('DELETE') and form.instance.pk: |
---|
| 89 | form.instance.delete() |
---|
| 90 | else: |
---|
| 91 | instance = form.save(commit=False) |
---|
| 92 | parametros = { |
---|
| 93 | 'nombres': instance.nombres, |
---|
| 94 | 'apellidos': instance.apellidos, |
---|
| 95 | 'correo': instance.correo |
---|
| 96 | } |
---|
| 97 | nuevo_participante, create = Participante.objects.update_or_create(pasaporte=instance.pasaporte, defaults=parametros) |
---|
| 98 | asigna_evento = ParticipanteEvento( |
---|
| 99 | fk_participante=nuevo_participante, |
---|
| 100 | fk_evento=nuevo_evento) |
---|
| 101 | asigna_evento.save() |
---|
| 102 | messages.success(self.request, "El usaurio %s, ha creado con exito,\ |
---|
| 103 | un nuevo envento %s" % |
---|
| 104 | (str(self.request.user), |
---|
| 105 | str(nuevo_evento))) |
---|
| 106 | else: |
---|
| 107 | messages.error(self.request, "Existe un error en el\ |
---|
| 108 | Formualario %s %s" % |
---|
| 109 | (self.form_class(request.POST).errors, |
---|
| 110 | self.form_participante(request.POST).errors)) |
---|
| 111 | except Exception as e: |
---|
| 112 | print (e) |
---|
| 113 | messages.error(self.request, "Esta intentado realizar una\ |
---|
| 114 | accion incorrecta") |
---|
| 115 | |
---|
[94b3e3f] | 116 | return redirect(self.success_url) |
---|
[66158c7] | 117 | |
---|
| 118 | |
---|
| 119 | class ListEvent(ListView): |
---|
| 120 | """! |
---|
| 121 | Muestra el listado de eventos |
---|
| 122 | |
---|
| 123 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
[5dea5ff] | 124 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[66158c7] | 125 | @date 20-11-2017 |
---|
| 126 | @version 1.0.0 |
---|
| 127 | """ |
---|
| 128 | model = Evento |
---|
| 129 | template_name = "evento.list.html" |
---|
[248b1b4] | 130 | paginate_by = 5 |
---|
[5d88f36] | 131 | |
---|
| 132 | |
---|
[248b1b4] | 133 | class SignEvent(FormView): |
---|
| 134 | """! |
---|
| 135 | Muestra el formulario para buscar y luego firmar documento |
---|
| 136 | |
---|
| 137 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
[5dea5ff] | 138 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[248b1b4] | 139 | @date 20-11-2017 |
---|
| 140 | @version 1.0.0 |
---|
| 141 | """ |
---|
| 142 | form_class = FirmaEventoForm |
---|
[ae65f8e] | 143 | template_name = "evento.firma.html" |
---|
[5d88f36] | 144 | |
---|
[ae65f8e] | 145 | def get_context_data(self, **kwargs): |
---|
| 146 | """! |
---|
| 147 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
[5d88f36] | 148 | |
---|
[ae65f8e] | 149 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
| 150 | @copyright GNU/GPLv3 |
---|
| 151 | @date 20-11-2017 |
---|
| 152 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
| 153 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
| 154 | @return Retorna los datos de contexto |
---|
| 155 | """ |
---|
| 156 | kwargs['nombre_evento'] = Evento.objects.get(pk=int(self.kwargs['pk'])) |
---|
[16cf088] | 157 | return super(SignEvent, self).get_context_data(**kwargs) |
---|
[27c0bd4] | 158 | |
---|
| 159 | |
---|
| 160 | class DetailEvent(DetailView): |
---|
| 161 | """! |
---|
| 162 | Muestra el detalle del evento |
---|
| 163 | |
---|
| 164 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
| 165 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
| 166 | @date 20-11-2017 |
---|
| 167 | @version 1.0.0 |
---|
| 168 | """ |
---|
| 169 | model = Evento |
---|
| 170 | template_name = "evento.detail.html" |
---|
| 171 | |
---|
| 172 | def get_context_data(self, **kwargs): |
---|
| 173 | evento = int(self.kwargs['pk']) |
---|
| 174 | context = super(DetailEvent, self).get_context_data(**kwargs) |
---|
| 175 | try: |
---|
| 176 | participante_evento = ParticipanteEvento.objects.select_related().filter(fk_evento=evento) |
---|
| 177 | falta_porfirma = participante_evento.filter(firma=False).count() |
---|
| 178 | except Exception as e: |
---|
| 179 | print(e) |
---|
| 180 | participante_evento = None |
---|
| 181 | falta_porfirma = None |
---|
| 182 | context['participantes'] = participante_evento |
---|
| 183 | context['num_firma'] = falta_porfirma |
---|
| 184 | return context |
---|
[5d88f36] | 185 | |
---|
| 186 | |
---|
| 187 | class EventoProcesado(View): |
---|
| 188 | """! |
---|
| 189 | Clase que permite consultar si el evento se encuentra disponible para firmar |
---|
| 190 | |
---|
| 191 | @author Ing. Leonel P. Hernandez M. (lhernandez at cenditel.gob.ve) |
---|
| 192 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
| 193 | @date 27-11-2017 |
---|
| 194 | @version 1.0.0 |
---|
| 195 | """ |
---|
| 196 | model = Evento |
---|
| 197 | |
---|
| 198 | def get(self, request): |
---|
| 199 | """! |
---|
| 200 | Metodo que permite verificar si el documento esta procesado |
---|
| 201 | |
---|
| 202 | @author Leonel P. Hernandez M (lhernandez at cenditel.gob.ve) |
---|
| 203 | @copyright GNU/GPLv3 |
---|
| 204 | @date 27-11-2017 |
---|
| 205 | @param request <b>{object}</b> Objeto que contiene la petición |
---|
| 206 | @return Retorna un Json con la respuesta |
---|
| 207 | """ |
---|
| 208 | evento_id = request.GET.get('event_id', None) |
---|
| 209 | mensaje = '' |
---|
| 210 | procesando = False |
---|
| 211 | if evento_id is not None: |
---|
| 212 | try: |
---|
| 213 | evento_pro = self.model.objects.get(pk=evento_id) |
---|
| 214 | except: |
---|
| 215 | print(e) |
---|
| 216 | procesando = True |
---|
| 217 | mensaje += 'No se encuentra un evento con ese serial' |
---|
| 218 | else: |
---|
| 219 | evento_pro = None |
---|
| 220 | procesando = True |
---|
| 221 | mensaje += 'No puedes enviar un evento vacio' |
---|
| 222 | |
---|
| 223 | if evento_pro.procesando: |
---|
| 224 | procesando = True |
---|
| 225 | mensaje += 'No puedes firmar el documento, en este momento\ |
---|
| 226 | se encuentra ocupado por otro Usuario' |
---|
| 227 | else: |
---|
| 228 | procesando = False |
---|
| 229 | mensaje += 'Puedes Firmar el Documento' |
---|
| 230 | data = {'validate': procesando, 'mensaje': mensaje} |
---|
| 231 | return JsonResponse(data, safe=False) |
---|
| 232 | |
---|
| 233 | def post(self, request): |
---|
| 234 | """! |
---|
| 235 | Metodo que permite cambiar el valor procesado al evento |
---|
| 236 | |
---|
| 237 | @author Leonel P. Hernandez M (lhernandez at cenditel.gob.ve) |
---|
| 238 | @copyright GNU/GPLv3 |
---|
| 239 | @date 27-11-2017 |
---|
| 240 | @param request <b>{object}</b> Objeto que contiene la petición |
---|
| 241 | @return Retorna un Json con la respuesta |
---|
| 242 | """ |
---|
| 243 | evento_id = request.POST.get('event_id', None) |
---|
| 244 | if evento_id is not None: |
---|
| 245 | try: |
---|
| 246 | evento = self.model.objects.get(pk=evento_id) |
---|
| 247 | evento.procesando = not evento.procesando |
---|
| 248 | evento.save() |
---|
| 249 | validado = True |
---|
| 250 | except: |
---|
| 251 | print(e) |
---|
| 252 | validado = False |
---|
| 253 | return JsonResponse(validado, safe=False) |
---|