-
Notifications
You must be signed in to change notification settings - Fork 12
chore: make formgenerator better #7
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
Changes from all commits
9bd7011
81c605e
bbc1bd2
4c088a6
3433fb5
5823e4e
91b062e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Forms Pro | ||
| Forms Pro | ||
| User Forms |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import frappe | ||
| from frappe import _ | ||
| from frappe.share import add_docshare | ||
|
|
||
| FORMS_PRO_ROLE = "Forms Pro User" | ||
|
|
||
|
|
@@ -7,10 +9,19 @@ | |
| def create_form_with_doctype(team_id: str, doctype: str): | ||
| roles = frappe.get_roles(frappe.session.user) | ||
| if FORMS_PRO_ROLE not in roles: | ||
| frappe.throw("You are not authorized to create a form") | ||
|
|
||
| form_generator = FormGenerator(team_id=team_id, linked_doctype=doctype) | ||
| form_generator.generate() | ||
| frappe.throw( | ||
| _("You are not authorized to create a form"), | ||
| frappe.PermissionError, | ||
| ) | ||
|
|
||
| try: | ||
| form_generator = FormGenerator(team_id=team_id, linked_doctype=doctype) | ||
| form_generator.generate() | ||
| except Exception as e: | ||
| frappe.log_error(f"Error creating form with doctype: {doctype} - {e}") | ||
| frappe.throw( | ||
| _("Error creating form with doctype: {0} - {1}").format(doctype, e), | ||
| ) | ||
|
|
||
| return { | ||
| "doctype": form_generator.doctype.name, | ||
|
|
@@ -22,10 +33,19 @@ def create_form_with_doctype(team_id: str, doctype: str): | |
| def create_form(team_id: str): | ||
| roles = frappe.get_roles(frappe.session.user) | ||
| if FORMS_PRO_ROLE not in roles: | ||
| frappe.throw("You are not authorized to create a form") | ||
|
|
||
| form_generator = FormGenerator(team_id=team_id) | ||
| form_generator.generate() | ||
| frappe.throw( | ||
| _("You are not authorized to create a form"), | ||
| frappe.PermissionError, | ||
| ) | ||
|
|
||
| try: | ||
| form_generator = FormGenerator(team_id=team_id) | ||
| form_generator.generate() | ||
| except Exception as e: | ||
| frappe.log_error(f"Error creating form: {e}") | ||
| frappe.throw( | ||
| _("Error creating form: {0}").format(e), | ||
| ) | ||
|
|
||
| return { | ||
| "doctype": form_generator.doctype.name, | ||
|
|
@@ -47,46 +67,82 @@ def __init__( | |
| def generate(self) -> None: | ||
| self._initialize_doctype() | ||
| self._initialize_form_document() | ||
| frappe.clear_cache() | ||
|
|
||
| def _initialize_doctype(self) -> None: | ||
| if self.doctype: | ||
| return | ||
|
|
||
| placeholder_doctype = frappe.new_doc("DocType") | ||
| placeholder_doctype.name = self._generate_doctype_name() | ||
| placeholder_doctype.module = "Forms Pro" | ||
| placeholder_doctype.module = "User Forms" | ||
| placeholder_doctype.custom = True | ||
| placeholder_doctype.track_changes = True | ||
| placeholder_doctype.track_views = True | ||
| placeholder_doctype.index_web_pages_for_search = False | ||
| placeholder_doctype.insert(ignore_permissions=True) | ||
|
|
||
| self.doctype = placeholder_doctype | ||
| # self.set_placeholder_doctype_fields() | ||
| placeholder_doctype.is_submittable = 0 | ||
| placeholder_doctype.istable = 0 | ||
| placeholder_doctype.editable_grid = 0 | ||
| placeholder_doctype.issingle = 0 | ||
| placeholder_doctype.is_tree = 0 | ||
| placeholder_doctype.is_virtual = 0 | ||
|
|
||
| # Add permissions | ||
| placeholder_doctype.append( | ||
| "permissions", | ||
| { | ||
| "create": 1, | ||
| "delete": 1, | ||
| "email": 1, | ||
| "export": 1, | ||
| "print": 1, | ||
| "read": 1, | ||
| "report": 1, | ||
| "role": "System Manager", | ||
| "share": 1, | ||
| "write": 1, | ||
| "submit": 0, | ||
| }, | ||
| ) | ||
|
|
||
| # Add fields | ||
| placeholder_doctype.append("fields", {"fieldtype": "Section Break"}) | ||
|
|
||
| # def set_placeholder_doctype_fields(self) -> None: | ||
| # fields = [ | ||
| # { | ||
| # "label": "Is Form Pro DocType", | ||
| # "fieldname": "is_forms_pro_doctype", | ||
| # "fieldtype": "Check", | ||
| # "reqd": 1, | ||
| # "read_only": 1, | ||
| # "default": 1, | ||
| # } | ||
| # ] | ||
| placeholder_doctype.insert(ignore_permissions=True) | ||
|
|
||
| # for field in fields: | ||
| # self.doctype.append("fields", field) | ||
| add_docshare( | ||
| "DocType", | ||
| placeholder_doctype.name, | ||
| read=1, | ||
| write=1, | ||
| share=1, | ||
| submit=0, | ||
| flags={ | ||
| "ignore_share_permission": True, | ||
| }, | ||
| ) | ||
|
Comment on lines
+113
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Search for add_docshare definition in the codebase
rg "def add_docshare" -B 2 -A 10Repository: BuildWithHussain/forms_pro Length of output: 196 🏁 Script executed: # Also search for imports of add_docshare to understand where it comes from
rg "add_docshare" --type py -B 2 -A 2 | head -50Repository: BuildWithHussain/forms_pro Length of output: 196 🏁 Script executed: # Check the specific file to see the import statement
cat -n forms_pro/utils/form_generator.py | head -30Repository: BuildWithHussain/forms_pro Length of output: 196 🌐 Web query:
💡 Result: The add_docshare function signature in Frappe is commonly called as: add_docshare(doctype, name, user=None, everyone=None, read=0, write=0, submit=0, share=0).
Sources:
🌐 Web query:
💡 Result: The user parameter is the user identifier (the User.name / email string) to share the document with — e.g. frappe.session.user or each user from a User Group (pass a string like "user@example.com"). Examples and usage in the Frappe forum show calls such as add_docshare(doctype, name, user=frappe.session.user, ...) and looping over User Group members passing user.user. [1][2] Sources:
Add The 🤖 Prompt for AI Agents |
||
|
|
||
| # self.doctype.save(ignore_permissions=True) | ||
| self.doctype = placeholder_doctype | ||
|
|
||
| def _initialize_form_document(self) -> None: | ||
| form_document = frappe.new_doc("Form") | ||
| form_document.linked_doctype = self.doctype.name | ||
| form_document.title = "Untitled Form" | ||
| form_document.linked_team_id = self.team_id | ||
| form_document.document_type = "System" | ||
| form_document.insert(ignore_permissions=True) | ||
|
|
||
| add_docshare( | ||
| "Form", | ||
| form_document.name, | ||
| read=1, | ||
| write=1, | ||
| share=1, | ||
| submit=0, | ||
| flags={ | ||
| "ignore_share_permission": True, | ||
| }, | ||
| ) | ||
| self.form_document = form_document | ||
|
|
||
| def _generate_doctype_name(self) -> str: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix error handling issues.
The error handling has several issues:
Exceptionis too broad and can mask unexpected errors. Consider catching specific exceptions.frappe.throw()on Line 34 raises an exception.🔎 Proposed fix
If error handling is required for specific exceptions, catch those explicitly and remove the unreachable print statement.
🧰 Tools
🪛 Ruff (0.14.10)
32-32: Do not catch blind exception:
Exception(BLE001)
🤖 Prompt for AI Agents