Skip to content

Commit b03e6fa

Browse files
committed
Issue #8: Implementation of DomainModel.get_data()
1 parent cbfce0c commit b03e6fa

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

domain_models/fields.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def get_value(self, model, default=None):
5555
5656
:param DomainModel model:
5757
:param mixed default:
58+
:rtype object:
5859
"""
5960
if default is not None:
6061
default = self._converter(default)
@@ -63,7 +64,11 @@ def get_value(self, model, default=None):
6364
return value if value is not None else default
6465

6566
def set_value(self, model, value):
66-
"""Set field's value."""
67+
"""Set field's value.
68+
69+
:param DomainModel model:
70+
:param mixed value:
71+
"""
6772
if value is None and self.required:
6873
raise AttributeError("This field is required.")
6974

@@ -72,6 +77,14 @@ def set_value(self, model, value):
7277

7378
setattr(model, self.storage_name, value)
7479

80+
def get_builtin_type(self, model):
81+
"""Return built-in type representation of Field.
82+
83+
:param DomainModel model:
84+
:rtype object:
85+
"""
86+
return self.get_value(model)
87+
7588
def _converter(self, value):
7689
"""Convert raw input value of the field."""
7790
return value
@@ -154,6 +167,14 @@ def _converter(self, value):
154167
self.related_model_cls))
155168
return value
156169

170+
def get_builtin_type(self, model):
171+
"""Return built-in type representation of Model.
172+
173+
:param DomainModel model:
174+
:rtype dict:
175+
"""
176+
return self.get_value(model).get_data()
177+
157178

158179
class Collection(Field):
159180
"""Models collection relation field."""
@@ -168,3 +189,12 @@ def _converter(self, value):
168189
if type(value) is not self.related_model_cls.Collection:
169190
value = self.related_model_cls.Collection(value)
170191
return value
192+
193+
def get_builtin_type(self, model):
194+
"""Return built-in type representation of Collection.
195+
196+
:param DomainModel model:
197+
:rtype list:
198+
"""
199+
return [item.get_data() if isinstance(item, self.related_model_cls)
200+
else item for item in self.get_value(model)]

domain_models/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,12 @@ def get(self, field_name, default=None):
209209
"Field {0} does not exist.".format(field_name))
210210
else:
211211
return field.get_value(self, default)
212+
213+
def get_data(self):
214+
"""Read only dictionary of model fields/values.
215+
216+
:rtype dict:
217+
"""
218+
return dict((name, field.get_builtin_type(self))
219+
for name, field in
220+
six.iteritems(self.__class__.__fields__))

0 commit comments

Comments
 (0)