-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase.py
More file actions
95 lines (74 loc) · 2.37 KB
/
base.py
File metadata and controls
95 lines (74 loc) · 2.37 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import inspect
import pecan
from pecan import request
from pecan.rest import RestController
from wsme.types import Base, UserType, Unset
from billingstack.openstack.common import log
LOG = log.getLogger(__name__)
class Property(UserType):
"""
A Property that just passes the value around...
"""
def tonativetype(self, value):
return value
def fromnativetype(self, value):
return value
property_type = Property()
class RestBase(RestController):
__resource__ = None
__id__ = None
def __init__(self, parent=None, id_=None):
self.parent = parent
if self.__id__:
request.context[self.__id__ + '_id'] = id_
self.id_ = id_
@pecan.expose()
def _lookup(self, *url_data):
"""
A fun approach to _lookup - checks self.__resource__ for the "id"
"""
id_ = None
if len(url_data) >= 1:
id_ = url_data[0]
parts = url_data[1:] if len(url_data) > 1 else ()
LOG.debug("Lookup: id '%s' parts '%s'", id_, parts)
resource = self.__resource__
if inspect.isclass(resource) and issubclass(resource, RestBase):
return resource(parent=self, id_=id_), parts
def __getattr__(self, name):
"""
Overload this to look in self.__resource__ if name is defined as a
Controller
"""
if name in self.__dict__:
return self.__dict__[name]
elif isinstance(self.__resource__, dict) and name in self.__resource__:
return self.__resource__[name](parent=self)
else:
raise AttributeError
class ModelBase(Base):
def as_dict(self):
"""
Return this model as a dict
"""
data = {}
for attr in self._wsme_attributes:
value = attr.__get__(self, self.__class__)
if value is not Unset:
if isinstance(value, Base) and hasattr(value, "as_dict"):
value = value.as_dict()
data[attr.name] = value
return data
def to_db(self):
"""
Returns this Model object as it's DB form
Example
'currency' vs 'currency_name'
"""
return self.as_dict()
@classmethod
def from_db(cls, values):
"""
Return a class of this object from values in the from_db
"""
return cls(**values)