From 6fb486cc5cd04e84d3d556b16d1f382f890b91f1 Mon Sep 17 00:00:00 2001 From: Eugen Zagorodniy Date: Sat, 12 Jun 2021 00:50:00 +0300 Subject: [PATCH 1/3] Make parent formid a part of autogenerated oid https://github.com/Pylons/deform/issues/394 --- deform/field.py | 7 ++++--- deform/form.py | 2 +- deform/tests/test_form.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/deform/field.py b/deform/field.py index 750ab635..1f56ab03 100644 --- a/deform/field.py +++ b/deform/field.py @@ -183,11 +183,10 @@ def __init__( appstruct=colander.null, parent=None, autofocus=None, - **kw + **kw, ): self.counter = counter or itertools.count() self.order = next(self.counter) - self.oid = getattr(schema, "oid", "deformField%s" % self.order) self.schema = schema self.typ = schema.typ # required by Invalid exception self.name = schema.name @@ -225,6 +224,8 @@ def __init__( if parent is not None: parent = weakref.ref(parent) self._parent = parent + oid_prefix = getattr(self.get_root(), "formid", "deform") + self.oid = getattr(schema, "oid", f"{oid_prefix}Field{self.order}") self.__dict__.update(kw) first_input_index = -1 @@ -254,7 +255,7 @@ def __init__( resource_registry=resource_registry, parent=self, autofocus=autofocus, - **kw + **kw, ) ) child_count += 1 diff --git a/deform/form.py b/deform/form.py index 684b1905..d27321e6 100644 --- a/deform/form.py +++ b/deform/form.py @@ -139,6 +139,7 @@ def __init__( # Use kwargs to pass flags to descendant fields; saves cluttering # the constructor kw["focus"] = self.focus + self.formid = formid field.Field.__init__(self, schema, **kw) _buttons = [] for button in buttons: @@ -148,7 +149,6 @@ def __init__( self.action = action self.method = method self.buttons = _buttons - self.formid = formid self.use_ajax = use_ajax self.ajax_options = Markup(ajax_options.strip()) form_widget = getattr(schema, "widget", None) diff --git a/deform/tests/test_form.py b/deform/tests/test_form.py index b7e0b35d..862d21b4 100644 --- a/deform/tests/test_form.py +++ b/deform/tests/test_form.py @@ -162,6 +162,27 @@ def test_issue_71(self): 1, ) + def test_oid_inherits_formid( + self, + ): # https://github.com/Pylons/deform/issues/394 + # Pyramid + import colander + + # Deform + import deform + + class FooForm(colander.Schema): + foo_field = colander.SchemaNode(colander.String()) + + class BarForm(colander.Schema): + bar_field = colander.SchemaNode(colander.String()) + + foo_form = deform.Form(FooForm(), formid="fooForm") + bar_form = deform.Form(BarForm(), formid="barForm") + self.assertNotEqual( + foo_form["foo_field"].oid, bar_form["bar_field"].oid + ) + class TestButton(unittest.TestCase): def _makeOne(self, **kw): From 28e451b762dfdbe3f0c76b8a0c5e7f9a8ca8fc48 Mon Sep 17 00:00:00 2001 From: Eugen Zagorodniy Date: Sat, 23 Oct 2021 00:11:37 +0300 Subject: [PATCH 2/3] More readable line break --- deform/tests/test_form.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deform/tests/test_form.py b/deform/tests/test_form.py index 862d21b4..e2173a69 100644 --- a/deform/tests/test_form.py +++ b/deform/tests/test_form.py @@ -162,9 +162,9 @@ def test_issue_71(self): 1, ) - def test_oid_inherits_formid( - self, - ): # https://github.com/Pylons/deform/issues/394 + def test_oid_inherits_formid(self): + # See https://github.com/Pylons/deform/issues/394 + # Pyramid import colander From c896125306032adba3aed0725a6804736302137d Mon Sep 17 00:00:00 2001 From: Eugen Zagorodniy Date: Sat, 23 Oct 2021 12:30:43 +0300 Subject: [PATCH 3/3] Move parent & oid initialization to the top --- deform/field.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/deform/field.py b/deform/field.py index 1f56ab03..1d584ede 100644 --- a/deform/field.py +++ b/deform/field.py @@ -187,6 +187,11 @@ def __init__( ): self.counter = counter or itertools.count() self.order = next(self.counter) + if parent is not None: + parent = weakref.ref(parent) + self._parent = parent + oid_prefix = getattr(self.get_root(), "formid", "deform") + self.oid = getattr(schema, "oid", f"{oid_prefix}Field{self.order}") self.schema = schema self.typ = schema.typ # required by Invalid exception self.name = schema.name @@ -221,11 +226,6 @@ def __init__( self.resource_registry = resource_registry self.children = [] - if parent is not None: - parent = weakref.ref(parent) - self._parent = parent - oid_prefix = getattr(self.get_root(), "formid", "deform") - self.oid = getattr(schema, "oid", f"{oid_prefix}Field{self.order}") self.__dict__.update(kw) first_input_index = -1