diff --git a/domain_models/__init__.py b/domain_models/__init__.py index e1e9e56..b2f023c 100644 --- a/domain_models/__init__.py +++ b/domain_models/__init__.py @@ -1,3 +1,3 @@ """Domain models.""" -VERSION = '0.0.9' +VERSION = '0.0.10' diff --git a/domain_models/fields.py b/domain_models/fields.py index bede8ac..5e45471 100644 --- a/domain_models/fields.py +++ b/domain_models/fields.py @@ -16,6 +16,9 @@ def __init__(self, default=None, required=False): self.name = None self.storage_name = None + self.setter_fn = None + self.getter_fn = None + self.model_cls = None self.default = default @@ -60,7 +63,11 @@ def get_value(self, model, default=None): if default is not None: default = self._converter(default) - value = getattr(model, self.storage_name) + if callable(self.getter_fn): + value = self.getter_fn(model) + else: + value = getattr(model, self.storage_name) + return value if value is not None else default def set_value(self, model, value): @@ -75,7 +82,10 @@ def set_value(self, model, value): if value is not None: value = self._converter(value) - setattr(model, self.storage_name, value) + if callable(self.setter_fn) and value is not None: + self.setter_fn(model, value) + else: + setattr(model, self.storage_name, value) def get_builtin_type(self, model): """Return built-in type representation of Field. @@ -85,6 +95,14 @@ def get_builtin_type(self, model): """ return self.get_value(model) + def getter(self, fn): + """Set function for implementation custom getter feature.""" + self.getter_fn = fn + + def setter(self, fn): + """Set function for implementation custom setter feature.""" + self.setter_fn = fn + def _converter(self, value): """Convert raw input value of the field. diff --git a/tests/test_fields.py b/tests/test_fields.py index 96f4868..e865007 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -44,6 +44,19 @@ class RequiredFieldModel(models.DomainModel): field_required = fields.Field(required=True) +class CustomGetterSetter(models.DomainModel): + open_id = fields.Int() + id = fields.Int() + + @open_id.getter + def _get_oid(self): + return self.id << 8 + + @open_id.setter + def _set_oid(self, value): + self.id = value >> 8 + + class FieldTest(unittest.TestCase): """Base field tests.""" @@ -454,3 +467,27 @@ def test_set_incorrect(self): with self.assertRaises(TypeError): model.collection_field = [some_object] + + +class CustomGetterSetterTest(unittest.TestCase): + """Test cases for custom getters and setters feature.""" + + def test_getter(self): + test_model = CustomGetterSetter() + test_model.id = 1 + self.assertEqual(test_model.id, 1) + self.assertEqual(test_model.open_id, 256) + + test_model = CustomGetterSetter(id=1) + self.assertEqual(test_model.id, 1) + self.assertEqual(test_model.open_id, 256) + + def test_setter(self): + test_model = CustomGetterSetter() + test_model.open_id = 256 + self.assertEqual(test_model.id, 1) + self.assertEqual(test_model.open_id, 256) + + test_model = CustomGetterSetter(open_id=256) + self.assertEqual(test_model.id, 1) + self.assertEqual(test_model.open_id, 256) diff --git a/tests/test_models.py b/tests/test_models.py index 78ef7c2..707916b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -4,8 +4,6 @@ import unittest2 as unittest -import six - from domain_models import models from domain_models import fields from domain_models import collections