Commit 088e4c92140339be5cd6ddbbceed65658bcb4ae4
1 parent
15125a1c
Exists in
master
Altera criação para métodos do objeto
Showing
2 changed files
with
40 additions
and
5 deletions
Show diff stats
wscacicneo/model/orgao.py
@@ -2,12 +2,15 @@ | @@ -2,12 +2,15 @@ | ||
2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
3 | __author__ = 'eduardo' | 3 | __author__ = 'eduardo' |
4 | 4 | ||
5 | +from requests.exceptions import HTTPError | ||
5 | from wscacicneo import WSCacicNeo | 6 | from wscacicneo import WSCacicNeo |
6 | from liblightbase.lbbase.struct import Base, BaseMetadata | 7 | from liblightbase.lbbase.struct import Base, BaseMetadata |
7 | from liblightbase.lbbase.lbstruct.group import * | 8 | from liblightbase.lbbase.lbstruct.group import * |
8 | from liblightbase.lbbase.lbstruct.field import * | 9 | from liblightbase.lbbase.lbstruct.field import * |
9 | from liblightbase.lbbase.content import Content | 10 | from liblightbase.lbbase.content import Content |
10 | from liblightbase.lbrest.base import BaseREST | 11 | from liblightbase.lbrest.base import BaseREST |
12 | +from liblightbase.lbrest.document import DocumentREST | ||
13 | +from liblightbase.lbutils import conv | ||
11 | 14 | ||
12 | class OrgaoBase(WSCacicNeo): | 15 | class OrgaoBase(WSCacicNeo): |
13 | """ | 16 | """ |
@@ -19,6 +22,8 @@ class OrgaoBase(WSCacicNeo): | @@ -19,6 +22,8 @@ class OrgaoBase(WSCacicNeo): | ||
19 | """ | 22 | """ |
20 | WSCacicNeo.__init__(self) | 23 | WSCacicNeo.__init__(self) |
21 | self.baserest = BaseREST(rest_url=self.rest_url, response_object=True) | 24 | self.baserest = BaseREST(rest_url=self.rest_url, response_object=True) |
25 | + self.documentrest = DocumentREST(rest_url=self.rest_url, | ||
26 | + base=self.lbbase, response_object=True) | ||
22 | 27 | ||
23 | @property | 28 | @property |
24 | def lbbase(self): | 29 | def lbbase(self): |
@@ -145,12 +150,43 @@ class OrgaoBase(WSCacicNeo): | @@ -145,12 +150,43 @@ class OrgaoBase(WSCacicNeo): | ||
145 | else: | 150 | else: |
146 | raise IOError('Error excluding base from LB') | 151 | raise IOError('Error excluding base from LB') |
147 | 152 | ||
148 | -OrgaoMetaClass = OrgaoBase().metaclass | 153 | +orgao_base = OrgaoBase() |
149 | 154 | ||
150 | - | ||
151 | -class Orgao(OrgaoMetaClass): | 155 | +class Orgao(orgao_base.metaclass): |
152 | """ | 156 | """ |
153 | Classe genérica de órgãos | 157 | Classe genérica de órgãos |
154 | """ | 158 | """ |
155 | def __init__(self, **args): | 159 | def __init__(self, **args): |
156 | super(Orgao, self).__init__(**args) | 160 | super(Orgao, self).__init__(**args) |
161 | + | ||
162 | + def orgao_to_dict(self): | ||
163 | + """ | ||
164 | + Convert status object to Python dict | ||
165 | + :return: | ||
166 | + """ | ||
167 | + | ||
168 | + return conv.document2dict(orgao_base.lbbase, self) | ||
169 | + | ||
170 | + def orgao_to_json(self): | ||
171 | + """ | ||
172 | + Convert object to json | ||
173 | + :return: | ||
174 | + """ | ||
175 | + | ||
176 | + return conv.document2json(orgao_base.lbbase, self) | ||
177 | + | ||
178 | + def create_orgao(self): | ||
179 | + """ | ||
180 | + Insert document on base | ||
181 | + | ||
182 | + :return: Document creation ID | ||
183 | + """ | ||
184 | + | ||
185 | + document = self.orgao_to_json() | ||
186 | + try: | ||
187 | + result = orgao_base.documentrest.create(document) | ||
188 | + except HTTPError, err: | ||
189 | + log.error(err.strerror) | ||
190 | + return None | ||
191 | + | ||
192 | + return result |
wscacicneo/views.py
@@ -184,8 +184,7 @@ def post_orgao(request): | @@ -184,8 +184,7 @@ def post_orgao(request): | ||
184 | telefone = doc['telefone'] | 184 | telefone = doc['telefone'] |
185 | ) | 185 | ) |
186 | 186 | ||
187 | - document = conv.document2json(orgaobase, orgao_obj) | ||
188 | - id_doc = DocumentREST(rest_url, orgaobase).create(document) | 187 | + id_doc = orgao_obj.create_orgao() |
189 | print(id_doc) | 188 | print(id_doc) |
190 | 189 | ||
191 | return Response(str(id_doc)) | 190 | return Response(str(id_doc)) |