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