Commit 922ad5dad61249ab89c8bda2cac78e3250b3dcca
1 parent
1fff73f9
Exists in
master
Codigo funcionando com envio do registro e store do id no banco
git-svn-id: http://svn.brlight.net/svn/lightbase-neo/trunk/LBBulk@807 29b92fdf-8c97-4584-b987-84e8d3c556fa
Showing
6 changed files
with
51 additions
and
14 deletions
Show diff stats
TODO.txt
1 | -- Recebe registro | ||
2 | -- Verifica se o registro já existe no lightbase: | ||
3 | - se sim, atualiza | ||
4 | - se não, cria | ||
5 | -- salva na tabela de chaves a chave do lightbase que corresponde à esse registro | ||
6 | \ No newline at end of file | 1 | \ No newline at end of file |
2 | +lbbulk/view/restfulview.py line 41 | ||
3 | +lbbulk/view/restfulview.py line 48 | ||
7 | \ No newline at end of file | 4 | \ No newline at end of file |
development.ini
@@ -12,18 +12,12 @@ pyramid.debug_notfound = false | @@ -12,18 +12,12 @@ pyramid.debug_notfound = false | ||
12 | pyramid.debug_routematch = false | 12 | pyramid.debug_routematch = false |
13 | pyramid.default_locale_name = en | 13 | pyramid.default_locale_name = en |
14 | pyramid.includes = | 14 | pyramid.includes = |
15 | -# pyramid_debugtoolbar | ||
16 | pyramid_tm | 15 | pyramid_tm |
17 | 16 | ||
18 | sqlalchemy.url = postgresql://postgres:postgres@10.1.0.152/projeto1 | 17 | sqlalchemy.url = postgresql://postgres:postgres@10.1.0.152/projeto1 |
19 | 18 | ||
20 | -# By default, the toolbar only appears for clients from IP addresses | ||
21 | -# '127.0.0.1' and '::1'. | ||
22 | -# debugtoolbar.hosts = 0.0.0.0/0 | ||
23 | - | ||
24 | -### | ||
25 | -# wsgi server configuration | ||
26 | -### | 19 | +domain = http://localhost/lbgenerator |
20 | +base_name = WMI | ||
27 | 21 | ||
28 | [server:main] | 22 | [server:main] |
29 | use = egg:waitress#main | 23 | use = egg:waitress#main |
lbbulk/model/BulkUpload.py
@@ -22,6 +22,12 @@ class BulkUploadContextFactory(SQLAlchemyORMContext): | @@ -22,6 +22,12 @@ class BulkUploadContextFactory(SQLAlchemyORMContext): | ||
22 | def session_factory(self): | 22 | def session_factory(self): |
23 | return session | 23 | return session |
24 | 24 | ||
25 | + def create_member(self, data): | ||
26 | + member = self.entity(**data) | ||
27 | + self.session.add(member) | ||
28 | + self.session.commit() | ||
29 | + return member | ||
30 | + | ||
25 | def get_member_id_as_string(self, member): | 31 | def get_member_id_as_string(self, member): |
26 | id = self.get_member_id(member) | 32 | id = self.get_member_id(member) |
27 | return json.dumps(id, cls=self.json_encoder) | 33 | return json.dumps(id, cls=self.json_encoder) |
28 | \ No newline at end of file | 34 | \ No newline at end of file |
lbbulk/view/restfulview.py
1 | from pyramid_restler.view import RESTfulView | 1 | from pyramid_restler.view import RESTfulView |
2 | +import configparser | ||
2 | import json | 3 | import json |
4 | +import requests | ||
3 | 5 | ||
4 | class RegCustomView(RESTfulView): | 6 | class RegCustomView(RESTfulView): |
5 | def _get_data(self): | 7 | def _get_data(self): |
8 | + """ Read json data from the post sent by some source """ | ||
9 | + print('\n\n\n\n\n\n\n\n\n\n\n\n') | ||
6 | content_type = self.request.content_type | 10 | content_type = self.request.content_type |
7 | if content_type == 'application/json': | 11 | if content_type == 'application/json': |
8 | data = json.loads(self.request.body) | 12 | data = json.loads(self.request.body) |
@@ -10,5 +14,36 @@ class RegCustomView(RESTfulView): | @@ -10,5 +14,36 @@ class RegCustomView(RESTfulView): | ||
10 | data = dict(self.request.POST) | 14 | data = dict(self.request.POST) |
11 | else: | 15 | else: |
12 | data = self.request.params | 16 | data = self.request.params |
13 | - data = data['json_reg'] | 17 | + data = self.send_to_lightbase(data) |
18 | + # Must come with the external key as a value of | ||
19 | + # 'data.json_reg.id_reg' | ||
20 | + return data | ||
21 | + | ||
22 | + def send_to_lightbase(self, data): | ||
23 | + """ Sends the register to a lightbase table and returns a dict | ||
24 | + with the external key and lighbase's id_reg """ | ||
25 | + registro = self.is_error(data) | ||
26 | + id_source = registro['id_source'] | ||
27 | + registro.pop('id_source', None) | ||
28 | + registro['json_reg'] = json.loads(registro['json_reg']) | ||
29 | + chave_externa = registro['json_reg']['id_reg'] | ||
30 | + registro['json_reg'].pop('id_reg', None) | ||
31 | + url = self.get_url_lightbase() | ||
32 | + r = requests.post(url, data=registro) | ||
33 | + id_reg = r.json() | ||
34 | + data = { | ||
35 | + 'id_source':id_source, | ||
36 | + 'id_reg':id_reg, | ||
37 | + 'chave_externa':chave_externa | ||
38 | + } | ||
39 | + return data | ||
40 | + | ||
41 | + def get_url_lightbase(self): #TODO | ||
42 | + """ Returns url from config """ | ||
43 | + domain = 'http://localhost/lbgenerator' | ||
44 | + base_name = 'WMI' | ||
45 | + url = domain + '/' + base_name + '/reg' | ||
46 | + return url | ||
47 | + | ||
48 | + def is_error(self,data): #TODO | ||
14 | return data | 49 | return data |
15 | \ No newline at end of file | 50 | \ No newline at end of file |
production.ini
@@ -16,6 +16,9 @@ pyramid.includes = | @@ -16,6 +16,9 @@ pyramid.includes = | ||
16 | 16 | ||
17 | sqlalchemy.url = postgresql://postgres:postgres@10.1.0.152/projeto1 | 17 | sqlalchemy.url = postgresql://postgres:postgres@10.1.0.152/projeto1 |
18 | 18 | ||
19 | +domain = http://localhost/lbgenerator | ||
20 | +base_name = WMI | ||
21 | + | ||
19 | [server:main] | 22 | [server:main] |
20 | use = egg:waitress#main | 23 | use = egg:waitress#main |
21 | host = 0.0.0.0 | 24 | host = 0.0.0.0 |
setup.py