Commit 30168326927f98d0defe384b71d5bc3d242961b0
1 parent
141d957b
Exists in
master
Criação do modelo de usuários
Showing
1 changed file
with
245 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,245 @@ |
1 | +#!/usr/env python | |
2 | +# -*- coding: utf-8 -*- | |
3 | +__author__ = 'adley' | |
4 | + | |
5 | +from requests.exceptions import HTTPError | |
6 | +from wscacicneo import WSCacicNeo | |
7 | +import logging | |
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 UserBase(WSCacicNeo): | |
20 | + """ | |
21 | + Classe para a base de usuários | |
22 | + """ | |
23 | + def __init__(self): | |
24 | + """ | |
25 | + Método construtor | |
26 | + """ | |
27 | + WSCacicNeo.__init__(self) | |
28 | + self.baserest = BaseREST(rest_url=self.rest_url, response_object=True) | |
29 | + self.documentrest = DocumentREST(rest_url=self.rest_url, | |
30 | + base=self.lbbase, response_object=False) | |
31 | + | |
32 | + @property | |
33 | + def lbbase(self): | |
34 | + """ | |
35 | + LB Base do Users | |
36 | + """ | |
37 | + nome = Field(**dict( | |
38 | + name='nome', | |
39 | + description='Nome do Usuário', | |
40 | + alias='nome', | |
41 | + datatype='Text', | |
42 | + indices=['Textual'], | |
43 | + multivalued=False, | |
44 | + required=True | |
45 | + )) | |
46 | + | |
47 | + matricula = Field(**dict( | |
48 | + name='matricula', | |
49 | + alias='matricula', | |
50 | + description='Matricula do Usuário', | |
51 | + datatype='Text', | |
52 | + indices=['Textual'], | |
53 | + multivalued=False, | |
54 | + required=True | |
55 | + )) | |
56 | + | |
57 | + email = Field(**dict( | |
58 | + name='email', | |
59 | + alias='email', | |
60 | + description='E-mail do Usuário', | |
61 | + datatype='Text', | |
62 | + indices=['Textual'], | |
63 | + multivalued=False, | |
64 | + required=True | |
65 | + )) | |
66 | + | |
67 | + telefone = Field(**dict( | |
68 | + name='telefone', | |
69 | + alias='telefone', | |
70 | + description='Telefone do Usuário', | |
71 | + datatype='Text', | |
72 | + indices=['Textual'], | |
73 | + multivalued=False, | |
74 | + required=True | |
75 | + )) | |
76 | + | |
77 | + orgao = Field(**dict( | |
78 | + name='orgao', | |
79 | + alias='orgao', | |
80 | + description='Orgão do Usuário', | |
81 | + datatype='Text', | |
82 | + indices=['Textual'], | |
83 | + multivalued=False, | |
84 | + required=True | |
85 | + )) | |
86 | + | |
87 | + | |
88 | + cargo = Field(**dict( | |
89 | + name='cargo', | |
90 | + alias='cargo', | |
91 | + description='Cargo do Usuário', | |
92 | + datatype='Text', | |
93 | + indices=['Textual'], | |
94 | + multivalued=False, | |
95 | + required=True | |
96 | + )) | |
97 | + | |
98 | + setor = Field(**dict( | |
99 | + name='setor', | |
100 | + alias='setor', | |
101 | + description='Setor do Usuário', | |
102 | + datatype='Text', | |
103 | + indices=['Textual'], | |
104 | + multivalued=False, | |
105 | + required=True | |
106 | + )) | |
107 | + | |
108 | + permissao = Field(**dict( | |
109 | + name='permissao', | |
110 | + alias='permissao', | |
111 | + description='Permissão do Usuário', | |
112 | + datatype='Text', | |
113 | + indices=['Textual'], | |
114 | + multivalued=False, | |
115 | + required=True | |
116 | + )) | |
117 | + | |
118 | + base_metadata = BaseMetadata( | |
119 | + name='users', | |
120 | + ) | |
121 | + | |
122 | + content_list = Content() | |
123 | + content_list.append(nome) | |
124 | + content_list.append(matricula) | |
125 | + content_list.append(email) | |
126 | + content_list.append(telefone) | |
127 | + content_list.append(orgao) | |
128 | + content_list.append(cargo) | |
129 | + content_list.append(setor) | |
130 | + content_list.append(permissao) | |
131 | + | |
132 | + lbbase = Base( | |
133 | + metadata=base_metadata, | |
134 | + content=content_list | |
135 | + ) | |
136 | + | |
137 | + return lbbase | |
138 | + | |
139 | + @property | |
140 | + def metaclass(self): | |
141 | + """ | |
142 | + Retorna metaclass para essa base | |
143 | + """ | |
144 | + return self.lbbase.metaclass() | |
145 | + | |
146 | + def create_base(self): | |
147 | + """ | |
148 | + Cria base no LB | |
149 | + """ | |
150 | + response = self.baserest.create(self.lbbase) | |
151 | + #print(response.status_code) | |
152 | + if response.status_code == 200: | |
153 | + return self.lbbase | |
154 | + else: | |
155 | + return None | |
156 | + | |
157 | + def remove_base(self): | |
158 | + """ | |
159 | + Remove base from Lightbase | |
160 | + :param lbbase: LBBase object instance | |
161 | + :return: True or Error if base was not excluded | |
162 | + """ | |
163 | + response = self.baserest.delete(self.lbbase) | |
164 | + if response.status_code == 200: | |
165 | + return True | |
166 | + else: | |
167 | + raise IOError('Error excluding base from LB') | |
168 | + | |
169 | +user_base = UserBase() | |
170 | + | |
171 | +class User(user.base.metaclass): | |
172 | + """ | |
173 | + Classe genérica de usuários | |
174 | + """ | |
175 | + def __init__(self, **args): | |
176 | + super(Orgao, self).__init__(**args) | |
177 | + self.documentrest = user_base.documentrest | |
178 | + | |
179 | + def user_to_dict(self): | |
180 | + """ | |
181 | + Convert status object to Python dict | |
182 | + :return: | |
183 | + """ | |
184 | + | |
185 | + return conv.document2dict(user_base.lbbase, self) | |
186 | + | |
187 | + def user_to_json(self): | |
188 | + """ | |
189 | + Convert object to json | |
190 | + :return: | |
191 | + """ | |
192 | + | |
193 | + return conv.document2json(user_base.lbbase, self) | |
194 | + | |
195 | + def create_user(self): | |
196 | + """ | |
197 | + Insert document on base | |
198 | + | |
199 | + :return: Document creation ID | |
200 | + """ | |
201 | + | |
202 | + document = self.orgao_to_json() | |
203 | + try: | |
204 | + result = user_base.documentrest.create(document) | |
205 | + except HTTPError as err: | |
206 | + log.error(err.strerror) | |
207 | + return None | |
208 | + | |
209 | + return result | |
210 | + | |
211 | + def search_user(self, matricula_user): | |
212 | + """ | |
213 | + Busca registro completo do usuário pela matricula | |
214 | + :return: obj collection com os dados da base | |
215 | + """ | |
216 | + search = Search( | |
217 | + literal="document->>'matricula' = '"+matricula_user+"'" | |
218 | + ) | |
219 | + results = self.documentrest.get_collection(search_obj=search) | |
220 | + | |
221 | + return results | |
222 | + | |
223 | + def search_list_users(self): | |
224 | + """ | |
225 | + Retorna todos os docs da base | |
226 | + """ | |
227 | + results = self.documentrest.get_collection(limit=None) | |
228 | + | |
229 | + return results | |
230 | + | |
231 | + def edit_user(self, id, doc): | |
232 | + """ | |
233 | + altera um doc ou path do doc | |
234 | + """ | |
235 | + results = self.documentrest.update(id, doc) | |
236 | + | |
237 | + return results | |
238 | + | |
239 | + def delete_user(self, id): | |
240 | + """ | |
241 | + Deleta o Órgao apartir do ID | |
242 | + """ | |
243 | + results = user_base.documentrest.delete(id) | |
244 | + | |
245 | + return results | |
0 | 246 | \ No newline at end of file | ... | ... |