From 0e90afed8e510313a55bd350bb6d1c44ada648e3 Mon Sep 17 00:00:00 2001 From: eddumm22 Date: Thu, 25 Sep 2014 13:20:46 -0300 Subject: [PATCH] notificações --- wscacicneo/__init__.py | 11 +++++------ wscacicneo/model/notify.py | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ wscacicneo/templates/notify_coleta.pt | 64 ++++++++++++++++++++++++++++++++++++++-------------------------- wscacicneo/test/test_notify_base.py | 31 +++++++++++++++++++++++++++++++ wscacicneo/views.py | 19 +++++++++---------- 5 files changed, 291 insertions(+), 42 deletions(-) create mode 100644 wscacicneo/model/notify.py create mode 100644 wscacicneo/test/test_notify_base.py diff --git a/wscacicneo/__init__.py b/wscacicneo/__init__.py index 5676ca8..bcdec6e 100755 --- a/wscacicneo/__init__.py +++ b/wscacicneo/__init__.py @@ -11,7 +11,7 @@ from pyramid.config import Configurator def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ - + config.setup(settings) from wscacicneo.security import groupfinder authn_policy = AuthTktAuthenticationPolicy( @@ -20,7 +20,7 @@ def main(global_config, **settings): cfg = Configurator(settings=settings, root_factory='wscacicneo.models.RootFactory') cfg.set_authentication_policy(authn_policy) cfg.set_authorization_policy(authz_policy) - + cfg.include('pyramid_chameleon') cfg.add_static_view('static', 'static', cache_max_age=3600) cfg.add_route('master', 'master') @@ -42,7 +42,6 @@ def main(global_config, **settings): cfg.add_route('delete_orgao', 'orgao/delete/{sigla}') cfg.add_route('base_de_dados', 'orgao/base/{sigla}') # - cfg.add_route('user', 'usuario/cadastro') cfg.add_route('post_user', 'post_user') cfg.add_route('put_user', 'put_user') @@ -51,8 +50,9 @@ def main(global_config, **settings): cfg.add_route('edit_favoritos', 'edit_favoritos') cfg.add_route('listuser', 'usuario/lista') cfg.add_route('delete_user', 'usuario/delete/{matricula}') + cfg.add_route('notify', 'lista/notificacoes') + cfg.add_route('post_notify', 'post_notify') # - cfg.add_route('list', 'list') cfg.add_route('gestao', 'gestao') cfg.add_route('memoria', 'memoria') @@ -74,7 +74,6 @@ def main(global_config, **settings): cfg.add_route('sobre', 'sobre') cfg.add_route('perfil', 'perfil') cfg.add_route('configapi','configapi') - cfg.add_route('notify','notify') cfg.add_route('processador','processador') cfg.add_route('configcoleta','configcoleta') cfg.add_route('configfav','configfav') @@ -83,4 +82,4 @@ def main(global_config, **settings): cfg.add_route('confighome','confighome') cfg.add_route('db','db') cfg.scan() - return cfg.make_wsgi_app() \ No newline at end of file + return cfg.make_wsgi_app() diff --git a/wscacicneo/model/notify.py b/wscacicneo/model/notify.py new file mode 100644 index 0000000..33d3adb --- /dev/null +++ b/wscacicneo/model/notify.py @@ -0,0 +1,208 @@ +#!/usr/env python +# -*- coding: utf-8 -*- +__author__ = 'adley' + +import logging +from requests.exceptions import HTTPError +from wscacicneo import config +from liblightbase.lbbase.struct import Base, BaseMetadata +from liblightbase.lbbase.lbstruct.group import * +from liblightbase.lbbase.lbstruct.field import * +from liblightbase.lbbase.content import Content +from liblightbase.lbrest.base import BaseREST +from liblightbase.lbrest.document import DocumentREST +from liblightbase.lbutils import conv +from liblightbase.lbsearch.search import Search, OrderBy + +log = logging.getLogger() + +class NotifyBase(): + """ + Classe para a base de usuários + """ + def __init__(self, rest_url=None): + """ + Método construtor + """ + if rest_url is None: + self.rest_url = config.REST_URL + else: + self.rest_url = rest_url + self.baserest = BaseREST(rest_url=self.rest_url, response_object=True) + self.documentrest = DocumentREST(rest_url=self.rest_url, + base=self.lbbase, response_object=False) + + @property + def lbbase(self): + """ + LB Base de Notify + """ + orgao = Field(**dict( + name='orgao', + description='Órgão referente a notrificção', + alias='orgao', + datatype='Text', + indices=['Textual'], + multivalued=False, + required=True + )) + + id_coleta = Field(**dict( + name='id_coleta', + alias='id_coleta', + description='id da coleta notificada', + datatype='Text', + indices=['Textual'], + multivalued=False, + required=True + )) + + notify = Field(**dict( + name='notify', + alias='notify', + description='campo de notificações', + datatype='Text', + indices=['Textual'], + multivalued=False, + required=True + )) + + status = Field(**dict( + name='status', + alias='status', + description='status da notificação', + datatype='Text', + indices=['Textual'], + multivalued=False, + required=True + )) + + base_metadata = BaseMetadata( + name='notify', + ) + + content_list = Content() + content_list.append(orgao) + content_list.append(id_coleta) + content_list.append(notify) + content_list.append(status) + + lbbase = Base( + metadata=base_metadata, + content=content_list + ) + + return lbbase + + @property + def metaclass(self): + """ + Retorna metaclass para essa base + """ + return self.lbbase.metaclass() + + def create_base(self): + """ + Cria base no LB + """ + response = self.baserest.create(self.lbbase) + if response.status_code == 200: + return self.lbbase + else: + return None + + def remove_base(self): + """ + Remove base from Lightbase + :param lbbase: LBBase object instance + :return: True or Error if base was not excluded + """ + response = self.baserest.delete(self.lbbase) + if response.status_code == 200: + return True + else: + raise IOError('Error excluding base from LB') + + + +notify_base = NotifyBase() + + +class Notify(notify_base.metaclass): + """ + Classe genérica de órgãos + """ + def __init__(self, **args): + super(Orgao, self).__init__(**args) + self.documentrest = notify_base.documentrest + + def notify_to_dict(self): + """ + Convert status object to Python dict + :return: + """ + + return conv.document2dict(notify_base.lbbase, self) + + def notify_to_json(self): + """ + Convert object to json + :return: + """ + + return conv.document2json(notify_base.lbbase, self) + + def create_notify(self): + """ + Insert document on base + + :return: Document creation ID + """ + + document = self.notify_to_json() + try: + result = notify_base.documentrest.create(document) + except HTTPError as err: + log.error(err.strerror) + return None + + return result + + def search_notify(self, sigla): + """ + Busca registro completo do notify pelo id + :return: obj collection com os dados da base + """ + search = Search( + literal="document->>'sigla' = '"+orgao+"'" + ) + results = self.documentrest.get_collection(search_obj=search) + + return results + + def search_list_notify(self): + """ + Retorna todos os docs da base + """ + search = Search( + limit=None + ) + results = self.documentrest.get_collection(search) + + return results + + def edit_notify(self, id, doc): + """ + altera um doc ou path do doc + """ + results = self.documentrest.update(id, doc) + + return results + + def delete_notify(self, id): + """ + Deleta o Órgao apartir do ID + """ + results = orgao_base.documentrest.delete(id) + + return results diff --git a/wscacicneo/templates/notify_coleta.pt b/wscacicneo/templates/notify_coleta.pt index 78e3353..a96362c 100644 --- a/wscacicneo/templates/notify_coleta.pt +++ b/wscacicneo/templates/notify_coleta.pt @@ -1,41 +1,53 @@ -
-
-
-
-
- -
- -
-
-
- +
+
+ +
+
- -
+
-
- -
- -
+
+
+ +
+ +
+
+
+ +
+
-
-
- - -
+
+
+
+ +
- -
+
+
diff --git a/wscacicneo/test/test_notify_base.py b/wscacicneo/test/test_notify_base.py new file mode 100644 index 0000000..68db1c0 --- /dev/null +++ b/wscacicneo/test/test_notify_base.py @@ -0,0 +1,31 @@ +#!/usr/env python +# -*- coding: utf-8 -*- +__author__ = 'macieski' + +import unittest +import configparser +import os +from wscacicneo.model import notify +from liblightbase.lbbase.struct import Base +from liblightbase.lbutils import conv +from pyramid import testing + +class NotifyBase(unittest.TestCase): + + + def setUp(self): + self.rest_url = 'http://api.brlight.net/api' + + def test_create_base(self): + """ + Testa criação da base no LB + """ + notify_base = notify.NotifyBase(self.rest_url) + lbbase = notify_base.lbbase + self.assertIsInstance(lbbase, Base) + + retorno = notify_base.create_base() + self.assertIsInstance(retorno, Base) + + def tearDown(self): + pass diff --git a/wscacicneo/views.py b/wscacicneo/views.py index 3452da2..ca77ab8 100755 --- a/wscacicneo/views.py +++ b/wscacicneo/views.py @@ -66,8 +66,8 @@ def graficop(request): def gestor(request): return {'project': 'WSCacicNeo'} -@view_config(route_name='notifications', renderer='templates/dashboard.pt') -def notifications(request): +@view_config(route_name='notify', renderer='templates/notify_coleta.pt') +def notify(request): return {'project': 'WSCacicNeo'} @view_config(route_name='admin', renderer='templates/admin.pt') @@ -86,10 +86,6 @@ def cadastro(request): def orgao(request): return {'project': 'WSCacicNeo'} -@view_config(route_name='notify_coleta', renderer='templates/notify_coleta.pt') -def notify_coleta(request): - return {'project': 'WSCacicNeo'} - @view_config(route_name='listorgao', renderer='templates/list_orgao.pt') def listorgao(request): orgao_obj = Orgao( @@ -177,10 +173,6 @@ def editorgao(request): 'url' : search.results[0].url } -@view_config(route_name='notify', renderer='templates/notify.pt') -def notify(request): - return {'project': 'WSCacicNeo'} - @view_config(route_name='configcoleta', renderer='templates/configcoleta.pt') def configcoleta(request): return {'project': 'WSCacicNeo'} @@ -353,6 +345,11 @@ def edituser(request): 'senha' : search.results[0].senha } +@view_config(route_name='post_notify') +def post_notify(request): + print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') + return { } + @view_config(route_name='put_user') def put_user(request): """ @@ -514,3 +511,5 @@ def logout(request): headers = forget(request) return HTTPFound(location = request.route_url('login'), headers = headers) + + -- libgit2 0.21.2