Created
February 16, 2025 21:15
-
-
Save esafwan/cc823f944bbe9f76a50a8e0200729a4e to your computer and use it in GitHub Desktop.
Frappe, ERPNext Doctype Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Frappe DocType Generator | |
======================= | |
A utility to generate DocType JSON with standard settings and fields. | |
DocType Properties | |
---------------- | |
1. Basic Settings: | |
- name: Name of the DocType | |
- module: Module where DocType belongs | |
- custom: Whether it's a custom DocType (0 or 1) | |
- is_submittable: Allow submit/cancel workflow (0 or 1) | |
- is_tree: Tree structure for parent-child (0 or 1) | |
- quick_entry: Allow quick entry form (0 or 1) | |
- track_changes: Track all document changes (0 or 1) | |
2. Naming Settings: | |
- naming_rule: How documents are named | |
* "Set by user": Manual naming | |
* "By fieldname": Use field value | |
* "By script": Use naming series | |
* "Random": Auto-generated random names | |
* "By sequence": Auto-incrementing numbers | |
- autoname: Pattern for automatic naming | |
* field:[fieldname] | |
* naming_series: | |
* format:### or YYYY.MM.DD.### | |
3. Form Settings: | |
- title_field: Field used as title | |
- image_field: Field for image preview | |
- timeline_field: Field for timeline tracking | |
- sort_field: Default sort field | |
- sort_order: "DESC" or "ASC" | |
- search_fields: Comma-separated searchable fields | |
- allow_copy: Allow document copy (0 or 1) | |
- allow_rename: Allow document rename (0 or 1) | |
- allow_import: Allow data import (0 or 1) | |
- max_attachments: Maximum attachments allowed | |
Field Types | |
---------- | |
1. Basic Fields: | |
- Data: Short text | |
- Text: Multi-line text | |
- Text Editor: Rich text editor | |
- Int: Whole numbers | |
- Float: Decimal numbers | |
- Currency: Money values | |
- Percent: Percentage values | |
- Check: Yes/No checkbox | |
- Date: Date picker | |
- Time: Time picker | |
- Datetime: Date and time | |
- Password: Masked input | |
2. Select Fields: | |
- Select: Dropdown with options | |
- Link: Link to another DocType | |
- Dynamic Link: Dynamic reference | |
- Table: Child table | |
- Table MultiSelect: Multiple select table | |
- Attach: File attachment | |
- Attach Image: Image attachment | |
- Signature: Digital signature | |
3. Display Fields: | |
- HTML: Custom HTML | |
- Markdown Editor: Markdown input | |
- Heading: Section heading | |
- Column Break: Multi-column layout | |
- Section Break: Form sections | |
- Tab Break: Form tabs | |
- Fold: Collapsible sections | |
- Button: Action button | |
- Code: Code editor | |
- Color: Color picker | |
- Barcode: Barcode field | |
- Geolocation: Maps and coordinates | |
Field Properties | |
-------------- | |
1. Basic Properties: | |
- label: Display label | |
- fieldname: Technical name | |
- fieldtype: Type of field | |
- reqd: Mandatory (0 or 1) | |
- unique: Unique value (0 or 1) | |
- default: Default value | |
- length: Maximum length | |
- options: Options for Select/Link | |
- description: Help text | |
- read_only: Non-editable (0 or 1) | |
- hidden: Not visible (0 or 1) | |
- depends_on: Conditional display | |
- permlevel: Permission level | |
- precision: Decimal places | |
- width: Column width | |
2. Display Properties: | |
- in_list_view: Show in list view (0 or 1) | |
- in_standard_filter: Show in filters (0 or 1) | |
- in_preview: Show in preview (0 or 1) | |
- in_global_search: Include in search (0 or 1) | |
- bold: Bold text (0 or 1) | |
- allow_in_quick_entry: Quick entry (0 or 1) | |
- translatable: Can be translated (0 or 1) | |
- fetch_from: Auto-fetch value | |
- fetch_if_empty: Fetch if empty (0 or 1) | |
3. Validation Properties: | |
- mandatory_depends_on: Conditional mandatory | |
- read_only_depends_on: Conditional read-only | |
- allow_bulk_edit: Bulk edit allowed (0 or 1) | |
- no_copy: Don't copy value (0 or 1) | |
- report_hide: Hide in reports (0 or 1) | |
- remember_last_selected_value: (0 or 1) | |
Permission Settings | |
----------------- | |
1. Role Permissions: | |
- role: User role | |
- read: Read permission (0 or 1) | |
- write: Write permission (0 or 1) | |
- create: Create permission (0 or 1) | |
- delete: Delete permission (0 or 1) | |
- submit: Submit permission (0 or 1) | |
- cancel: Cancel permission (0 or 1) | |
- amend: Amend permission (0 or 1) | |
- select: Select permission (0 or 1) | |
- export: Export permission (0 or 1) | |
- import: Import permission (0 or 1) | |
- share: Share permission (0 or 1) | |
- print: Print permission (0 or 1) | |
- email: Email permission (0 or 1) | |
- report: Report permission (0 or 1) | |
- set_user_permissions: Set user perms (0 or 1) | |
Example Usage | |
----------- | |
```python | |
fields = [ | |
{ | |
"label": "Customer Name", | |
"fieldname": "customer_name", | |
"fieldtype": "Data", | |
"reqd": 1, | |
"unique": 1, | |
"in_list_view": 1 | |
}, | |
{ | |
"label": "Address", | |
"fieldname": "address", | |
"fieldtype": "Text", | |
"reqd": 1 | |
} | |
] | |
json_str = generate_doctype_json( | |
doctype_name="Customer", | |
fields=fields, | |
module_name="Custom", | |
naming_rule="By fieldname", | |
is_submittable=True | |
) | |
``` | |
""" | |
import json | |
from typing import List, Dict, Any, Optional | |
def generate_doctype_json( | |
doctype_name: str, | |
fields: List[Dict[str, Any]], | |
module_name: str = "Custom", | |
naming_rule: str = "Set by user", | |
is_submittable: bool = False, | |
is_tree: bool = False, | |
quick_entry: bool = True, | |
track_changes: bool = True, | |
) -> str: | |
""" | |
Generate a DocType JSON with standard settings. | |
Args: | |
doctype_name (str): Name of the DocType | |
fields (List[Dict]): List of field definitions | |
module_name (str): Module name (default: "Custom") | |
naming_rule (str): Naming rule for documents | |
is_submittable (bool): Whether documents are submittable | |
is_tree (bool): Whether DocType is a Tree | |
quick_entry (bool): Enable quick entry | |
track_changes (bool): Track document changes | |
Returns: | |
str: JSON string representation of the DocType | |
Example: | |
fields = [ | |
{ | |
"label": "First Name", | |
"fieldname": "first_name", | |
}, | |
{ | |
"label": "Last Name", | |
"fieldname": "last_name", | |
} | |
] | |
json_str = generate_doctype_json("Customer", fields) | |
""" | |
# Process fields to ensure required properties | |
processed_fields = [] | |
for idx, field in enumerate(fields): | |
processed_field = { | |
"fieldname": field["fieldname"], | |
"label": field.get("label", field["fieldname"].replace("_", " ").title()), | |
"fieldtype": field.get("fieldtype", "Data"), | |
"reqd": field.get("reqd", 0), | |
"unique": field.get("unique", 0), | |
"in_list_view": field.get("in_list_view", 0), | |
"in_standard_filter": field.get("in_standard_filter", 0), | |
"in_preview": field.get("in_preview", 0), | |
"length": field.get("length", 0), | |
"precision": field.get("precision", None), | |
"default": field.get("default", None), | |
"read_only": field.get("read_only", 0), | |
"allow_in_quick_entry": field.get("allow_in_quick_entry", 1), | |
"translatable": field.get("translatable", 0), | |
"idx": idx + 1, | |
"owner": "Administrator" | |
} | |
processed_fields.append(processed_field) | |
doctype_json = { | |
"doctype": "DocType", | |
"name": doctype_name, | |
"owner": "Administrator", | |
"module": module_name, | |
"custom": 1, | |
"naming_rule": naming_rule, | |
"autoname": field.get("autoname", ""), | |
"title_field": "", | |
"image_field": "", | |
"timeline_field": "modified", | |
"sort_field": "modified", | |
"sort_order": "DESC", | |
"is_submittable": 1 if is_submittable else 0, | |
"is_tree": 1 if is_tree else 0, | |
"quick_entry": 1 if quick_entry else 0, | |
"track_changes": 1 if track_changes else 0, | |
"track_seen": 0, | |
"track_views": 0, | |
"editable_grid": 1, | |
"allow_copy": 1, | |
"allow_rename": 1, | |
"allow_import": 1, | |
"hide_toolbar": 0, | |
"max_attachments": 0, | |
"document_type": "Document", | |
"engine": "InnoDB", | |
"fields": processed_fields, | |
"permissions": [ | |
{ | |
"role": "System Manager", | |
"read": 1, | |
"write": 1, | |
"create": 1, | |
"delete": 1, | |
"submit": 1 if is_submittable else 0, | |
"cancel": 1 if is_submittable else 0, | |
"amend": 1 if is_submittable else 0, | |
"select": 1, | |
"export": 1, | |
"import": 1, | |
"share": 1, | |
"print": 1, | |
"email": 1, | |
"report": 1, | |
"set_user_permissions": 1 | |
} | |
], | |
"show_preview_popup": 0, | |
"show_name_in_global_search": 0, | |
"translated_doctype": 0, | |
"default_print_format": "Standard", | |
"default_email_template": None, | |
"email_append_to": 0, | |
"force_re_route_to_default_view": 0, | |
"icon": None, | |
"search_fields": None, | |
"subject_field": None, | |
"sender_field": None | |
} | |
return json.dumps(doctype_json, indent=4) | |
def example_usage(): | |
"""Example usage of the DocType generator""" | |
fields = [ | |
{ | |
"label": "First Name", | |
"fieldname": "first_name", | |
"reqd": 1, | |
"in_list_view": 1, | |
"in_standard_filter": 1 | |
}, | |
{ | |
"label": "Last Name", | |
"fieldname": "last_name", | |
"reqd": 1, | |
"in_list_view": 1 | |
}, | |
{ | |
"label": "Email", | |
"fieldname": "email", | |
"fieldtype": "Data", | |
"options": "Email", | |
"unique": 1 | |
}, | |
{ | |
"label": "Phone", | |
"fieldname": "phone", | |
"fieldtype": "Data" | |
} | |
] | |
json_str = generate_doctype_json( | |
doctype_name="Test Customer", | |
fields=fields, | |
module_name="Custom", | |
naming_rule="By fieldname", | |
is_submittable=False, | |
quick_entry=True | |
) | |
return json_str | |
if __name__ == "__main__": | |
# Print example DocType JSON | |
print(example_usage()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment