fields.Function no requiere getter

FossilOrigin-Name: 47f9b9427ef55c688678001361260e5d00ea53d82977ea13e3414ed04878fb36
This commit is contained in:
bit4bit 2021-06-24 01:28:07 +00:00
parent 0216d0141a
commit a015a9361b
2 changed files with 21 additions and 1 deletions

View File

@ -2,7 +2,7 @@ from .field import Field
from .model import Model
class Function(Field):
def __init__(self, getter, setter=None, field=None):
def __init__(self, getter=None, setter=None, field=None):
self.field = field
self.getter = getter
self.setter = setter
@ -12,6 +12,12 @@ class Function(Field):
return self
assert self.name is not None
if self.getter is None and self.field is None:
return None
if self.getter is None and self.field is not None:
return Model(self.field)
if self.field is None:
return self._call(inst, self.getter, self.name)
else:

View File

@ -257,4 +257,18 @@ def test_field_function_setter():
person = Person()
person.password = 'calculate'
assert '<Person hash="calculate+2"/>' == person.to_xml()
def test_field_function_only_setter():
class Person(facho.model.Model):
__name__ = 'Person'
hash = fields.Attribute('hash')
password = fields.Function(setter='set_hash')
def set_hash(self, name, value):
self.hash = "%s+2" % (value)
person = Person()
person.password = 'calculate'
assert '<Person hash="calculate+2"/>' == person.to_xml()