seitime-frappe/frappe/www/app.py
Corentin Flr 41d30e7213
feat: Add custom icons with the app_include_icons hook (#22254)
* feat: Add custom icons with the `app_include_icons` hook

These custom icons are available on the desk only. They can be picked in the `ControlIcon` picker.

Co-authored-by: Abraham Kalungi <85731451+kalungia@users.noreply.github.com>

* fix: skip conf for including icons

* test: Fix test_include_icons

---------

Co-authored-by: Abraham Kalungi <85731451+kalungia@users.noreply.github.com>
Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
2023-09-20 20:23:17 +05:30

70 lines
2.1 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
no_cache = 1
import json
import re
import frappe
import frappe.sessions
from frappe import _
from frappe.utils.jinja_globals import is_rtl
SCRIPT_TAG_PATTERN = re.compile(r"\<script[^<]*\</script\>")
CLOSING_SCRIPT_TAG_PATTERN = re.compile(r"</script\>")
def get_context(context):
if frappe.session.user == "Guest":
frappe.throw(_("Log in to access this page."), frappe.PermissionError)
elif (
frappe.db.get_value("User", frappe.session.user, "user_type", order_by=None) == "Website User"
):
frappe.throw(_("You are not permitted to access this page."), frappe.PermissionError)
hooks = frappe.get_hooks()
try:
boot = frappe.sessions.get()
except Exception as e:
raise frappe.SessionBootFailed from e
# this needs commit
csrf_token = frappe.sessions.get_csrf_token()
frappe.db.commit()
boot_json = frappe.as_json(boot, indent=None, separators=(",", ":"))
# remove script tags from boot
boot_json = SCRIPT_TAG_PATTERN.sub("", boot_json)
# TODO: Find better fix
boot_json = CLOSING_SCRIPT_TAG_PATTERN.sub("", boot_json)
boot_json = json.dumps(boot_json)
include_js = hooks.get("app_include_js", []) + frappe.conf.get("app_include_js", [])
include_css = hooks.get("app_include_css", []) + frappe.conf.get("app_include_css", [])
include_icons = hooks.get("app_include_icons", [])
context.update(
{
"no_cache": 1,
"build_version": frappe.utils.get_build_version(),
"include_js": include_js,
"include_css": include_css,
"include_icons": include_icons,
"layout_direction": "rtl" if is_rtl() else "ltr",
"lang": frappe.local.lang,
"sounds": hooks["sounds"],
"boot": boot if context.get("for_mobile") else boot_json,
"desk_theme": boot.get("desk_theme") or "Light",
"csrf_token": csrf_token,
"google_analytics_id": frappe.conf.get("google_analytics_id"),
"google_analytics_anonymize_ip": frappe.conf.get("google_analytics_anonymize_ip"),
"app_name": (
frappe.get_website_settings("app_name") or frappe.get_system_settings("app_name") or "Frappe"
),
}
)
return context