Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parent formid a part of autogenerated oid #520

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions deform/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ 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)
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
Expand Down Expand Up @@ -222,9 +226,6 @@ def __init__(

self.resource_registry = resource_registry
self.children = []
if parent is not None:
parent = weakref.ref(parent)
self._parent = parent
self.__dict__.update(kw)

first_input_index = -1
Expand Down Expand Up @@ -254,7 +255,7 @@ def __init__(
resource_registry=resource_registry,
parent=self,
autofocus=autofocus,
**kw
**kw,
)
)
child_count += 1
Expand Down
2 changes: 1 addition & 1 deletion deform/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you moved this line? I don't understand why it was moved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formid should be set prior to children initialization, since Field.__init__ does rely on it.

field.Field.__init__(self, schema, **kw)
_buttons = []
for button in buttons:
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions deform/tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ def test_issue_71(self):
1,
)

def test_oid_inherits_formid(self):
# See 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):
Expand Down