1 | """ |
---|
2 | Sistema Estadístico Integral de Venezuela - (SEIVEN) |
---|
3 | |
---|
4 | Copyleft (@) 2015 CENDITEL nodo Mérida - https://mpv.cenditel.gob.ve/seiven |
---|
5 | """ |
---|
6 | ## @namespace api_client.admin |
---|
7 | # |
---|
8 | # Contiene las clases, métodos y funciones que serán parte del panel administrativo |
---|
9 | # @author Ing. Roldan Vargas (rvargas at cenditel.gob.ve) |
---|
10 | # @author <a href='http://www.cenditel.gob.ve'>Centro Nacional de Desarrollo e Investigación en Tecnologías Libres |
---|
11 | # (CENDITEL) nodo Mérida - Venezuela</a> |
---|
12 | # @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
13 | from __future__ import unicode_literals |
---|
14 | |
---|
15 | import json |
---|
16 | |
---|
17 | from django.contrib import admin |
---|
18 | from .models import AccessToken, ConfigAccessToken |
---|
19 | from .forms import ConfigAccessTokenForm |
---|
20 | import requests |
---|
21 | |
---|
22 | __licence__ = "GNU Public License v2" |
---|
23 | __revision__ = "" |
---|
24 | __docstring__ = "DoxyGen" |
---|
25 | |
---|
26 | class ConfigAccessTokenAdmin(admin.ModelAdmin): |
---|
27 | """! |
---|
28 | Clase que gestiona los tokens de acceso para consumir servicios REST |
---|
29 | |
---|
30 | @author Ing. Roldan Vargas (rvargas at cenditel.gob.ve) |
---|
31 | @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a> |
---|
32 | @date 20-10-2017 |
---|
33 | @version 1.0.0 |
---|
34 | """ |
---|
35 | form = ConfigAccessTokenForm |
---|
36 | list_display = ('application_name',) |
---|
37 | list_filter = ('application_name',) |
---|
38 | ordering = ('application_name',) |
---|
39 | search_fields = ('application_name',) |
---|
40 | |
---|
41 | def save_model(self, request, obj, form, change): |
---|
42 | """! |
---|
43 | Método que permite guardar los datos de configuracción en el modelo AccessToken |
---|
44 | |
---|
45 | @author Ing. Roldan Vargas (rvargas at cenditel.gob.ve) |
---|
46 | @copyright GNU/GPLv2 |
---|
47 | @date 13-12-2017 |
---|
48 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
49 | @param request <b>{object}</b> Objeto con los datos |
---|
50 | @param obj <b>{object}</b> Objeto donde se guardan los datos |
---|
51 | @param form <b>{object}</b> Objeto que contiene el formulario de registro |
---|
52 | @param change <b>{object}</b> |
---|
53 | """ |
---|
54 | obj = form.save(commit=False) |
---|
55 | |
---|
56 | data = { |
---|
57 | 'grant_type': 'password', |
---|
58 | 'username': form.cleaned_data['username'], |
---|
59 | 'password': form.cleaned_data['password'] |
---|
60 | } |
---|
61 | params = { |
---|
62 | 'client_secret': form.cleaned_data['client_secret'], |
---|
63 | 'client_id': form.cleaned_data['client_id'] |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | url = obj.url_access if obj.url_access[-1:] == "/" else obj.url_access + "/" |
---|
68 | url_get_token = url + "o/token/" |
---|
69 | token_data = requests.post(url_get_token, data=data, params=params) |
---|
70 | |
---|
71 | result_token = json.loads(token_data.text) |
---|
72 | |
---|
73 | token = AccessToken() |
---|
74 | token.aplicacion = form.cleaned_data['application_name'] |
---|
75 | token.url_access = url |
---|
76 | token.scope = result_token['scope'] |
---|
77 | token.expire_in = result_token['expires_in'] |
---|
78 | token.access_token = result_token['access_token'] |
---|
79 | token.token_type = result_token['token_type'] |
---|
80 | token.refresh_token = result_token['refresh_token'] |
---|
81 | token.activo = True |
---|
82 | token.save() |
---|
83 | |
---|
84 | obj.save() |
---|
85 | |
---|
86 | def delete_model(self, request, obj): |
---|
87 | """! |
---|
88 | Método que permite eliminar los datos de configuracción en el modelo AccessToken |
---|
89 | |
---|
90 | @author Ing. Roldan Vargas (rvargas at cenditel.gob.ve) |
---|
91 | @copyright GNU/GPLv2 |
---|
92 | @date 13-12-2017 |
---|
93 | @param self <b>{object}</b> Objeto que instancia la clase |
---|
94 | @param request <b>{object}</b> Objeto con los datos |
---|
95 | @param obj <b>{object}</b> Objeto donde se guardan los datos |
---|
96 | """ |
---|
97 | AccessToken.objects.filter(aplicacion=obj.application_name).delete() |
---|
98 | |
---|
99 | obj.delete() |
---|
100 | |
---|
101 | |
---|
102 | admin.site.register(ConfigAccessToken, ConfigAccessTokenAdmin) |
---|