Skip to content

Commit

Permalink
Improve load methods for components and gridRow, with is_form arg.
Browse files Browse the repository at this point in the history
Improve the load methods for components and `gridRow`, by passing
whether it applies to a Form `is_form`, otherwise it's obtained as a
Builder.

Ensure an empty form `gridRow` doesn't appear in a grid's `rows`
property, made possible by the other `is_form` change.
  • Loading branch information
bobslee committed Jan 11, 2024
1 parent 1ec40ec commit c46b8b2
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 19 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.2.5

Improve the load methods for components and `gridRow`, by passing
whether it applies to a Form `is_form`, otherwise it's obtained as a
Builder.

Ensure an empty form `gridRow` doesn't appear in a grid's `rows`
property, made possible by the other `is_form` change.

## 1.2.4

Implementation of "simple" validation required.
Expand Down
2 changes: 1 addition & 1 deletion formiodata/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _load_components(self, components, parent=None):
if 'type' in component:
component_obj = self.get_component_object(component)
# start and traverse from toplevel
component_obj.load(component_owner=self, parent=None, data=None)
component_obj.load(component_owner=self, parent=None, data=None, is_form=False)
self.components[component_obj.key] = component_obj

def get_component_class(self, component):
Expand Down
2 changes: 1 addition & 1 deletion formiodata/components/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class buttonComponent(Component):
def is_form_component(self):
return False

def load_data(self, data):
def load_data(self, data, is_form=False):
# just bypass this
pass
10 changes: 8 additions & 2 deletions formiodata/components/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

class columnsComponent(layoutComponentBase):

def load_data(self, data):
def load_data(self, data, is_form=False):
for column in self.raw['columns']:
for component in column['components']:
# Only determine and load class if component type.
if 'type' in component:
component_obj = self.builder.get_component_object(component)
component_obj.load(self.child_component_owner, parent=self, data=data, all_data=self._all_data)
component_obj.load(
self.child_component_owner,
parent=self,
data=data,
all_data=self._all_data,
is_form=is_form,
)

@property
def rows(self):
Expand Down
6 changes: 3 additions & 3 deletions formiodata/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def __init__(self, raw, builder, **kwargs):
self.html_component = ""
self.defaultValue = self.raw.get('defaultValue')

def load(self, component_owner, parent=None, data=None, all_data=None):
def load(self, component_owner, parent=None, data=None, all_data=None, is_form=False):
self.component_owner = component_owner

if parent:
self.parent = parent

self._all_data = all_data
self.load_data(data)
self.load_data(data, is_form=is_form)

self.builder.component_ids[self.id] = self

Expand All @@ -67,7 +67,7 @@ def load(self, component_owner, parent=None, data=None, all_data=None):
builder_path_key = '.'.join(builder_path_keys)
self.builder.components_path_key[builder_path_key] = self

def load_data(self, data):
def load_data(self, data, is_form=False):
if self.input and data:
try:
self.value = data[self.key]
Expand Down
10 changes: 8 additions & 2 deletions formiodata/components/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

class fieldsetComponent(layoutComponentBase):

def load_data(self, data):
def load_data(self, data, is_form=False):
for component in self.raw.get('components', []):
# Only determine and load class if component type.
if 'type' in component:
component_obj = self.builder.get_component_object(component)
component_obj.load(self.child_component_owner, parent=self, data=data, all_data=self._all_data)
component_obj.load(
self.child_component_owner,
parent=self,
data=data,
all_data=self._all_data,
is_form=is_form,
)
4 changes: 2 additions & 2 deletions formiodata/components/grid_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create_component_objects(self, parent, data):
component_obj.load(component_owner=parent, parent=parent, data=data, all_data=self._all_data)
parent.components[component_obj.key] = component_obj

def load_data(self, data):
def load_data(self, data, is_form=False):
# Always instantiate child components, even if no data.
# This makes it exist both in the builder and in the form.
self.create_component_objects(self, data)
Expand All @@ -65,7 +65,7 @@ def load_data(self, data):
self._load_rows(data[self.key])
self.value = data[self.key]
self.raw_value = data[self.key]
elif not self.initEmpty:
elif not self.initEmpty and not is_form:
self.rows = [self.gridRow(self, None)]

def _load_rows(self, data):
Expand Down
10 changes: 8 additions & 2 deletions formiodata/components/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

class panelComponent(layoutComponentBase):

def load_data(self, data):
def load_data(self, data, is_form=False):
for component in self.raw.get('components', []):
# Only determine and load class if component type.
if 'type' in component:
component_obj = self.builder.get_component_object(component)
component_obj.load(self.child_component_owner, parent=self, data=data, all_data=self._all_data)
component_obj.load(
self.child_component_owner,
parent=self,
data=data,
all_data=self._all_data,
is_form=is_form,
)

@property
def title(self):
Expand Down
10 changes: 8 additions & 2 deletions formiodata/components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, raw, builder, **kwargs):
self.rows = []
super().__init__(raw, builder, **kwargs)

def load_data(self, data):
def load_data(self, data, is_form=False):
self.rows = []

for data_row in self.raw.get('rows', []):
Expand All @@ -21,7 +21,13 @@ def load_data(self, data):
# Only determine and load class if component type.
if 'type' in component:
component_obj = self.builder.get_component_object(component)
component_obj.load(self.child_component_owner, parent=self, data=data, all_data=self._all_data)
component_obj.load(
self.child_component_owner,
parent=self,
data=data,
all_data=self._all_data,
is_form=is_form,
)
components.append(component_obj)

row.append({'column': col, 'components': components})
Expand Down
10 changes: 8 additions & 2 deletions formiodata/components/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class tabsComponent(layoutComponentBase):

def load_data(self, data):
def load_data(self, data, is_form=False):
self.tabs = []

for data_tab in self.raw.get('components', []):
Expand All @@ -16,7 +16,13 @@ def load_data(self, data):
# Only determine and load class if component type.
if 'type' in component:
component_obj = self.builder.get_component_object(component)
component_obj.load(self.child_component_owner, parent=self, data=data, all_data=self._all_data)
component_obj.load(
self.child_component_owner,
parent=self,
data=data,
all_data=self._all_data,
is_form=is_form,
)
tab['components'].append(component_obj)

self.tabs.append(tab)
8 changes: 7 additions & 1 deletion formiodata/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ def load_components(self):
for key, component in self.builder.components.items():
# New object, don't affect the Builder component
component_obj = self.builder.get_component_object(component.raw)
component_obj.load(component_owner=self, parent=None, data=self.form, all_data=self.form)
component_obj.load(
component_owner=self,
parent=None,
data=self.form,
all_data=self.form,
is_form=True,
)
self.components[key] = component_obj
self.component_ids[component_obj.id] = component_obj

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "formio-data"
version = "1.2.4"
version = "1.2.5"
homepage = "https://github.com/novacode-nl/python-formio-data"
description = "formio.js JSON-data API"
readme = "README.md"
Expand Down

0 comments on commit c46b8b2

Please sign in to comment.