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 procesamiento.views |
---|
8 | # |
---|
9 | # Vistas correspondientes a la aplicación procesamiento |
---|
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 | from shutil import copy |
---|
16 | from django.shortcuts import render |
---|
17 | from django.views.generic import ( |
---|
18 | FormView, TemplateView, CreateView, ListView, |
---|
19 | DeleteView, UpdateView |
---|
20 | ) |
---|
21 | from django.core.urlresolvers import reverse_lazy |
---|
22 | from django.http import JsonResponse |
---|
23 | from django.contrib.messages.views import SuccessMessageMixin |
---|
24 | ## Importaciones de la consulta |
---|
25 | from consulta_publica.settings import PROCESAMIENTO_PATH, BASE_DIR |
---|
26 | from consulta.models import Consulta |
---|
27 | ## Importaciones propias |
---|
28 | from .forms import ProcesamientoSelectForm, ProcesamientoForm, ProcesamientoActionForm |
---|
29 | from .models import Procesamiento |
---|
30 | ## Importaciones de los utils |
---|
31 | from utils.freeling import * |
---|
32 | from utils.corpusScript import * |
---|
33 | from utils.run_lda import generate_comand |
---|
34 | |
---|
35 | |
---|
36 | class ProcesamientoIndex(FormView): |
---|
37 | """! |
---|
38 | Clase que gestiona la vista principal del procesamiento |
---|
39 | |
---|
40 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
41 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
42 | @date 28-03-2017 |
---|
43 | @version 1.0.0 |
---|
44 | """ |
---|
45 | template_name = "procesamiento.index.html" |
---|
46 | form_class = ProcesamientoSelectForm |
---|
47 | |
---|
48 | def get_success_url(self): |
---|
49 | """! |
---|
50 | Metodo que permite definir la url de dirección al ser válido el formulario |
---|
51 | |
---|
52 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
53 | @copyright GNU/GPLv2 |
---|
54 | @date 28-03-2017 |
---|
55 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
56 | @return Retorna la url |
---|
57 | """ |
---|
58 | consulta = self.request.POST['consultas'] |
---|
59 | return reverse_lazy('procesamiento_select', |
---|
60 | kwargs={'pk': consulta}) |
---|
61 | |
---|
62 | class ProcesamientoSelect(FormView): |
---|
63 | """! |
---|
64 | Clase que gestiona la vista principal de las opciones del procesamiento |
---|
65 | |
---|
66 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
67 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
68 | @date 28-03-2017 |
---|
69 | @version 1.0.0 |
---|
70 | """ |
---|
71 | template_name = "procesamiento.select.html" |
---|
72 | form_class = ProcesamientoActionForm |
---|
73 | |
---|
74 | def get_context_data(self, **kwargs): |
---|
75 | """! |
---|
76 | Metodo para cargar/obtener valores en el contexto de la vista |
---|
77 | |
---|
78 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
79 | @copyright GNU/GPLv2 |
---|
80 | @date 28-03-2017 |
---|
81 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
82 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
83 | @return Retorna los datos de contexto |
---|
84 | """ |
---|
85 | kwargs['pk'] = self.kwargs['pk'] |
---|
86 | procesamiento = Procesamiento.objects.filter(consulta_id=kwargs['pk']) |
---|
87 | if(procesamiento): |
---|
88 | kwargs['procesamiento'] = True |
---|
89 | return super(ProcesamientoSelect, self).get_context_data(**kwargs) |
---|
90 | |
---|
91 | def form_valid(self,form): |
---|
92 | """! |
---|
93 | Metodo que permite procesar si el formulario es válido |
---|
94 | |
---|
95 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
96 | @copyright GNU/GPLv2 |
---|
97 | @date 29-03-2017 |
---|
98 | @param self <b>{object}</b> Objeto que instancia el método |
---|
99 | @param form <b>{object}</b> Objeto que contiene el formulario |
---|
100 | @return Retorna un redirect a la success_url |
---|
101 | """ |
---|
102 | procesamiento_id = form.cleaned_data['procesamiento'] |
---|
103 | |
---|
104 | procesamiento = Procesamiento.objects.get(pk=procesamiento_id) |
---|
105 | excluded_words = procesamiento.excluded_words |
---|
106 | |
---|
107 | if len(excluded_words)>0: |
---|
108 | excluded_words = excluded_words.split(",") |
---|
109 | |
---|
110 | ## Se crea el directorio del procesamiento |
---|
111 | procesamiento_dir = PROCESAMIENTO_PATH+"/"+procesamiento.procesamiento_dir |
---|
112 | if not os.path.exists(procesamiento_dir): |
---|
113 | os.mkdir(procesamiento_dir) |
---|
114 | |
---|
115 | ## Se preparan los archivos/directorios necesarios para el pre-procesamiento |
---|
116 | self.prepare_files(procesamiento.corpus_dir,procesamiento_dir) |
---|
117 | |
---|
118 | ## Se realiza el freeling y procesamiento con LDA |
---|
119 | self.make_process(procesamiento_dir,procesamiento.words,procesamiento.excluded_words) |
---|
120 | |
---|
121 | ## Se guarda como corrido el procesamiento |
---|
122 | procesamiento.used = True |
---|
123 | procesamiento.save() |
---|
124 | |
---|
125 | if self.request.is_ajax(): |
---|
126 | return JsonResponse({"code":True}) |
---|
127 | else: |
---|
128 | return super(ProcesamientoSelect, self).form_valid(form) |
---|
129 | |
---|
130 | def prepare_files(self,path,dest): |
---|
131 | """! |
---|
132 | Metodo para copiar los corpus del destino al origen y crear los directorios |
---|
133 | |
---|
134 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
135 | @copyright GNU/GPLv2 |
---|
136 | @date 08-02-2017 |
---|
137 | @param self <b>{object}</b> Objeto que instancia el método |
---|
138 | @param path <b>{str}</b> Recibe la ruta de origen |
---|
139 | @param dest <b>{str}</b> Recibe la ruta de destino |
---|
140 | """ |
---|
141 | ## Se crea el directorio orig si no existe |
---|
142 | if not os.path.exists(dest+"/orig/"): |
---|
143 | os.mkdir(dest+"/orig/") |
---|
144 | ## Se obtiene una lista de los archivos en el directorio |
---|
145 | list_files = corpus_files = os.listdir(path) |
---|
146 | ## Se copian los archivos del directorio a la raiz de proyecto |
---|
147 | for item in list_files: |
---|
148 | copy(path+"/"+item, dest+"/orig/") |
---|
149 | ## Se crean los directorios necesarios para el pre-procesamiento |
---|
150 | if not os.path.exists(dest+"/lower/"): |
---|
151 | os.mkdir(dest+"/lower/") |
---|
152 | if not os.path.exists(dest+"/pp/"): |
---|
153 | os.mkdir(dest+"/pp/") |
---|
154 | if not os.path.exists(dest+"/lda/"): |
---|
155 | os.mkdir(dest+"/lda/") |
---|
156 | if not os.path.exists(dest+"/freeling/"): |
---|
157 | os.mkdir(dest+"/freeling/") |
---|
158 | if not os.path.exists(dest+"/noaccent/"): |
---|
159 | os.mkdir(dest+"/noaccent/") |
---|
160 | |
---|
161 | def make_process(self, path, word_list, exluded_words): |
---|
162 | """! |
---|
163 | Metodo para realizar el pre-procesamiento y lda |
---|
164 | |
---|
165 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
166 | @copyright GNU/GPLv2 |
---|
167 | @date 09-02-2017 |
---|
168 | @param self <b>{object}</b> Objeto que instancia el método |
---|
169 | @param path <b>{str}</b> Recibe la ruta de origen del corpus |
---|
170 | @param word_list <b>{list}</b> Recibe una lista palabras a excluir |
---|
171 | @param exluded_words <b>{list}</b> Recibe una lista de las palabras a excluir |
---|
172 | """ |
---|
173 | orig_corpus_path = path + '/orig/' |
---|
174 | lower_corpus_path = path + '/lower/' |
---|
175 | pp_corpus_path = path + '/pp/' |
---|
176 | lda_corpus_path = path + '/lda/' |
---|
177 | |
---|
178 | ## Crea los archivos de puro minusculas |
---|
179 | files_to_lower(orig_corpus_path,lower_corpus_path) |
---|
180 | |
---|
181 | ## Se realiza el pre-procesamiento |
---|
182 | if len(word_list)>1: |
---|
183 | file_words_pp,corpus_words = preprocess(lower_corpus_path,do_fl=True,pos_list=word_list) |
---|
184 | else: |
---|
185 | file_words_pp,corpus_words = preprocess(lower_corpus_path,do_fl=True) |
---|
186 | |
---|
187 | ## Se genera el archivo de vocabulario excluido |
---|
188 | generate_exluded_file(path+"/",pp_corpus_path,file_words_pp,exluded_words) |
---|
189 | |
---|
190 | ## Se generan los archivos .dat |
---|
191 | corpus_script(pp_corpus_path,lda_corpus_path) |
---|
192 | |
---|
193 | ## Se generan los archivos con el LDA |
---|
194 | corpus_dat = lda_corpus_path+'corpus.dat' |
---|
195 | generate_comand(BASE_DIR,corpus_dat,path,9) |
---|
196 | |
---|
197 | class ProcesamientoCreate(SuccessMessageMixin,CreateView): |
---|
198 | """! |
---|
199 | Clase que gestiona la creación de perfiles procesamiento |
---|
200 | |
---|
201 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
202 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
203 | @date 28-03-2017 |
---|
204 | @version 1.0.0 |
---|
205 | """ |
---|
206 | model = Procesamiento |
---|
207 | form_class = ProcesamientoForm |
---|
208 | template_name = "procesamiento.create.html" |
---|
209 | success_message = "Se creó el perfil de procesamiento con éxito" |
---|
210 | success_url = reverse_lazy('procesamiento_index') |
---|
211 | |
---|
212 | def get_context_data(self, **kwargs): |
---|
213 | """! |
---|
214 | Metodo para cargar/obtener valores en el contexto de la vista |
---|
215 | |
---|
216 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
217 | @copyright GNU/GPLv2 |
---|
218 | @date 28-03-2017 |
---|
219 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
220 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
221 | @return Retorna los datos de contexto |
---|
222 | """ |
---|
223 | kwargs['pk'] = self.kwargs['pk'] |
---|
224 | return super(ProcesamientoCreate, self).get_context_data(**kwargs) |
---|
225 | |
---|
226 | def form_valid(self,form): |
---|
227 | """! |
---|
228 | Metodo que al que accede si el formulario es válido |
---|
229 | |
---|
230 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
231 | @copyright GNU/GPLv2 |
---|
232 | @date 28-03-2017 |
---|
233 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
234 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
235 | @return Retorna el formulario validado |
---|
236 | """ |
---|
237 | consulta = Consulta.objects.get(pk=self.kwargs['pk']) |
---|
238 | self.object = form.save(commit=False) |
---|
239 | self.object.procesamiento_dir = form.cleaned_data['procesamiento_dir'] |
---|
240 | self.object.corpus_dir = form.cleaned_data['corpus_dir'] |
---|
241 | self.object.words = form.cleaned_data['words'] |
---|
242 | self.object.excluded_words = form.cleaned_data['excluded_words'] |
---|
243 | self.object.consulta = consulta |
---|
244 | self.object.save() |
---|
245 | return super(ProcesamientoCreate, self).form_valid(form) |
---|
246 | |
---|
247 | |
---|
248 | class ProcesamientoList(ListView): |
---|
249 | """! |
---|
250 | Clase que gestiona la lista de los perfiles procesamiento |
---|
251 | |
---|
252 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
253 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
254 | @date 28-03-2017 |
---|
255 | @version 1.0.0 |
---|
256 | """ |
---|
257 | model = Procesamiento |
---|
258 | template_name = "procesamiento.list.html" |
---|
259 | |
---|
260 | def get_context_data(self, **kwargs): |
---|
261 | """! |
---|
262 | Metodo para cargar/obtener valores en el contexto de la vista |
---|
263 | |
---|
264 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
265 | @copyright GNU/GPLv2 |
---|
266 | @date 28-03-2017 |
---|
267 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
268 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
269 | @return Retorna los datos de contexto |
---|
270 | """ |
---|
271 | kwargs['pk'] = self.kwargs['pk'] |
---|
272 | return super(ProcesamientoList, self).get_context_data(**kwargs) |
---|
273 | |
---|
274 | |
---|
275 | class ProcesamientoDelete(SuccessMessageMixin,DeleteView): |
---|
276 | """! |
---|
277 | Clase que gestiona la borrado de perfiles de procesamiento |
---|
278 | |
---|
279 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
280 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
281 | @date 28-03-2017 |
---|
282 | @version 1.0.0 |
---|
283 | """ |
---|
284 | model = Procesamiento |
---|
285 | template_name = "procesamiento.delete.html" |
---|
286 | success_message = "Se eliminó el perfil de procesamiento con éxito" |
---|
287 | |
---|
288 | def get_context_data(self, **kwargs): |
---|
289 | """! |
---|
290 | Metodo para cargar/obtener valores en el contexto de la vista |
---|
291 | |
---|
292 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
293 | @copyright GNU/GPLv2 |
---|
294 | @date 28-03-2017 |
---|
295 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
296 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
297 | @return Retorna los datos de contexto |
---|
298 | """ |
---|
299 | kwargs['pk'] = self.kwargs['pk_consulta'] |
---|
300 | return super(ProcesamientoDelete, self).get_context_data(**kwargs) |
---|
301 | |
---|
302 | |
---|
303 | def get_success_url(self): |
---|
304 | """! |
---|
305 | Metodo que permite definir la url de dirección al ser válido el formulario |
---|
306 | |
---|
307 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
308 | @copyright GNU/GPLv2 |
---|
309 | @date 28-03-2017 |
---|
310 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
311 | @return Retorna la url |
---|
312 | """ |
---|
313 | pk = self.kwargs['pk_consulta'] |
---|
314 | return reverse_lazy('procesamiento_list', |
---|
315 | kwargs={'pk': pk}) |
---|
316 | |
---|
317 | |
---|
318 | class ProcesamientoUpdate(SuccessMessageMixin,UpdateView): |
---|
319 | """! |
---|
320 | Clase que gestiona la actualización de perfiles de procesamiento |
---|
321 | |
---|
322 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
323 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
324 | @date 28-03-2017 |
---|
325 | @version 1.0.0 |
---|
326 | """ |
---|
327 | model = Procesamiento |
---|
328 | form_class = ProcesamientoForm |
---|
329 | template_name = "procesamiento.update.html" |
---|
330 | success_message = "Se actualizó el perfil de procesamiento con éxito" |
---|
331 | |
---|
332 | def get_context_data(self, **kwargs): |
---|
333 | """! |
---|
334 | Metodo para cargar/obtener valores en el contexto de la vista |
---|
335 | |
---|
336 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
337 | @copyright GNU/GPLv2 |
---|
338 | @date 28-03-2017 |
---|
339 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
340 | @param kwargs <b>{object}</b> Objeto que contiene los datos de contexto |
---|
341 | @return Retorna los datos de contexto |
---|
342 | """ |
---|
343 | kwargs['pk'] = self.kwargs['pk_consulta'] |
---|
344 | return super(ProcesamientoUpdate, self).get_context_data(**kwargs) |
---|
345 | |
---|
346 | def get_success_url(self): |
---|
347 | """! |
---|
348 | Metodo que permite definir la url de dirección al ser válido el formulario |
---|
349 | |
---|
350 | @author Rodrigo Boet (rboet at cenditel.gob.ve) |
---|
351 | @copyright GNU/GPLv2 |
---|
352 | @date 28-03-2017 |
---|
353 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
354 | @return Retorna la url |
---|
355 | """ |
---|
356 | pk = self.kwargs['pk_consulta'] |
---|
357 | return reverse_lazy('procesamiento_list', |
---|
358 | kwargs={'pk': pk}) |
---|
359 | |
---|