From 3eacb29afabd4cf92ffd52abe55201ae4a7d7f47 Mon Sep 17 00:00:00 2001 From: bit4bit Date: Thu, 24 Jun 2021 23:38:28 +0000 Subject: [PATCH] se separa responsabilidad de fields.Function FossilOrigin-Name: 4d5daa47a75a0e283e86bf992126bf60f3a8a14287e9acc437d5f2f3eca43150 --- facho/fe/model/__init__.py | 2 +- facho/model/fields/__init__.py | 3 ++- facho/model/fields/attribute.py | 9 ++++---- facho/model/fields/function.py | 25 ++++++++------------ tests/test_model.py | 41 +++++++++++++++------------------ 5 files changed, 36 insertions(+), 44 deletions(-) diff --git a/facho/fe/model/__init__.py b/facho/fe/model/__init__.py index 42536d6..a2cbe20 100644 --- a/facho/fe/model/__init__.py +++ b/facho/fe/model/__init__.py @@ -49,7 +49,7 @@ class Invoice(model.Model): __name__ = 'Invoice' id = fields.Many2One(ID) - issue = fields.Function(setter='set_issue') + issue = fields.Virtual(setter='set_issue') issue_date = fields.Many2One(Date, name='IssueDate') issue_time = fields.Many2One(Time, name='IssueTime') diff --git a/facho/model/fields/__init__.py b/facho/model/fields/__init__.py index fb85bc9..77d3bef 100644 --- a/facho/model/fields/__init__.py +++ b/facho/model/fields/__init__.py @@ -2,5 +2,6 @@ from .attribute import Attribute from .many2one import Many2One from .model import Model from .function import Function +from .virtual import Virtual -__all__ = [Attribute, Many2One, Model] +__all__ = [Attribute, Many2One, Model, Virtual] diff --git a/facho/model/fields/attribute.py b/facho/model/fields/attribute.py index 2ad9f73..e8fd7a0 100644 --- a/facho/model/fields/attribute.py +++ b/facho/model/fields/attribute.py @@ -1,17 +1,18 @@ from .field import Field class Attribute(Field): - def __init__(self, tag): + def __init__(self, tag, default=None): self.tag = tag - + self.value = default + def __get__(self, inst, cls): if inst is None: return self assert self.name is not None - (tag, value) = inst._xml_attributes[self.name] - return value + return self.value def __set__(self, inst, value): assert self.name is not None + self.value = value inst._xml_attributes[self.name] = (self.tag, value) diff --git a/facho/model/fields/function.py b/facho/model/fields/function.py index 9ea2ac3..2294041 100644 --- a/facho/model/fields/function.py +++ b/facho/model/fields/function.py @@ -2,29 +2,24 @@ from .field import Field from .model import Model class Function(Field): - def __init__(self, getter=None, setter=None, field=None): + def __init__(self, field, getter=None): self.field = field self.getter = getter - self.setter = setter def __get__(self, inst, cls): if inst is None: return self assert self.name is not None - if self.getter is None and self.field is None: - return None + # si se indica `field` se adiciona + # como campo del modelo, esto es + # que se serializa a xml + inst._fields[self.name] = self.field - if self.getter is None and self.field is not None: - return Model(self.field) + if self.getter is not None: + value = self._call(inst, self.getter, self.name, self.field) - if self.field is None: - return self._call(inst, self.getter, self.name) - else: - obj = Model(self.field) - return self._call(inst, self.getter, self.name, obj) + if value is not None: + self.field.__set__(inst, value) - def __set__(self, inst, value): - if self.setter is None: - return super().__set__(self.name, value) - self._call(inst, self.setter, self.name, value) + return self.field diff --git a/tests/test_model.py b/tests/test_model.py index 220a4ec..b42b473 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -231,7 +231,7 @@ def test_field_function_with_attribute(): class Person(facho.model.Model): __name__ = 'Person' - hash = fields.Function('get_hash', field=fields.Attribute('hash')) + hash = fields.Function(fields.Attribute('hash'), getter='get_hash') def get_hash(self, name, field): return 'calculate' @@ -248,39 +248,23 @@ def test_field_function_with_model(): class Person(facho.model.Model): __name__ = 'Person' - hash = fields.Function('get_hash', field=Hash) + hash = fields.Function(fields.Many2One(Hash), getter='get_hash') def get_hash(self, name, field): field.id = 'calculate' - return field + person = Person() assert person.hash.id == 'calculate' assert '' - -def test_field_function(): - class Person(facho.model.Model): - __name__ = 'Person' - - hash = fields.Function('get_hash') - - def get_hash(self, name): - return 'calculate' - - person = Person() - assert person.hash == 'calculate' - assert "" == person.to_xml() - + def test_field_function_setter(): class Person(facho.model.Model): __name__ = 'Person' hash = fields.Attribute('hash') - password = fields.Function('get_hash', setter='set_hash') - - def get_hash(self, name): - return None + password = fields.Virtual(setter='set_hash') def set_hash(self, name, value): self.hash = "%s+2" % (value) @@ -294,7 +278,7 @@ def test_field_function_only_setter(): __name__ = 'Person' hash = fields.Attribute('hash') - password = fields.Function(setter='set_hash') + password = fields.Virtual(setter='set_hash') def set_hash(self, name, value): self.hash = "%s+2" % (value) @@ -303,7 +287,6 @@ def test_field_function_only_setter(): person.password = 'calculate' assert '' == person.to_xml() - def test_model_set_default_setter(): class Hash(facho.model.Model): __name__ = 'Hash' @@ -319,3 +302,15 @@ def test_model_set_default_setter(): person = Person() person.hash = 'hola' assert 'hola+3' == person.to_xml() + + +def test_field_virtual(): + class Person(facho.model.Model): + __name__ = 'Person' + + age = fields.Virtual() + + person = Person() + person.age = 55 + assert person.age == 55 + assert "" == person.to_xml()