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