2016-05-11 09:43:44 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
2017-07-18 07:00:57 +00:00
|
|
|
from frappe import _
|
2016-05-11 09:43:44 +00:00
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
def get_domain(domain):
|
2016-06-28 16:07:28 +00:00
|
|
|
'''Written as a function to prevent data mutation effects'''
|
2016-07-21 10:31:59 +00:00
|
|
|
data = {
|
2016-06-28 16:07:28 +00:00
|
|
|
'Manufacturing': {
|
|
|
|
'desktop_icons': ['Item', 'BOM', 'Customer', 'Supplier', 'Sales Order',
|
|
|
|
'Production Order', 'Stock Entry', 'Purchase Order', 'Task', 'Buying', 'Selling',
|
|
|
|
'Accounts', 'HR', 'ToDo'],
|
2017-04-29 10:11:34 +00:00
|
|
|
'remove_roles': ['Academics User', 'Instructor'],
|
2016-06-28 16:07:28 +00:00
|
|
|
'properties': [
|
|
|
|
{'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'collapsible_depends_on', 'value': 'is_stock_item'},
|
|
|
|
],
|
|
|
|
'set_value': [
|
|
|
|
['Stock Settings', None, 'show_barcode_field', 1]
|
2016-07-21 10:31:59 +00:00
|
|
|
],
|
2016-09-23 16:21:51 +00:00
|
|
|
'default_portal_role': 'Customer'
|
2016-06-28 16:07:28 +00:00
|
|
|
},
|
2016-05-20 07:24:44 +00:00
|
|
|
|
2016-06-28 16:07:28 +00:00
|
|
|
'Retail': {
|
2017-03-09 12:41:11 +00:00
|
|
|
'desktop_icons': ['POS', 'Item', 'Customer', 'Sales Invoice', 'Purchase Order',
|
|
|
|
'Warranty Claim', 'Accounts', 'Task', 'Buying', 'ToDo'],
|
2017-04-29 10:11:34 +00:00
|
|
|
'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
|
2016-06-28 16:07:28 +00:00
|
|
|
'properties': [
|
|
|
|
{'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'hidden', 'value': 1},
|
|
|
|
{'doctype': 'Customer', 'fieldname': 'credit_limit_section', 'property': 'hidden', 'value': 1},
|
|
|
|
],
|
|
|
|
'set_value': [
|
|
|
|
['Stock Settings', None, 'show_barcode_field', 1]
|
2016-07-21 10:31:59 +00:00
|
|
|
],
|
2016-09-23 16:21:51 +00:00
|
|
|
'default_portal_role': 'Customer'
|
2016-06-28 16:07:28 +00:00
|
|
|
},
|
2016-05-20 07:24:44 +00:00
|
|
|
|
2016-06-28 16:07:28 +00:00
|
|
|
'Distribution': {
|
2017-03-09 12:41:11 +00:00
|
|
|
'desktop_icons': ['Item', 'Customer', 'Supplier', 'Lead', 'Sales Order', 'Task',
|
2016-06-28 16:07:28 +00:00
|
|
|
'Sales Invoice', 'CRM', 'Selling', 'Buying', 'Stock', 'Accounts', 'HR', 'ToDo'],
|
2017-04-29 10:11:34 +00:00
|
|
|
'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
|
2016-06-28 16:07:28 +00:00
|
|
|
'set_value': [
|
|
|
|
['Stock Settings', None, 'show_barcode_field', 1]
|
2016-07-21 10:31:59 +00:00
|
|
|
],
|
2016-09-23 16:21:51 +00:00
|
|
|
'default_portal_role': 'Customer'
|
2016-06-28 16:07:28 +00:00
|
|
|
},
|
2016-05-20 07:24:44 +00:00
|
|
|
|
2016-06-28 16:07:28 +00:00
|
|
|
'Services': {
|
2017-03-09 12:41:11 +00:00
|
|
|
'desktop_icons': ['Project', 'Timesheet', 'Customer', 'Sales Order', 'Sales Invoice',
|
|
|
|
'Lead', 'Opportunity', 'Task', 'Expense Claim', 'Employee', 'HR', 'ToDo'],
|
2017-04-29 10:11:34 +00:00
|
|
|
'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User', 'Instructor'],
|
2016-06-28 16:07:28 +00:00
|
|
|
'properties': [
|
|
|
|
{'doctype': 'Item', 'fieldname': 'is_stock_item', 'property': 'default', 'value': 0},
|
|
|
|
],
|
|
|
|
'set_value': [
|
|
|
|
['Stock Settings', None, 'show_barcode_field', 0]
|
2016-07-21 10:31:59 +00:00
|
|
|
],
|
2016-09-23 16:21:51 +00:00
|
|
|
'default_portal_role': 'Customer'
|
2016-07-21 10:31:59 +00:00
|
|
|
},
|
|
|
|
'Education': {
|
|
|
|
'desktop_icons': ['Student', 'Program', 'Course', 'Student Group', 'Instructor',
|
2017-03-09 12:41:11 +00:00
|
|
|
'Fees', 'Task', 'ToDo', 'Schools'],
|
2016-09-20 11:52:54 +00:00
|
|
|
'allow_roles': ['Academics User', 'Accounts User', 'Accounts Manager', 'Item Manager',
|
2016-09-23 16:21:51 +00:00
|
|
|
'Website Manager', 'HR User', 'HR Manager', 'Purchase User', 'Purchase Manager',
|
2017-05-01 06:09:36 +00:00
|
|
|
'Student', 'Projects User', 'Instructor'],
|
2016-09-23 16:21:51 +00:00
|
|
|
'default_portal_role': 'Student'
|
2016-07-21 10:31:59 +00:00
|
|
|
},
|
2016-05-11 09:43:44 +00:00
|
|
|
}
|
2016-07-21 10:31:59 +00:00
|
|
|
if not domain in data:
|
|
|
|
raise 'Invalid Domain {0}'.format(domain)
|
|
|
|
return frappe._dict(data[domain])
|
2016-05-11 09:43:44 +00:00
|
|
|
|
|
|
|
def setup_domain(domain):
|
2016-07-21 10:31:59 +00:00
|
|
|
'''Setup roles, desktop icons, properties, values, portal sidebar menu based on domain'''
|
|
|
|
data = get_domain(domain)
|
|
|
|
setup_roles(data)
|
|
|
|
setup_desktop_icons(data)
|
|
|
|
setup_properties(data)
|
|
|
|
set_values(data)
|
|
|
|
setup_sidebar_items(data)
|
2017-07-18 07:00:57 +00:00
|
|
|
update_module_def_restrict_to_domain()
|
|
|
|
|
2016-09-23 16:21:51 +00:00
|
|
|
if data.get('default_portal_role'):
|
|
|
|
frappe.db.set_value('Portal Settings', None, 'default_role', data.get('default_portal_role'))
|
2017-07-18 07:00:57 +00:00
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
frappe.clear_cache()
|
2016-05-11 09:43:44 +00:00
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
def setup_desktop_icons(data):
|
|
|
|
'''set desktop icons form `data.desktop_icons`'''
|
2016-05-11 09:43:44 +00:00
|
|
|
from frappe.desk.doctype.desktop_icon.desktop_icon import set_desktop_icons
|
|
|
|
if data.desktop_icons:
|
|
|
|
set_desktop_icons(data.desktop_icons)
|
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
def setup_properties(data):
|
2016-05-11 09:43:44 +00:00
|
|
|
if data.properties:
|
|
|
|
for args in data.properties:
|
|
|
|
frappe.make_property_setter(args)
|
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
def setup_roles(data):
|
|
|
|
'''Add, remove roles from `data.allow_roles` or `data.remove_roles`'''
|
|
|
|
def remove_role(role):
|
2017-02-20 09:41:56 +00:00
|
|
|
frappe.db.sql('delete from `tabHas Role` where role=%s', role)
|
2016-10-25 09:31:30 +00:00
|
|
|
frappe.set_value('Role', role, 'disabled', 1)
|
2016-07-21 10:31:59 +00:00
|
|
|
|
|
|
|
if data.remove_roles:
|
|
|
|
for role in data.remove_roles:
|
|
|
|
remove_role(role)
|
|
|
|
|
|
|
|
if data.allow_roles:
|
|
|
|
# remove all roles other than allowed roles
|
2017-06-16 07:32:45 +00:00
|
|
|
active_domains = frappe.get_active_domains()
|
2016-11-05 04:39:53 +00:00
|
|
|
data.allow_roles += ['Administrator', 'Guest', 'System Manager', 'All']
|
2017-06-16 07:32:45 +00:00
|
|
|
for role in frappe.get_all('Role', filters = {"restrict_to_domain": ("not in", active_domains)}):
|
2016-07-21 10:31:59 +00:00
|
|
|
if not (role.name in data.allow_roles):
|
|
|
|
remove_role(role.name)
|
|
|
|
|
|
|
|
def set_values(data):
|
|
|
|
'''set values based on `data.set_value`'''
|
2016-05-12 10:56:01 +00:00
|
|
|
if data.set_value:
|
|
|
|
for args in data.set_value:
|
|
|
|
doc = frappe.get_doc(args[0], args[1] or args[0])
|
|
|
|
doc.set(args[2], args[3])
|
|
|
|
doc.save()
|
|
|
|
|
2016-07-21 10:31:59 +00:00
|
|
|
def setup_sidebar_items(data):
|
|
|
|
'''Enable / disable sidebar items'''
|
|
|
|
if data.allow_sidebar_items:
|
|
|
|
# disable all
|
|
|
|
frappe.db.sql('update `tabPortal Menu Item` set enabled=0')
|
|
|
|
|
|
|
|
# enable
|
|
|
|
frappe.db.sql('''update `tabPortal Menu Item` set enabled=1
|
|
|
|
where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.allow_sidebar_items])))
|
|
|
|
|
|
|
|
if data.remove_sidebar_items:
|
|
|
|
# disable all
|
|
|
|
frappe.db.sql('update `tabPortal Menu Item` set enabled=1')
|
|
|
|
|
|
|
|
# enable
|
|
|
|
frappe.db.sql('''update `tabPortal Menu Item` set enabled=0
|
|
|
|
where route in ({0})'''.format(', '.join(['"{0}"'.format(d) for d in data.remove_sidebar_items])))
|
|
|
|
|
2016-05-11 09:43:44 +00:00
|
|
|
def reset():
|
|
|
|
from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to
|
|
|
|
add_all_roles_to('Administrator')
|
|
|
|
|
2016-06-28 16:07:28 +00:00
|
|
|
frappe.db.sql('delete from `tabProperty Setter`')
|
2017-07-18 07:00:57 +00:00
|
|
|
|
|
|
|
def update_module_def_restrict_to_domain():
|
|
|
|
""" set the restrict to domain for the module def """
|
|
|
|
|
|
|
|
module_def_restrict_to_domain_mapper = {
|
2017-07-18 09:39:30 +00:00
|
|
|
"Schools": 'Education'
|
2017-07-18 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 09:39:30 +00:00
|
|
|
lang = frappe.db.get_single_value("System Settings", "language") or "en"
|
2017-07-18 07:00:57 +00:00
|
|
|
for module, domain in module_def_restrict_to_domain_mapper.iteritems():
|
2017-07-18 09:39:30 +00:00
|
|
|
if frappe.db.exists("Domain", _(domain, lang)):
|
|
|
|
frappe.set_value("Module Def", module, "restrict_to_domain", _(domain, lang))
|
|
|
|
elif frappe.db.exists("Domain", domain):
|
|
|
|
frappe.set_value("Module Def", module, "restrict_to_domain", domain)
|
|
|
|
else:
|
|
|
|
pass
|