Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 6f9915c | 2013-01-16 17:48:17 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 6 | from frappe import _ |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 7 | from frappe.custom.doctype.custom_field.custom_field import create_custom_field |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to |
| 9 | from frappe.installer import update_site_config |
| 10 | from frappe.utils import cint |
Rushabh Mehta | 6f9915c | 2013-01-16 17:48:17 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | from erpnext.accounts.doctype.cash_flow_mapper.default_cash_flow_mapper import DEFAULT_MAPPERS |
| 13 | from erpnext.setup.default_energy_point_rules import get_default_energy_point_rules |
| 14 | |
| 15 | from .default_success_action import get_default_success_action |
| 16 | |
Anand Doshi | 56198f4 | 2014-06-26 12:47:45 +0530 | [diff] [blame] | 17 | default_mail_footer = """<div style="padding: 7px; text-align: right; color: #888"><small>Sent via |
Anand Doshi | 7f41ff2 | 2014-06-26 12:02:55 +0530 | [diff] [blame] | 18 | <a style="color: #888" href="http://erpnext.org">ERPNext</a></div>""" |
| 19 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 20 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 21 | def after_install(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | frappe.get_doc({"doctype": "Role", "role_name": "Analytics"}).insert() |
Rushabh Mehta | 33003c6 | 2014-04-30 11:16:02 +0530 | [diff] [blame] | 23 | set_single_defaults() |
Ankush Menat | 9a99f99 | 2022-01-23 17:38:04 +0530 | [diff] [blame] | 24 | create_print_setting_custom_fields() |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 25 | add_all_roles_to("Administrator") |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 26 | create_default_cash_flow_mapper_templates() |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 27 | create_default_success_action() |
Suraj Shetty | 627a3dc | 2019-09-17 15:54:41 +0530 | [diff] [blame] | 28 | create_default_energy_point_rules() |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 29 | add_company_to_session_defaults() |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 30 | add_standard_navbar_items() |
Shivam Mishra | 1323a9a | 2020-09-30 16:33:14 +0530 | [diff] [blame] | 31 | add_app_name() |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 32 | add_non_standard_user_types() |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 33 | frappe.db.commit() |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 34 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 35 | |
Rushabh Mehta | d55bdcf | 2015-12-31 11:12:48 +0530 | [diff] [blame] | 36 | def check_setup_wizard_not_completed(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 37 | if cint(frappe.db.get_single_value("System Settings", "setup_complete") or 0): |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 38 | message = """ERPNext can only be installed on a fresh site where the setup wizard is not completed. |
Faris Ansari | f3a6e30 | 2020-07-08 10:45:55 +0530 | [diff] [blame] | 39 | You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | frappe.throw(message) # nosemgrep |
Rushabh Mehta | d55bdcf | 2015-12-31 11:12:48 +0530 | [diff] [blame] | 41 | |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 42 | |
Pratik Vyas | 28da752 | 2014-02-19 20:53:45 +0530 | [diff] [blame] | 43 | def set_single_defaults(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | for dt in ( |
| 45 | "Accounts Settings", |
| 46 | "Print Settings", |
| 47 | "HR Settings", |
| 48 | "Buying Settings", |
| 49 | "Selling Settings", |
| 50 | "Stock Settings", |
| 51 | ): |
| 52 | default_values = frappe.db.sql( |
| 53 | """select fieldname, `default` from `tabDocField` |
| 54 | where parent=%s""", |
| 55 | dt, |
| 56 | ) |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 57 | if default_values: |
| 58 | try: |
Ankush Menat | c3e1f0e | 2022-03-27 11:52:31 +0530 | [diff] [blame] | 59 | doc = frappe.get_doc(dt, dt) |
Anand Doshi | eb7fea6 | 2014-03-19 17:10:01 +0530 | [diff] [blame] | 60 | for fieldname, value in default_values: |
Ankush Menat | c3e1f0e | 2022-03-27 11:52:31 +0530 | [diff] [blame] | 61 | doc.set(fieldname, value) |
| 62 | doc.flags.ignore_mandatory = True |
| 63 | doc.save() |
Rushabh Mehta | b62ed7a | 2016-10-13 11:00:00 +0530 | [diff] [blame] | 64 | except frappe.ValidationError: |
| 65 | pass |
Rushabh Mehta | d42167e | 2016-05-11 16:47:14 +0530 | [diff] [blame] | 66 | |
rtdany10 | 227466c | 2021-09-04 14:04:56 +0530 | [diff] [blame] | 67 | frappe.db.set_default("date_format", "dd-mm-yyyy") |
Deepesh Garg | e2dab6f | 2022-01-10 17:31:38 +0530 | [diff] [blame] | 68 | |
| 69 | setup_currency_exchange() |
| 70 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 71 | |
Deepesh Garg | e2dab6f | 2022-01-10 17:31:38 +0530 | [diff] [blame] | 72 | def setup_currency_exchange(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | ces = frappe.get_single("Currency Exchange Settings") |
rtdany10 | 227466c | 2021-09-04 14:04:56 +0530 | [diff] [blame] | 74 | try: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | ces.set("result_key", []) |
| 76 | ces.set("req_params", []) |
Deepesh Garg | e2dab6f | 2022-01-10 17:31:38 +0530 | [diff] [blame] | 77 | |
| 78 | ces.api_endpoint = "https://frankfurter.app/{transaction_date}" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | ces.append("result_key", {"key": "rates"}) |
| 80 | ces.append("result_key", {"key": "{to_currency}"}) |
| 81 | ces.append("req_params", {"key": "base", "value": "{from_currency}"}) |
| 82 | ces.append("req_params", {"key": "symbols", "value": "{to_currency}"}) |
rtdany10 | 227466c | 2021-09-04 14:04:56 +0530 | [diff] [blame] | 83 | ces.save() |
| 84 | except frappe.ValidationError: |
| 85 | pass |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 86 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 87 | |
Ankush Menat | 9a99f99 | 2022-01-23 17:38:04 +0530 | [diff] [blame] | 88 | def create_print_setting_custom_fields(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | create_custom_field( |
| 90 | "Print Settings", |
| 91 | { |
| 92 | "label": _("Compact Item Print"), |
| 93 | "fieldname": "compact_item_print", |
| 94 | "fieldtype": "Check", |
| 95 | "default": 1, |
| 96 | "insert_after": "with_letterhead", |
| 97 | }, |
| 98 | ) |
| 99 | create_custom_field( |
| 100 | "Print Settings", |
| 101 | { |
| 102 | "label": _("Print UOM after Quantity"), |
| 103 | "fieldname": "print_uom_after_quantity", |
| 104 | "fieldtype": "Check", |
| 105 | "default": 0, |
| 106 | "insert_after": "compact_item_print", |
| 107 | }, |
| 108 | ) |
| 109 | create_custom_field( |
| 110 | "Print Settings", |
| 111 | { |
| 112 | "label": _("Print taxes with zero amount"), |
| 113 | "fieldname": "print_taxes_with_zero_amount", |
| 114 | "fieldtype": "Check", |
| 115 | "default": 0, |
| 116 | "insert_after": "allow_print_for_cancelled", |
| 117 | }, |
| 118 | ) |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def create_default_cash_flow_mapper_templates(): |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 122 | for mapper in DEFAULT_MAPPERS: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 123 | if not frappe.db.exists("Cash Flow Mapper", mapper["section_name"]): |
tundebabzy | cad22db | 2018-02-22 06:38:36 +0100 | [diff] [blame] | 124 | doc = frappe.get_doc(mapper) |
| 125 | doc.insert(ignore_permissions=True) |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 126 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 127 | |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 128 | def create_default_success_action(): |
| 129 | for success_action in get_default_success_action(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 130 | if not frappe.db.exists("Success Action", success_action.get("ref_doctype")): |
Suraj Shetty | 00cced1 | 2018-05-03 19:06:32 +0530 | [diff] [blame] | 131 | doc = frappe.get_doc(success_action) |
| 132 | doc.insert(ignore_permissions=True) |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 133 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 134 | |
Suraj Shetty | 627a3dc | 2019-09-17 15:54:41 +0530 | [diff] [blame] | 135 | def create_default_energy_point_rules(): |
| 136 | |
| 137 | for rule in get_default_energy_point_rules(): |
| 138 | # check if any rule for ref. doctype exists |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 139 | rule_exists = frappe.db.exists( |
| 140 | "Energy Point Rule", {"reference_doctype": rule.get("reference_doctype")} |
| 141 | ) |
| 142 | if rule_exists: |
| 143 | continue |
Suraj Shetty | 627a3dc | 2019-09-17 15:54:41 +0530 | [diff] [blame] | 144 | doc = frappe.get_doc(rule) |
| 145 | doc.insert(ignore_permissions=True) |
| 146 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 147 | |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 148 | def add_company_to_session_defaults(): |
| 149 | settings = frappe.get_single("Session Default Settings") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 150 | settings.append("session_defaults", {"ref_doctype": "Company"}) |
Rucha Mahabal | 4865eab | 2019-07-24 13:37:54 +0530 | [diff] [blame] | 151 | settings.save() |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 152 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 153 | |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 154 | def add_standard_navbar_items(): |
| 155 | navbar_settings = frappe.get_single("Navbar Settings") |
| 156 | |
| 157 | erpnext_navbar_items = [ |
| 158 | { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | "item_label": "Documentation", |
| 160 | "item_type": "Route", |
| 161 | "route": "https://erpnext.com/docs/user/manual", |
| 162 | "is_standard": 1, |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 163 | }, |
| 164 | { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 165 | "item_label": "User Forum", |
| 166 | "item_type": "Route", |
| 167 | "route": "https://discuss.erpnext.com", |
| 168 | "is_standard": 1, |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 169 | }, |
| 170 | { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 171 | "item_label": "Report an Issue", |
| 172 | "item_type": "Route", |
| 173 | "route": "https://github.com/frappe/erpnext/issues", |
| 174 | "is_standard": 1, |
| 175 | }, |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 176 | ] |
| 177 | |
prssanna | 194dd12 | 2021-03-24 16:45:32 +0530 | [diff] [blame] | 178 | current_navbar_items = navbar_settings.help_dropdown |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 179 | navbar_settings.set("help_dropdown", []) |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 180 | |
| 181 | for item in erpnext_navbar_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | current_labels = [item.get("item_label") for item in current_navbar_items] |
| 183 | if not item.get("item_label") in current_labels: |
| 184 | navbar_settings.append("help_dropdown", item) |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 185 | |
prssanna | 194dd12 | 2021-03-24 16:45:32 +0530 | [diff] [blame] | 186 | for item in current_navbar_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 187 | navbar_settings.append( |
| 188 | "help_dropdown", |
| 189 | { |
| 190 | "item_label": item.item_label, |
| 191 | "item_type": item.item_type, |
| 192 | "route": item.route, |
| 193 | "action": item.action, |
| 194 | "is_standard": item.is_standard, |
| 195 | "hidden": item.hidden, |
| 196 | }, |
| 197 | ) |
Deepesh Garg | a306af8 | 2020-08-06 20:52:02 +0530 | [diff] [blame] | 198 | |
| 199 | navbar_settings.save() |
Shivam Mishra | 1323a9a | 2020-09-30 16:33:14 +0530 | [diff] [blame] | 200 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 201 | |
Shivam Mishra | 1323a9a | 2020-09-30 16:33:14 +0530 | [diff] [blame] | 202 | def add_app_name(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 203 | frappe.db.set_value("System Settings", None, "app_name", "ERPNext") |
| 204 | |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 205 | |
| 206 | def add_non_standard_user_types(): |
| 207 | user_types = get_user_types_data() |
| 208 | |
| 209 | user_type_limit = {} |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 210 | for user_type, data in user_types.items(): |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 211 | user_type_limit.setdefault(frappe.scrub(user_type), 20) |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 212 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 213 | update_site_config("user_type_doctype_limit", user_type_limit) |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 214 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 215 | for user_type, data in user_types.items(): |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 216 | create_custom_role(data) |
| 217 | create_user_type(user_type, data) |
| 218 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 219 | |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 220 | def get_user_types_data(): |
| 221 | return { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | "Employee Self Service": { |
| 223 | "role": "Employee Self Service", |
| 224 | "apply_user_permission_on": "Employee", |
| 225 | "user_id_field": "user_id", |
| 226 | "doctypes": { |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 227 | # masters |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 228 | "Holiday List": ["read"], |
| 229 | "Employee": ["read", "write"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 230 | # payroll |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 231 | "Salary Slip": ["read"], |
| 232 | "Employee Benefit Application": ["read", "write", "create", "delete"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 233 | # expenses |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 234 | "Expense Claim": ["read", "write", "create", "delete"], |
| 235 | "Employee Advance": ["read", "write", "create", "delete"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 236 | # leave and attendance |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 237 | "Leave Application": ["read", "write", "create", "delete"], |
| 238 | "Attendance Request": ["read", "write", "create", "delete"], |
| 239 | "Compensatory Leave Request": ["read", "write", "create", "delete"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 240 | # tax |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | "Employee Tax Exemption Declaration": ["read", "write", "create", "delete"], |
| 242 | "Employee Tax Exemption Proof Submission": ["read", "write", "create", "delete"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 243 | # projects |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 244 | "Timesheet": ["read", "write", "create", "delete", "submit", "cancel", "amend"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 245 | # trainings |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | "Training Program": ["read"], |
| 247 | "Training Feedback": ["read", "write", "create", "delete", "submit", "cancel", "amend"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 248 | # shifts |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 249 | "Shift Request": ["read", "write", "create", "delete", "submit", "cancel", "amend"], |
Rucha Mahabal | 6c1c85b | 2022-01-27 00:51:29 +0530 | [diff] [blame] | 250 | # misc |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 251 | "Employee Grievance": ["read", "write", "create", "delete"], |
| 252 | "Employee Referral": ["read", "write", "create", "delete"], |
| 253 | "Travel Request": ["read", "write", "create", "delete"], |
| 254 | }, |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 258 | |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 259 | def create_custom_role(data): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 260 | if data.get("role") and not frappe.db.exists("Role", data.get("role")): |
| 261 | frappe.get_doc( |
| 262 | {"doctype": "Role", "role_name": data.get("role"), "desk_access": 1, "is_custom": 1} |
| 263 | ).insert(ignore_permissions=True) |
| 264 | |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 265 | |
| 266 | def create_user_type(user_type, data): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 267 | if frappe.db.exists("User Type", user_type): |
| 268 | doc = frappe.get_cached_doc("User Type", user_type) |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 269 | doc.user_doctypes = [] |
| 270 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 271 | doc = frappe.new_doc("User Type") |
| 272 | doc.update( |
| 273 | { |
| 274 | "name": user_type, |
| 275 | "role": data.get("role"), |
| 276 | "user_id_field": data.get("user_id_field"), |
| 277 | "apply_user_permission_on": data.get("apply_user_permission_on"), |
| 278 | } |
| 279 | ) |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 280 | |
| 281 | create_role_permissions_for_doctype(doc, data) |
| 282 | doc.save(ignore_permissions=True) |
| 283 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 284 | |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 285 | def create_role_permissions_for_doctype(doc, data): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | for doctype, perms in data.get("doctypes").items(): |
| 287 | args = {"document_type": doctype} |
Rohit Waghchaure | 03635fb | 2021-01-18 15:06:35 +0530 | [diff] [blame] | 288 | for perm in perms: |
| 289 | args[perm] = 1 |
| 290 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 291 | doc.append("user_doctypes", args) |
| 292 | |
Rohit Waghchaure | a8f78fa | 2021-04-13 18:43:57 +0530 | [diff] [blame] | 293 | |
| 294 | def update_select_perm_after_install(): |
| 295 | if not frappe.flags.update_select_perm_after_migrate: |
| 296 | return |
| 297 | |
| 298 | frappe.flags.ignore_select_perm = False |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | for row in frappe.get_all("User Type", filters={"is_standard": 0}): |
| 300 | print("Updating user type :- ", row.name) |
| 301 | doc = frappe.get_doc("User Type", row.name) |
Rohit Waghchaure | a8f78fa | 2021-04-13 18:43:57 +0530 | [diff] [blame] | 302 | doc.save() |
| 303 | |
| 304 | frappe.flags.update_select_perm_after_migrate = False |