@@ -18,6 +18,10 @@ from frappe.social.doctype.energy_point_log.energy_point_log import get_energy_p
from frappe.model.base_document import get_controller
from frappe.model.base_document import get_controller
from frappe.core.doctype.navbar_settings.navbar_settings import get_navbar_settings, get_app_logo
from frappe.core.doctype.navbar_settings.navbar_settings import get_navbar_settings, get_app_logo
from frappe.utils import get_time_zone, add_user_info
from frappe.utils import get_time_zone, add_user_info
from frappe.query_builder import DocType
from frappe.query_builder.functions import Count
from frappe.query_builder.terms import subqry
def get_bootinfo():
def get_bootinfo():
"""build and return boot info"""
"""build and return boot info"""
@@ -129,43 +133,53 @@ def get_user_pages_or_reports(parent, cache=False):
roles = frappe.get_roles()
roles = frappe.get_roles()
has_role = {}
has_role = {}
column = get_column(parent)
page = DocType("Page")
report = DocType("Report")
if parent == "Report":
columns = (report.name.as_("title"), report.ref_doctype, report.report_type)
else:
columns = (page.title.as_("title"), )
customRole = DocType("Custom Role")
hasRole = DocType("Has Role")
parentTable = DocType(parent)
# get pages or reports set on custom role
# get pages or reports set on custom role
pages_with_custom_roles = frappe.db.sql("""
select
`tabCustom Role`.{field} as name,
`tabCustom Role`.modified,
`tabCustom Role`.ref_doctype,
{column}
from `tabCustom Role`, `tabHas Role`, `tab{parent}`
where
`tabHas Role`.parent = `tabCustom Role`.name
and `tab{parent}`.name = `tabCustom Role`.{field}
and `tabCustom Role`.{field} is not null
and `tabHas Role`.role in ({roles})
""".format(field=parent.lower(), parent=parent, column=column,
roles = ', '.join(['%s']*len(roles))), roles, as_dict=1)
pages_with_custom_roles = (
frappe.qb.from_(customRole).from_(hasRole).from_(parentTable)
.select(customRole[parent.lower()].as_("name"), customRole.modified, customRole.ref_doctype, *columns)
.where(
(hasRole.parent == customRole.name)
& (parentTable.name == customRole[parent.lower()])
& (customRole[parent.lower()].isnotnull())
& (hasRole.role.isin(roles)))
).run(as_dict=True)
for p in pages_with_custom_roles:
for p in pages_with_custom_roles:
has_role[p.name] = {"modified":p.modified, "title": p.title, "ref_doctype": p.ref_doctype}
has_role[p.name] = {"modified":p.modified, "title": p.title, "ref_doctype": p.ref_doctype}
pages_with_standard_roles = frappe.db.sql("""
select distinct
`tab{parent}`.name as name,
`tab{parent}`.modified,
{column}
from `tabHas Role`, `tab{parent}`
where
`tabHas Role`.role in ({roles})
and `tabHas Role`.parent = `tab{parent}`.name
and `tab{parent}`.`name` not in (
select `tabCustom Role`.{field} from `tabCustom Role`
where `tabCustom Role`.{field} is not null)
{condition}
""".format(parent=parent, column=column, roles = ', '.join(['%s']*len(roles)),
field=parent.lower(), condition="and `tabReport`.disabled=0" if parent == "Report" else ""),
roles, as_dict=True)
subq = (
frappe.qb.from_(customRole).select(customRole[parent.lower()])
.where(customRole[parent.lower()].isnotnull())
)
pages_with_standard_roles = (
frappe.qb.from_(hasRole).from_(parentTable)
.select(parentTable.name.as_("name"), parentTable.modified, *columns)
.where(
(hasRole.role.isin(roles))
& (hasRole.parent == parentTable.name)
& (parentTable.name.notin(subq))
).distinct()
)
if parent == "Report":
pages_with_standard_roles = pages_with_standard_roles.where(report.disabled == 0)
pages_with_standard_roles = pages_with_standard_roles.run(as_dict=True)
for p in pages_with_standard_roles:
for p in pages_with_standard_roles:
if p.name not in has_role:
if p.name not in has_role:
@@ -173,16 +187,16 @@ def get_user_pages_or_reports(parent, cache=False):
if parent == "Report":
if parent == "Report":
has_role[p.name].update({'ref_doctype': p.ref_doctype})
has_role[p.name].update({'ref_doctype': p.ref_doctype})
no_of_roles = (frappe.qb.from_(hasRole).select(Count("*"))
.where(hasRole.parent == parentTable.name)
)
# pages with no role are allowed
# pages with no role are allowed
if parent =="Page":
if parent =="Page":
pages_with_no_roles = frappe.db.sql("""
select
`tab{parent}`.name, `tab{parent}`.modified, {column}
from `tab{parent}`
where
(select count(*) from `tabHas Role`
where `tabHas Role`.parent=`tab{parent}`.`name`) = 0
""".format(parent=parent, column=column), as_dict=1)
pages_with_no_roles = (frappe.qb.from_(parentTable).select(parentTable.name, parentTable.modified, *columns)
.where(subqry(no_of_roles) == 0)
).run(as_dict=True)
for p in pages_with_no_roles:
for p in pages_with_no_roles:
if p.name not in has_role:
if p.name not in has_role:
@@ -201,13 +215,6 @@ def get_user_pages_or_reports(parent, cache=False):
_cache.set_value('has_role:' + parent, has_role, frappe.session.user, 21600)
_cache.set_value('has_role:' + parent, has_role, frappe.session.user, 21600)
return has_role
return has_role
def get_column(doctype):
column = "`tabPage`.title as title"
if doctype == "Report":
column = "`tabReport`.`name` as title, `tabReport`.ref_doctype, `tabReport`.report_type"
return column
def load_translations(bootinfo):
def load_translations(bootinfo):
messages = frappe.get_lang_dict("boot")
messages = frappe.get_lang_dict("boot")
@@ -271,10 +278,16 @@ def load_print_css(bootinfo, print_settings):
bootinfo.print_css = frappe.www.printview.get_print_style(print_settings.print_style or "Redesign", for_legacy=True)
bootinfo.print_css = frappe.www.printview.get_print_style(print_settings.print_style or "Redesign", for_legacy=True)
def get_unseen_notes():
def get_unseen_notes():
return frappe.db.sql('''select `name`, title, content, notify_on_every_login from `tabNote` where notify_on_login=1
and expire_notification_on > %s and %s not in
(select user from `tabNote Seen By` nsb
where nsb.parent=`tabNote`.name)''', (frappe.utils.now(), frappe.session.user), as_dict=True)
note = DocType("Note")
nsb = DocType("Note Seen By").as_("nsb")
return (
frappe.qb.from_(note).select(note.name, note.title, note.content, note.notify_on_every_login)
.where(
(note.notify_on_every_login == 1)
& (note.expire_notification_on > frappe.utils.now())
& (subqry(frappe.qb.from_(nsb).select(nsb.user).where(nsb.parent == note.name)).notin([frappe.session.user])))
).run(as_dict=1)
def get_success_action():
def get_success_action():
return frappe.get_all("Success Action", fields=["*"])
return frappe.get_all("Success Action", fields=["*"])