From 4d0c50565f2f9d2eb20934049fe3005e955993c3 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Tue, 2 Jun 2015 22:07:16 -0700 Subject: [PATCH] Fixed adding hooks to fields defined in parent classes --- pilo/fields.py | 4 ++-- tests/test_fields.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pilo/fields.py b/pilo/fields.py index b8d96c2..f2a9dd8 100644 --- a/pilo/fields.py +++ b/pilo/fields.py @@ -137,7 +137,7 @@ class CreatedCountMixin(object): _created_count = 0 - def __init__(self): + def __init__(self): CreatedCountMixin._created_count += 1 self._count = CreatedCountMixin._created_count @@ -389,7 +389,7 @@ def is_attached(self): return self.parent is not None def clone(self): - other = copy.copy(self) + other = copy.deepcopy(self) other.parent = None return other diff --git a/tests/test_fields.py b/tests/test_fields.py index d933d67..7280514 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -374,3 +374,28 @@ def send_to_zoo(self): ]: obj = Animal.type.cast(desc)(desc) self.assertIsInstance(obj, cls) + + def test_override_hook(self): + """ + Test that Sub-classes can independently provide hooks on the fields by + cloning. + """ + + class Item(pilo.Form): + price = pilo.fields.Integer() + + class Heirloom(Item): + + has_sentimental_value = pilo.fields.Boolean() + + price = Item.price.clone() + + @price.compute + def get_price(self): + return 1000000 if self.has_sentimental_value else 0 + + item = Item(price=100) + self.assertEqual(item.price, 100) + + heirloom = Heirloom(has_sentimental_value=False) + self.assertEqual(heirloom.price, 0)