blob: c4d920082b1b4b45f841a0d3e6456538d225d404 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
class RightMixin:
@staticmethod
def _fields_base_write():
return {}
@staticmethod
def _fields_base_read():
return {'id'}
@staticmethod
def _fields_api_write():
return {}
@staticmethod
def _fields_api_read():
return {'id'}
@classmethod
def fields_base_write(cls):
return cls._fields_base_write()
@classmethod
def fields_base_read(cls):
return cls._fields_base_write().union(cls._fields_base_read())
@classmethod
def fields_api_write(cls):
return cls.fields_base_write().union(cls._fields_api_write())
@classmethod
def fields_api_read(cls):
return cls.fields_base_read().union(cls._fields_api_read())
def __getitem__(self, key):
if not hasattr(self, '__dump__'):
self.__dump__ = {}
return self.__dump__.get(key)
def __setitem__(self, key, value):
if not hasattr(self, '__dump__'):
self.__dump__ = {}
self.__dump__[key] = value
def dump(self, role='admin'):
if role == 'admin':
dico = {k: getattr(self, k) for k in self.__table__.columns.keys()}
elif role == 'api':
dico = {k: getattr(self, k) for k in self.fields_api_read()}
else:
dico = {k: getattr(self, k) for k in self.fields_base_read()}
if hasattr(self, '__dump__'):
dico.update(self.__dump__)
return dico
|