[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 |
---|
| 11 | from django.shortcuts import ( |
---|
[a4764f9] | 12 | redirect |
---|
[94b3e3f] | 13 | ) |
---|
[27c0bd4] | 14 | from django.views.generic import ( |
---|
| 15 | ListView, FormView |
---|
| 16 | ) |
---|
| 17 | from django.views.generic.detail import DetailView |
---|
[8ec6ee3] | 18 | |
---|
[8fa24c3] | 19 | from multi_form_view import MultiModelFormView |
---|
[bf47591] | 20 | |
---|
[8fa24c3] | 21 | from .forms import * |
---|
| 22 | from participantes.forms import ( |
---|
| 23 | FormsetParticipanteEvento |
---|
| 24 | ) |
---|
[66158c7] | 25 | from .models import Evento |
---|
[b8fffff] | 26 | from participantes.models import ( |
---|
| 27 | Participante, ParticipanteEvento |
---|
| 28 | ) |
---|
[8fa24c3] | 29 | |
---|
[8ec6ee3] | 30 | |
---|
[a4764f9] | 31 | def handle_uploaded_file(file, name): |
---|
| 32 | with open('%s/%s' % (settings.TMP, name), 'wb+') as destination: |
---|
| 33 | for chunk in file.chunks(): |
---|
| 34 | destination.write(chunk) |
---|
| 35 | |
---|
[b8fffff] | 36 | class RegisterEvent(LoginRequiredMixin, FormView): |
---|
[8fa24c3] | 37 | """! |
---|
| 38 | Muestra el formulario de registro de usuarios |
---|
| 39 | |
---|
| 40 | @author Ing. Leonel P. Hernandez M. (lhernandez at cenditel.gob.ve) |
---|
[5dea5ff] | 41 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[8fa24c3] | 42 | @date 20-11-2017 |
---|
| 43 | @version 1.0.0 |
---|
| 44 | """ |
---|
[94b3e3f] | 45 | |
---|
[8fa24c3] | 46 | template_name = "register.event.html" |
---|
[94b3e3f] | 47 | form_class = EventoForm |
---|
| 48 | form_participante = FormsetParticipanteEvento |
---|
| 49 | success_url = reverse_lazy('base:inicio') |
---|
| 50 | |
---|
| 51 | def get_context_data(self, **kwargs): |
---|
| 52 | context = super(RegisterEvent, self).get_context_data(**kwargs) |
---|
| 53 | if 'form' not in context: |
---|
| 54 | context['form'] = self.form_class() |
---|
| 55 | if 'form2' not in context: |
---|
[45f784a] | 56 | context['form2'] = self.form_participante(queryset=Participante.objects.none()) |
---|
[94b3e3f] | 57 | return context |
---|
| 58 | |
---|
| 59 | def post(self, request, *args, **kwargs): |
---|
[a4764f9] | 60 | file = request.FILES['file'] |
---|
| 61 | handle_uploaded_file(request.FILES['file'], file) |
---|
| 62 | ruta = '%s/%s' % (settings.TMP, file) |
---|
[61f3a75] | 63 | file = open(ruta, 'rb') |
---|
| 64 | files = {'file': file} |
---|
[a4764f9] | 65 | try: |
---|
[11044a5] | 66 | r = requests.post('https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/cargar', verify=False, headers={'Authorization': 'Basic YWRtaW46YWRtaW4='}, files=files) |
---|
[a4764f9] | 67 | nuevo_participante = self.form_participante(request.POST) |
---|
| 68 | consulta_api = r.json()['fileId'] |
---|
| 69 | # elimina el archivo si fue creado en la carpeta tmp |
---|
[61f3a75] | 70 | file.close() |
---|
[a4764f9] | 71 | os.unlink(ruta) |
---|
| 72 | except Exception as e: |
---|
| 73 | print (e) |
---|
[61f3a75] | 74 | file.close() |
---|
| 75 | os.unlink(ruta) |
---|
[a4764f9] | 76 | messages.error(self.request, "Error al concetar al servidor y subir\ |
---|
| 77 | el archivo a la api Murachi") |
---|
| 78 | return redirect(self.success_url) |
---|
| 79 | try: |
---|
| 80 | if self.form_class(request.POST).is_valid() and nuevo_participante.is_valid(): |
---|
[d285c11] | 81 | nuevo_evento = self.form_class(request.POST, request.FILES).save(commit=False) |
---|
[a4764f9] | 82 | nuevo_evento.serial = consulta_api |
---|
| 83 | nuevo_evento.save() |
---|
| 84 | # Control para guardar y asignar participantes al evento |
---|
| 85 | for form in nuevo_participante: |
---|
| 86 | if form.cleaned_data.get('DELETE') and form.instance.pk: |
---|
| 87 | form.instance.delete() |
---|
| 88 | else: |
---|
| 89 | instance = form.save(commit=False) |
---|
| 90 | parametros = { |
---|
| 91 | 'nombres': instance.nombres, |
---|
| 92 | 'apellidos': instance.apellidos, |
---|
| 93 | 'correo': instance.correo |
---|
| 94 | } |
---|
| 95 | nuevo_participante, create = Participante.objects.update_or_create(pasaporte=instance.pasaporte, defaults=parametros) |
---|
| 96 | asigna_evento = ParticipanteEvento( |
---|
| 97 | fk_participante=nuevo_participante, |
---|
| 98 | fk_evento=nuevo_evento) |
---|
| 99 | asigna_evento.save() |
---|
| 100 | messages.success(self.request, "El usaurio %s, ha creado con exito,\ |
---|
| 101 | un nuevo envento %s" % |
---|
| 102 | (str(self.request.user), |
---|
| 103 | str(nuevo_evento))) |
---|
| 104 | else: |
---|
| 105 | messages.error(self.request, "Existe un error en el\ |
---|
| 106 | Formualario %s %s" % |
---|
| 107 | (self.form_class(request.POST).errors, |
---|
| 108 | self.form_participante(request.POST).errors)) |
---|
| 109 | except Exception as e: |
---|
| 110 | print (e) |
---|
| 111 | messages.error(self.request, "Esta intentado realizar una\ |
---|
| 112 | accion incorrecta") |
---|
| 113 | |
---|
[94b3e3f] | 114 | return redirect(self.success_url) |
---|
[66158c7] | 115 | |
---|
| 116 | |
---|
| 117 | class ListEvent(ListView): |
---|
| 118 | """! |
---|
| 119 | Muestra el listado de eventos |
---|
| 120 | |
---|
| 121 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
[5dea5ff] | 122 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[66158c7] | 123 | @date 20-11-2017 |
---|
| 124 | @version 1.0.0 |
---|
| 125 | """ |
---|
| 126 | model = Evento |
---|
| 127 | template_name = "evento.list.html" |
---|
[248b1b4] | 128 | paginate_by = 5 |
---|
| 129 | |
---|
| 130 | class SignEvent(FormView): |
---|
| 131 | """! |
---|
| 132 | Muestra el formulario para buscar y luego firmar documento |
---|
| 133 | |
---|
| 134 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
[5dea5ff] | 135 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
[248b1b4] | 136 | @date 20-11-2017 |
---|
| 137 | @version 1.0.0 |
---|
| 138 | """ |
---|
| 139 | form_class = FirmaEventoForm |
---|
[ae65f8e] | 140 | template_name = "evento.firma.html" |
---|
| 141 | |
---|
| 142 | def get_context_data(self, **kwargs): |
---|
| 143 | """! |
---|
| 144 | Metodo que permite cargar de nuevo valores en los datos de contexto de la vista |
---|
| 145 | |
---|
| 146 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
| 147 | @copyright GNU/GPLv3 |
---|
| 148 | @date 20-11-2017 |
---|
| 149 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
| 150 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
| 151 | @return Retorna los datos de contexto |
---|
| 152 | """ |
---|
| 153 | kwargs['nombre_evento'] = Evento.objects.get(pk=int(self.kwargs['pk'])) |
---|
[16cf088] | 154 | return super(SignEvent, self).get_context_data(**kwargs) |
---|
[27c0bd4] | 155 | |
---|
| 156 | |
---|
| 157 | class DetailEvent(DetailView): |
---|
| 158 | """! |
---|
| 159 | Muestra el detalle del evento |
---|
| 160 | |
---|
| 161 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
| 162 | @copyright <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU Public License versión 3 (GPLv3)</a> |
---|
| 163 | @date 20-11-2017 |
---|
| 164 | @version 1.0.0 |
---|
| 165 | """ |
---|
| 166 | model = Evento |
---|
| 167 | template_name = "evento.detail.html" |
---|
| 168 | |
---|
| 169 | def get_context_data(self, **kwargs): |
---|
| 170 | evento = int(self.kwargs['pk']) |
---|
| 171 | context = super(DetailEvent, self).get_context_data(**kwargs) |
---|
| 172 | try: |
---|
| 173 | participante_evento = ParticipanteEvento.objects.select_related().filter(fk_evento=evento) |
---|
| 174 | falta_porfirma = participante_evento.filter(firma=False).count() |
---|
| 175 | except Exception as e: |
---|
| 176 | print(e) |
---|
| 177 | participante_evento = None |
---|
| 178 | falta_porfirma = None |
---|
| 179 | context['participantes'] = participante_evento |
---|
| 180 | context['num_firma'] = falta_porfirma |
---|
| 181 | return context |
---|