From cf60a7ac60d2e6286274a9a407a63f6792a39fe6 Mon Sep 17 00:00:00 2001 From: pedro Date: Mon, 2 Dec 2013 17:46:01 +0000 Subject: [PATCH] post no lighbase funcionando corretamente, porém, funcionamento do put é incerto --- development.ini | 9 ++------- lbbulk/config/routing.py | 2 +- lbbulk/model/BulkUpload.py | 15 ++++++++------- lbbulk/view/restfulview.py | 43 +++++++++++++++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/development.ini b/development.ini index 24b6265..e7a297f 100644 --- a/development.ini +++ b/development.ini @@ -1,8 +1,3 @@ -### -# app configuration -# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html -### - [app:main] use = egg:LBBulk @@ -14,7 +9,7 @@ pyramid.default_locale_name = en pyramid.includes = pyramid_tm -sqlalchemy.url = postgresql://postgres:postgres@10.1.0.152/projeto1 +sqlalchemy.url = postgresql://rest:rest@localhost/projeto1 domain = http://localhost/lbgenerator base_name = WMI @@ -48,7 +43,7 @@ handlers = qualname = lbbulk [logger_sqlalchemy] -level = INFO +level = WARN handlers = qualname = sqlalchemy.engine # "level = INFO" logs SQL queries. diff --git a/lbbulk/config/routing.py b/lbbulk/config/routing.py index c2166f8..a1723ca 100644 --- a/lbbulk/config/routing.py +++ b/lbbulk/config/routing.py @@ -10,5 +10,5 @@ def make_routes(config): config.add_static_view('static', 'static', cache_max_age=3600) config.add_route('home', '/') config.add_restful_routes('sources', BulkSourcesContextFactory) - config.add_restful_routes('registro', BulkUploadContextFactory, + config.add_restful_routes('reg', BulkUploadContextFactory, view=RegCustomView) \ No newline at end of file diff --git a/lbbulk/model/BulkUpload.py b/lbbulk/model/BulkUpload.py index 309141c..f99920d 100644 --- a/lbbulk/model/BulkUpload.py +++ b/lbbulk/model/BulkUpload.py @@ -9,25 +9,26 @@ bulk_upload = Table('lb_bulk_upload', metadata, Column('chave_externa', String, nullable=False), Column('id_source', Integer, ForeignKey('lb_bulk_sources.id_source'), - nullable=False) + nullable=False), + extend_existing=True ) # map to it class BulkUpload(Base): __table__ = bulk_upload + def verifica_registro(data): + q = session.query(BulkUpload).filter_by(id_source=data['id_source'], chave_externa=data['json_reg']['id_reg'] ) + registro_existe = q.first() + return registro_existe + + class BulkUploadContextFactory(SQLAlchemyORMContext): entity = BulkUpload def session_factory(self): return session - def create_member(self, data): - member = self.entity(**data) - self.session.add(member) - self.session.commit() - return member - def get_member_id_as_string(self, member): id = self.get_member_id(member) return json.dumps(id, cls=self.json_encoder) \ No newline at end of file diff --git a/lbbulk/view/restfulview.py b/lbbulk/view/restfulview.py index 2f9d13b..4f69139 100644 --- a/lbbulk/view/restfulview.py +++ b/lbbulk/view/restfulview.py @@ -1,12 +1,20 @@ from pyramid_restler.view import RESTfulView +from pyramid.response import Response +from lbbulk.model.BulkUpload import BulkUpload import configparser import json import requests class RegCustomView(RESTfulView): + + def create_member(self): + member = self.context.create_member(self._get_data()) + id = self.context.get_member_id_as_string(member) + headers = {'Location': '/'.join((self.request.path, id))} + return Response(status=201, headers=headers) + def _get_data(self): """ Read json data from the post sent by some source """ - print('\n\n\n\n\n\n\n\n\n\n\n\n') content_type = self.request.content_type if content_type == 'application/json': data = json.loads(self.request.body) @@ -22,20 +30,30 @@ class RegCustomView(RESTfulView): def send_to_lightbase(self, data): """ Sends the register to a lightbase table and returns a dict with the external key and lighbase's id_reg """ + data['json_reg'] = json.loads(data['json_reg']) + # converte os filhos pra dicts + existente = BulkUpload.verifica_registro(data) + # print(existente.id_reg) + print('\n\n\n\n\n\n\n\n\n\n') registro = self.is_error(data) id_source = registro['id_source'] registro.pop('id_source', None) - registro['json_reg'] = json.loads(registro['json_reg']) chave_externa = registro['json_reg']['id_reg'] registro['json_reg'].pop('id_reg', None) + registro['json_reg'] = json.dumps(registro['json_reg']) url = self.get_url_lightbase() - r = requests.post(url, data=registro) - id_reg = r.json() - data = { - 'id_source':id_source, - 'id_reg':id_reg, - 'chave_externa':chave_externa - } + if existente is None: + r = requests.post(url, data=registro) + id_reg = self.is_error_resp(r.json()) + data = { + 'id_source':id_source, + 'id_reg':id_reg, + 'chave_externa':chave_externa + } + else: + r = requests.put(url, data=registro) + id_reg = existente + data = None return data def get_url_lightbase(self): #TODO @@ -46,4 +64,9 @@ class RegCustomView(RESTfulView): return url def is_error(self,data): #TODO - return data \ No newline at end of file + return data + + def is_error_resp(self,r): + if type(r) is dict: + raise TypeError('Lightbase error ' + str(r['_status']) + ': ' + r['_error_message']) + return r \ No newline at end of file -- libgit2 0.21.2