se separa responsabilidad de fields.Function

FossilOrigin-Name: 4d5daa47a75a0e283e86bf992126bf60f3a8a14287e9acc437d5f2f3eca43150
This commit is contained in:
bit4bit 2021-06-24 23:38:28 +00:00
parent f630a544c2
commit 3eacb29afa
5 changed files with 36 additions and 44 deletions

View File

@ -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')

View File

@ -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]

View File

@ -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)

View File

@ -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

View File

@ -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 '<Person/>'
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/>" == 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 hash="calculate+2"/>' == 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 '<Person><Hash>hola+3</Hash></Person>' == 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/>" == person.to_xml()