Skip to content

Commit 4dc4402

Browse files
committed
Merge pull request #31 from ets-labs/8-get_model_data
Issue #8: Implementation of DomainModel.get_data() method.
2 parents 2a99328 + ff3773e commit 4dc4402

File tree

5 files changed

+76
-51
lines changed

5 files changed

+76
-51
lines changed

CONTRIBUTORS.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Domain Models Contributors
22
==========================
33

44
+ Roman Mogilatov
5-
5+
+ Sergii [boonya] Buinytskyi

domain_models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Domain models."""
22

3-
VERSION = '0.0.3'
3+
VERSION = '0.0.4'

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,6 @@ def __str__(self):
180180
str(field.get_value(self))))
181181
for field in self.__class__.__view_key__))
182182

183-
@property
184-
def __data__(self):
185-
"""Read only dictionary of model fields/values."""
186-
return dict((name, field.get_value(self))
187-
for name, field in
188-
six.iteritems(self.__class__.__fields__))
189-
190183
def get(self, field_name, default=None):
191184
"""Return the value of the field.
192185
@@ -209,3 +202,12 @@ def get(self, field_name, default=None):
209202
"Field {0} does not exist.".format(field_name))
210203
else:
211204
return field.get_value(self, default)
205+
206+
def get_data(self):
207+
"""Read only dictionary of model fields/values.
208+
209+
:rtype dict:
210+
"""
211+
return dict((name, field.get_builtin_type(self))
212+
for name, field in
213+
six.iteritems(self.__class__.__fields__))

tests/test_models.py

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,6 @@ class User(models.DomainModel):
5757
self.assertEqual(user2.gender, 'female')
5858
self.assertEqual(user2.birth_date, '05/04/1985')
5959

60-
def test_data_attr(self):
61-
"""Test model's __data__ attribute."""
62-
class User(models.DomainModel):
63-
"""Test user domain model."""
64-
65-
id = fields.Int()
66-
email = fields.String()
67-
first_name = fields.String()
68-
last_name = fields.String()
69-
gender = fields.String()
70-
birth_date = fields.String()
71-
72-
user1 = User()
73-
user1.id = 1
74-
user1.email = 'example1@example.com'
75-
user1.first_name = 'John'
76-
user1.last_name = 'Smith'
77-
user1.gender = 'male'
78-
user1.birth_date = '05/04/1988'
79-
80-
user2 = User()
81-
user2.id = 2
82-
user2.email = 'example2@example.com'
83-
user2.first_name = 'Jane'
84-
user2.last_name = 'Smith'
85-
user2.gender = 'female'
86-
user2.birth_date = '05/04/1985'
87-
88-
self.assertEquals(user1.__data__, dict(id=1,
89-
email='example1@example.com',
90-
first_name='John',
91-
last_name='Smith',
92-
gender='male',
93-
birth_date='05/04/1988'))
94-
self.assertEquals(user2.__data__, dict(id=2,
95-
email='example2@example.com',
96-
first_name='Jane',
97-
last_name='Smith',
98-
gender='female',
99-
birth_date='05/04/1985'))
100-
10160
def test_not_valid_unique_key_field(self):
10261
"""Test that error is raised when unique key is not correct."""
10362
with self.assertRaises(errors.Error):
@@ -324,6 +283,40 @@ def test_get_method_on_collection(self):
324283
"""Test method get on Collection of Model."""
325284
self.skipTest("Test is not implemented yet")
326285

286+
def test_get_data_method(self):
287+
class Photo(models.DomainModel):
288+
id = fields.Int()
289+
url = fields.String()
290+
291+
class Profile(models.DomainModel):
292+
id = fields.Int()
293+
name = fields.String()
294+
main_photo = fields.Model(Photo)
295+
photos = fields.Collection(Photo)
296+
birth_date = fields.Date()
297+
sequence = fields.Collection(fields.Int)
298+
299+
photo1 = Photo(id=1, url='http://boonya.info/wat.jpg?1')
300+
photo2 = Photo(id=2, url='http://boonya.info/wat.jpg?2')
301+
profile = Profile(id=1, name='John', main_photo=photo1,
302+
photos=[photo1, photo2],
303+
sequence=[1, 1, 2, 3, 5, 8, 13],
304+
birth_date=datetime.date(year=1986, month=4,
305+
day=26))
306+
307+
self.assertDictEqual(profile.get_data(), {
308+
'id': 1,
309+
'name': 'John',
310+
'main_photo': {'id': 1,
311+
'url': 'http://boonya.info/wat.jpg?1'},
312+
'photos': [
313+
{'id': 1, 'url': 'http://boonya.info/wat.jpg?1'},
314+
{'id': 2, 'url': 'http://boonya.info/wat.jpg?2'}
315+
],
316+
'sequence': [1, 1, 2, 3, 5, 8, 13],
317+
'birth_date': datetime.date(year=1986, month=4, day=26)
318+
})
319+
327320

328321
class ModelReprTests(unittest.TestCase):
329322
"""Tests for model Pythonic representation."""

0 commit comments

Comments
 (0)