From 5f5a6182c9973563821a1e765269d316ff49e5aa Mon Sep 17 00:00:00 2001 From: bit4bit Date: Fri, 25 Jun 2021 23:55:36 +0000 Subject: [PATCH] se adiciona fields.One2Many FossilOrigin-Name: 94c1cca50451a46c417d925b27fdd53d8199b8dc58783e600c84179eac666a36 --- facho/model/fields/__init__.py | 3 ++- facho/model/fields/field.py | 9 +++++++-- facho/model/fields/one2many.py | 25 +++++++++++++++++++++++++ tests/test_model.py | 17 +++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 facho/model/fields/one2many.py diff --git a/facho/model/fields/__init__.py b/facho/model/fields/__init__.py index df6d582..6e9408d 100644 --- a/facho/model/fields/__init__.py +++ b/facho/model/fields/__init__.py @@ -1,10 +1,11 @@ from .attribute import Attribute from .many2one import Many2One +from .one2many import One2Many from .function import Function from .virtual import Virtual from .field import Field -__all__ = [Attribute, Many2One, Virtual, Field] +__all__ = [Attribute, One2Many, Many2One, Virtual, Field] def on_change(fields): from functools import wraps diff --git a/facho/model/fields/field.py b/facho/model/fields/field.py index d340bbc..b3b5e2f 100644 --- a/facho/model/fields/field.py +++ b/facho/model/fields/field.py @@ -26,7 +26,7 @@ class Field: if callable(call): return call(*args) - def _create_model(self, inst, name=None, model=None): + def _create_model(self, inst, name=None, model=None, attribute=None): try: return inst._fields[self.name] except KeyError: @@ -37,7 +37,12 @@ class Field: if name is not None: obj.__name__ = name self._set_namespace(obj, self.namespace, inst.__namespace__) - inst._fields[self.name] = obj + + if attribute: + inst._fields[attribute] = obj + else: + inst._fields[self.name] = obj + return obj def _changed_field(self, inst, name, value): diff --git a/facho/model/fields/one2many.py b/facho/model/fields/one2many.py new file mode 100644 index 0000000..571f61d --- /dev/null +++ b/facho/model/fields/one2many.py @@ -0,0 +1,25 @@ +from .field import Field + +class BoundModel: + def __init__(self, creator): + self.creator = creator + + def create(self): + return self.creator() + +class One2Many(Field): + def __init__(self, model, name=None, namespace=None, default=None): + self.model = model + self.field_name = name + self.namespace = namespace + self.default = default + self.count_relations = 0 + + def __get__(self, inst, cls): + assert self.name is not None + def creator(): + attribute = '%s_%d' % (self.name, self.count_relations) + self.count_relations += 1 + return self._create_model(inst, name=self.field_name, model=self.model, attribute=attribute) + + return BoundModel(creator) diff --git a/tests/test_model.py b/tests/test_model.py index 532c582..4754a3d 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -399,3 +399,20 @@ def test_model_on_change_field_attribute(): person.hash = 'hola' assert '' == person.to_xml() +def test_model_one2many(): + class Line(facho.model.Model): + __name__ = 'Line' + + quantity = fields.Attribute('quantity') + + class Invoice(facho.model.Model): + __name__ = 'Invoice' + + lines = fields.One2Many(Line) + + invoice = Invoice() + line = invoice.lines.create() + line.quantity = 3 + line = invoice.lines.create() + line.quantity = 5 + assert '' == invoice.to_xml()