Commit 0e90afed8e510313a55bd350bb6d1c44ada648e3
1 parent
9676d27b
Exists in
master
notificações
Showing
5 changed files
with
291 additions
and
42 deletions
Show diff stats
wscacicneo/__init__.py
... | ... | @@ -11,7 +11,7 @@ from pyramid.config import Configurator |
11 | 11 | def main(global_config, **settings): |
12 | 12 | """ This function returns a Pyramid WSGI application. |
13 | 13 | """ |
14 | - | |
14 | + | |
15 | 15 | config.setup(settings) |
16 | 16 | from wscacicneo.security import groupfinder |
17 | 17 | authn_policy = AuthTktAuthenticationPolicy( |
... | ... | @@ -20,7 +20,7 @@ def main(global_config, **settings): |
20 | 20 | cfg = Configurator(settings=settings, root_factory='wscacicneo.models.RootFactory') |
21 | 21 | cfg.set_authentication_policy(authn_policy) |
22 | 22 | cfg.set_authorization_policy(authz_policy) |
23 | - | |
23 | + | |
24 | 24 | cfg.include('pyramid_chameleon') |
25 | 25 | cfg.add_static_view('static', 'static', cache_max_age=3600) |
26 | 26 | cfg.add_route('master', 'master') |
... | ... | @@ -42,7 +42,6 @@ def main(global_config, **settings): |
42 | 42 | cfg.add_route('delete_orgao', 'orgao/delete/{sigla}') |
43 | 43 | cfg.add_route('base_de_dados', 'orgao/base/{sigla}') |
44 | 44 | # |
45 | - | |
46 | 45 | cfg.add_route('user', 'usuario/cadastro') |
47 | 46 | cfg.add_route('post_user', 'post_user') |
48 | 47 | cfg.add_route('put_user', 'put_user') |
... | ... | @@ -51,8 +50,9 @@ def main(global_config, **settings): |
51 | 50 | cfg.add_route('edit_favoritos', 'edit_favoritos') |
52 | 51 | cfg.add_route('listuser', 'usuario/lista') |
53 | 52 | cfg.add_route('delete_user', 'usuario/delete/{matricula}') |
53 | + cfg.add_route('notify', 'lista/notificacoes') | |
54 | + cfg.add_route('post_notify', 'post_notify') | |
54 | 55 | # |
55 | - | |
56 | 56 | cfg.add_route('list', 'list') |
57 | 57 | cfg.add_route('gestao', 'gestao') |
58 | 58 | cfg.add_route('memoria', 'memoria') |
... | ... | @@ -74,7 +74,6 @@ def main(global_config, **settings): |
74 | 74 | cfg.add_route('sobre', 'sobre') |
75 | 75 | cfg.add_route('perfil', 'perfil') |
76 | 76 | cfg.add_route('configapi','configapi') |
77 | - cfg.add_route('notify','notify') | |
78 | 77 | cfg.add_route('processador','processador') |
79 | 78 | cfg.add_route('configcoleta','configcoleta') |
80 | 79 | cfg.add_route('configfav','configfav') |
... | ... | @@ -83,4 +82,4 @@ def main(global_config, **settings): |
83 | 82 | cfg.add_route('confighome','confighome') |
84 | 83 | cfg.add_route('db','db') |
85 | 84 | cfg.scan() |
86 | - return cfg.make_wsgi_app() | |
87 | 85 | \ No newline at end of file |
86 | + return cfg.make_wsgi_app() | ... | ... |
... | ... | @@ -0,0 +1,208 @@ |
1 | +#!/usr/env python | |
2 | +# -*- coding: utf-8 -*- | |
3 | +__author__ = 'adley' | |
4 | + | |
5 | +import logging | |
6 | +from requests.exceptions import HTTPError | |
7 | +from wscacicneo import config | |
8 | +from liblightbase.lbbase.struct import Base, BaseMetadata | |
9 | +from liblightbase.lbbase.lbstruct.group import * | |
10 | +from liblightbase.lbbase.lbstruct.field import * | |
11 | +from liblightbase.lbbase.content import Content | |
12 | +from liblightbase.lbrest.base import BaseREST | |
13 | +from liblightbase.lbrest.document import DocumentREST | |
14 | +from liblightbase.lbutils import conv | |
15 | +from liblightbase.lbsearch.search import Search, OrderBy | |
16 | + | |
17 | +log = logging.getLogger() | |
18 | + | |
19 | +class NotifyBase(): | |
20 | + """ | |
21 | + Classe para a base de usuários | |
22 | + """ | |
23 | + def __init__(self, rest_url=None): | |
24 | + """ | |
25 | + Método construtor | |
26 | + """ | |
27 | + if rest_url is None: | |
28 | + self.rest_url = config.REST_URL | |
29 | + else: | |
30 | + self.rest_url = rest_url | |
31 | + self.baserest = BaseREST(rest_url=self.rest_url, response_object=True) | |
32 | + self.documentrest = DocumentREST(rest_url=self.rest_url, | |
33 | + base=self.lbbase, response_object=False) | |
34 | + | |
35 | + @property | |
36 | + def lbbase(self): | |
37 | + """ | |
38 | + LB Base de Notify | |
39 | + """ | |
40 | + orgao = Field(**dict( | |
41 | + name='orgao', | |
42 | + description='Órgão referente a notrificção', | |
43 | + alias='orgao', | |
44 | + datatype='Text', | |
45 | + indices=['Textual'], | |
46 | + multivalued=False, | |
47 | + required=True | |
48 | + )) | |
49 | + | |
50 | + id_coleta = Field(**dict( | |
51 | + name='id_coleta', | |
52 | + alias='id_coleta', | |
53 | + description='id da coleta notificada', | |
54 | + datatype='Text', | |
55 | + indices=['Textual'], | |
56 | + multivalued=False, | |
57 | + required=True | |
58 | + )) | |
59 | + | |
60 | + notify = Field(**dict( | |
61 | + name='notify', | |
62 | + alias='notify', | |
63 | + description='campo de notificações', | |
64 | + datatype='Text', | |
65 | + indices=['Textual'], | |
66 | + multivalued=False, | |
67 | + required=True | |
68 | + )) | |
69 | + | |
70 | + status = Field(**dict( | |
71 | + name='status', | |
72 | + alias='status', | |
73 | + description='status da notificação', | |
74 | + datatype='Text', | |
75 | + indices=['Textual'], | |
76 | + multivalued=False, | |
77 | + required=True | |
78 | + )) | |
79 | + | |
80 | + base_metadata = BaseMetadata( | |
81 | + name='notify', | |
82 | + ) | |
83 | + | |
84 | + content_list = Content() | |
85 | + content_list.append(orgao) | |
86 | + content_list.append(id_coleta) | |
87 | + content_list.append(notify) | |
88 | + content_list.append(status) | |
89 | + | |
90 | + lbbase = Base( | |
91 | + metadata=base_metadata, | |
92 | + content=content_list | |
93 | + ) | |
94 | + | |
95 | + return lbbase | |
96 | + | |
97 | + @property | |
98 | + def metaclass(self): | |
99 | + """ | |
100 | + Retorna metaclass para essa base | |
101 | + """ | |
102 | + return self.lbbase.metaclass() | |
103 | + | |
104 | + def create_base(self): | |
105 | + """ | |
106 | + Cria base no LB | |
107 | + """ | |
108 | + response = self.baserest.create(self.lbbase) | |
109 | + if response.status_code == 200: | |
110 | + return self.lbbase | |
111 | + else: | |
112 | + return None | |
113 | + | |
114 | + def remove_base(self): | |
115 | + """ | |
116 | + Remove base from Lightbase | |
117 | + :param lbbase: LBBase object instance | |
118 | + :return: True or Error if base was not excluded | |
119 | + """ | |
120 | + response = self.baserest.delete(self.lbbase) | |
121 | + if response.status_code == 200: | |
122 | + return True | |
123 | + else: | |
124 | + raise IOError('Error excluding base from LB') | |
125 | + | |
126 | + | |
127 | + | |
128 | +notify_base = NotifyBase() | |
129 | + | |
130 | + | |
131 | +class Notify(notify_base.metaclass): | |
132 | + """ | |
133 | + Classe genérica de órgãos | |
134 | + """ | |
135 | + def __init__(self, **args): | |
136 | + super(Orgao, self).__init__(**args) | |
137 | + self.documentrest = notify_base.documentrest | |
138 | + | |
139 | + def notify_to_dict(self): | |
140 | + """ | |
141 | + Convert status object to Python dict | |
142 | + :return: | |
143 | + """ | |
144 | + | |
145 | + return conv.document2dict(notify_base.lbbase, self) | |
146 | + | |
147 | + def notify_to_json(self): | |
148 | + """ | |
149 | + Convert object to json | |
150 | + :return: | |
151 | + """ | |
152 | + | |
153 | + return conv.document2json(notify_base.lbbase, self) | |
154 | + | |
155 | + def create_notify(self): | |
156 | + """ | |
157 | + Insert document on base | |
158 | + | |
159 | + :return: Document creation ID | |
160 | + """ | |
161 | + | |
162 | + document = self.notify_to_json() | |
163 | + try: | |
164 | + result = notify_base.documentrest.create(document) | |
165 | + except HTTPError as err: | |
166 | + log.error(err.strerror) | |
167 | + return None | |
168 | + | |
169 | + return result | |
170 | + | |
171 | + def search_notify(self, sigla): | |
172 | + """ | |
173 | + Busca registro completo do notify pelo id | |
174 | + :return: obj collection com os dados da base | |
175 | + """ | |
176 | + search = Search( | |
177 | + literal="document->>'sigla' = '"+orgao+"'" | |
178 | + ) | |
179 | + results = self.documentrest.get_collection(search_obj=search) | |
180 | + | |
181 | + return results | |
182 | + | |
183 | + def search_list_notify(self): | |
184 | + """ | |
185 | + Retorna todos os docs da base | |
186 | + """ | |
187 | + search = Search( | |
188 | + limit=None | |
189 | + ) | |
190 | + results = self.documentrest.get_collection(search) | |
191 | + | |
192 | + return results | |
193 | + | |
194 | + def edit_notify(self, id, doc): | |
195 | + """ | |
196 | + altera um doc ou path do doc | |
197 | + """ | |
198 | + results = self.documentrest.update(id, doc) | |
199 | + | |
200 | + return results | |
201 | + | |
202 | + def delete_notify(self, id): | |
203 | + """ | |
204 | + Deleta o Órgao apartir do ID | |
205 | + """ | |
206 | + results = orgao_base.documentrest.delete(id) | |
207 | + | |
208 | + return results | ... | ... |
wscacicneo/templates/notify_coleta.pt
1 | 1 | <metal:main use-macro="load: master.pt"> |
2 | 2 | <metal:content fill-slot="conteudo"> |
3 | - <div class="widget wgreen"> | |
4 | - <div class="widget-content"> | |
5 | - <div class="padd"> | |
6 | - <form class="form-horizontal" role="form"> | |
7 | - <div class="form-group"> | |
8 | - <label class="col-lg-2 control-label">Ministério</label> | |
9 | - <div class="col-lg-5"> | |
10 | - <input type="text" id="ministerio" class="form-control" placeholder="Ministério"> | |
11 | - </div> | |
12 | - </div> | |
13 | - <div class="form-group"> | |
14 | - <label class="col-lg-2 control-label">ID da Coleta</label> | |
3 | + <div class="widget-content"> | |
4 | + <div class="padd"> | |
5 | + <form class="form-horizontal" style="margin-top: 20px;" role="form"> | |
6 | + <div class="form-group"> | |
7 | + <label class="col-lg-2 control-label">Ministério</label> | |
15 | 8 | <div class="col-lg-5"> |
16 | - <input type="password" id="id_coleta" class="form-control" placeholder="ID"> | |
17 | - </div> | |
9 | + <input type="text" id="orgao" class="form-control" placeholder="Ministério"> | |
18 | 10 | </div> |
19 | - <div class="form-group"> | |
20 | - <label class="col-lg-2 control-label">Notificação</label> | |
21 | - <div class="col-lg-5"> | |
22 | - <textarea class="form-control" id="notify" rows="5" placeholder="Conteudo" style="margin: 0px -57.671875px 0px 0px; width: 423px; height: 104px;"></textarea> | |
23 | - </div> | |
11 | + </div> | |
12 | + <div class="form-group"> | |
13 | + <label class="col-lg-2 control-label">ID da Coleta</label> | |
14 | + <div class="col-lg-5"> | |
15 | + <input type="password" id="id_coleta" class="form-control" placeholder="ID"> | |
16 | + </div> | |
17 | + </div> | |
18 | + <div class="form-group"> | |
19 | + <label class="col-lg-2 control-label">Notificação</label> | |
20 | + <div class="col-lg-5"> | |
21 | + <textarea class="form-control" id="notify" rows="5" placeholder="Conteudo" style="margin: 0px -57.671875px 0px 0px; width: 423px; height: 104px;"></textarea> | |
24 | 22 | </div> |
25 | - <div class="form-group"> | |
26 | - <div class="col-lg-offset-2 col-lg-6"> | |
27 | - <button type="button" id="enviar" class="btn btn-sm btn-default">Enviar</button> | |
28 | - <button type="button" class="btn btn-sm btn-default">Cancelar</button> | |
29 | - </div> | |
23 | + </div> | |
24 | + <div class="form-group"> | |
25 | + <div class="col-lg-offset-2 col-lg-6"> | |
26 | + <button type="button" id="enviar" class="btn btn-sm btn-default">Enviar</button> | |
27 | + <button type="button" class="btn btn-sm btn-default">Cancelar</button> | |
30 | 28 | </div> |
31 | - </form> | |
32 | - </div> | |
29 | + </div> | |
30 | + </form> | |
33 | 31 | </div> |
34 | 32 | </div> |
35 | 33 | </metal:content> |
36 | 34 | <metal:content fill-slot="javascript"> |
37 | 35 | <script type="text/javascript"> |
38 | 36 | $('#enviar').click(function(){ |
37 | + alert('asdasdasd') | |
38 | + var data = { | |
39 | + 'orgao' : $('#orgao').val(), | |
40 | + 'id_coleta' : $('#id_coleta').val(), | |
41 | + 'notify' : $('#notify').val(), | |
42 | + 'status' : True | |
43 | + } | |
44 | + $.ajax({ | |
45 | + type: "POST", | |
46 | + url: "${request.route_url('root')}post_notify", | |
47 | + data: data, | |
48 | + success: function(){ alert('asdasdasd') }, | |
49 | + error: function(){ alert('Não foi desta vez Tente novamente mais tarde') }, | |
50 | + }); | |
39 | 51 | }); |
40 | 52 | </script> |
41 | 53 | </metal:content> | ... | ... |
... | ... | @@ -0,0 +1,31 @@ |
1 | +#!/usr/env python | |
2 | +# -*- coding: utf-8 -*- | |
3 | +__author__ = 'macieski' | |
4 | + | |
5 | +import unittest | |
6 | +import configparser | |
7 | +import os | |
8 | +from wscacicneo.model import notify | |
9 | +from liblightbase.lbbase.struct import Base | |
10 | +from liblightbase.lbutils import conv | |
11 | +from pyramid import testing | |
12 | + | |
13 | +class NotifyBase(unittest.TestCase): | |
14 | + | |
15 | + | |
16 | + def setUp(self): | |
17 | + self.rest_url = 'http://api.brlight.net/api' | |
18 | + | |
19 | + def test_create_base(self): | |
20 | + """ | |
21 | + Testa criação da base no LB | |
22 | + """ | |
23 | + notify_base = notify.NotifyBase(self.rest_url) | |
24 | + lbbase = notify_base.lbbase | |
25 | + self.assertIsInstance(lbbase, Base) | |
26 | + | |
27 | + retorno = notify_base.create_base() | |
28 | + self.assertIsInstance(retorno, Base) | |
29 | + | |
30 | + def tearDown(self): | |
31 | + pass | ... | ... |
wscacicneo/views.py
... | ... | @@ -66,8 +66,8 @@ def graficop(request): |
66 | 66 | def gestor(request): |
67 | 67 | return {'project': 'WSCacicNeo'} |
68 | 68 | |
69 | -@view_config(route_name='notifications', renderer='templates/dashboard.pt') | |
70 | -def notifications(request): | |
69 | +@view_config(route_name='notify', renderer='templates/notify_coleta.pt') | |
70 | +def notify(request): | |
71 | 71 | return {'project': 'WSCacicNeo'} |
72 | 72 | |
73 | 73 | @view_config(route_name='admin', renderer='templates/admin.pt') |
... | ... | @@ -86,10 +86,6 @@ def cadastro(request): |
86 | 86 | def orgao(request): |
87 | 87 | return {'project': 'WSCacicNeo'} |
88 | 88 | |
89 | -@view_config(route_name='notify_coleta', renderer='templates/notify_coleta.pt') | |
90 | -def notify_coleta(request): | |
91 | - return {'project': 'WSCacicNeo'} | |
92 | - | |
93 | 89 | @view_config(route_name='listorgao', renderer='templates/list_orgao.pt') |
94 | 90 | def listorgao(request): |
95 | 91 | orgao_obj = Orgao( |
... | ... | @@ -177,10 +173,6 @@ def editorgao(request): |
177 | 173 | 'url' : search.results[0].url |
178 | 174 | } |
179 | 175 | |
180 | -@view_config(route_name='notify', renderer='templates/notify.pt') | |
181 | -def notify(request): | |
182 | - return {'project': 'WSCacicNeo'} | |
183 | - | |
184 | 176 | @view_config(route_name='configcoleta', renderer='templates/configcoleta.pt') |
185 | 177 | def configcoleta(request): |
186 | 178 | return {'project': 'WSCacicNeo'} |
... | ... | @@ -353,6 +345,11 @@ def edituser(request): |
353 | 345 | 'senha' : search.results[0].senha |
354 | 346 | } |
355 | 347 | |
348 | +@view_config(route_name='post_notify') | |
349 | +def post_notify(request): | |
350 | + print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') | |
351 | + return { } | |
352 | + | |
356 | 353 | @view_config(route_name='put_user') |
357 | 354 | def put_user(request): |
358 | 355 | """ |
... | ... | @@ -514,3 +511,5 @@ def logout(request): |
514 | 511 | headers = forget(request) |
515 | 512 | return HTTPFound(location = request.route_url('login'), |
516 | 513 | headers = headers) |
514 | + | |
515 | + | ... | ... |