Skip to content

Commit

Permalink
Merge pull request #278 from Q-Niranjan/17.0-1.2-formioImprove
Browse files Browse the repository at this point in the history
Map the form dynamically based on programid
  • Loading branch information
shibu-narayanan authored Dec 23, 2024
2 parents 1fe2664 + 996a609 commit 5da1836
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
55 changes: 53 additions & 2 deletions g2p_formio/models/program.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
from odoo import fields, models
import json

from odoo import api, fields, models


class G2PProgram(models.Model):
_inherit = "g2p.program"

self_service_portal_form = fields.Many2one("formio.builder", string="Program Form")
portal_form_builder_id = fields.Many2one(
"formio.builder", string="Program Form", domain="[('is_form_mapped_with_program', '=', False)]"
)

is_multiple_form_submission = fields.Boolean(default=False)

@api.constrains("portal_form_builder_id")
def _constrain_portal_form_mapping(self):
self.ensure_one()

if self.portal_form_builder_id:
formio_builder = self.portal_form_builder_id

try:
# Parse the JSON string into a dictionary
js_options = json.loads(formio_builder.formio_js_options)

if "editForm" in js_options and "file" in js_options["editForm"]:
file_components = js_options["editForm"]["file"][0]["components"]

for component in file_components:
if "defaultValue" in component and component["key"] == "url":
component["defaultValue"] = f"/v1/selfservice/uploadDocument/{self.id}"

# Convert back to JSON string and update the builder
formio_builder.write(
{
"formio_js_options": json.dumps(js_options, indent=4),
"is_form_mapped_with_program": True,
}
)
else:
formio_builder.write({"is_form_mapped_with_program": False})

except (json.JSONDecodeError, KeyError, IndexError):
# Handle potential JSON parsing errors or missing keys
formio_builder.write({"is_form_mapped_with_program": False})

@api.onchange("portal_form_builder_id")
def _onchange_portal_form_unmapping(self):
# Check if there was a previous form that is now being removed
previous_form = self._origin.portal_form_builder_id
current_form = self.portal_form_builder_id

if previous_form and not current_form:
previous_form.write({"is_form_mapped_with_program": False})


class G2PProgramFomio(models.Model):
_inherit = "formio.builder"

is_form_mapped_with_program = fields.Boolean(string="Is Form Mapped", default=False)
7 changes: 5 additions & 2 deletions g2p_formio/views/formio_builder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<xpath expr="//group[@name='publish_settings']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//group[@name='other_settings']" position="attributes">
<attribute name="invisible">1</attribute>
<xpath expr="//group[@name='other_settings']" position="inside">
<field name="is_form_mapped_with_program" invisible="1" />
</xpath>
<!-- <xpath expr="//group[@name='formiojs_settings']" position="attributes">
<attribute name="invisible">1</attribute>
Expand All @@ -48,6 +48,9 @@
<field name="formio_version_id" position="attributes">
<attribute name="column_invisible">1</attribute>
</field>
<field name="name" position="after">
<field name="is_form_mapped_with_program" column_invisible="0" />
</field>
</data>
</field>
</record>
Expand Down
2 changes: 1 addition & 1 deletion g2p_formio/views/program_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Part of OpenG2P. See LICENSE file for full copyright and licensing details.
<page string="Portal Form">
<group>
<field name="is_multiple_form_submission" />
<field name="self_service_portal_form" />
<field name="portal_form_builder_id" />
</group>
</page>
</xpath>
Expand Down

0 comments on commit 5da1836

Please sign in to comment.