|
|
@@ -13,16 +13,16 @@ from werkzeug.routing import Map, Rule, NotFound |
|
|
|
from frappe.website.context import get_context |
|
|
|
from frappe.website.utils import get_home_page, can_cache, delete_page_cache |
|
|
|
from frappe.website.router import clear_sitemap |
|
|
|
from frappe.translate import guess_language |
|
|
|
|
|
|
|
class PageNotFoundError(Exception): pass |
|
|
|
|
|
|
|
def render(path, http_status_code=None): |
|
|
|
"""render html page""" |
|
|
|
path = resolve_path(path.strip("/ ")) |
|
|
|
set_lang() |
|
|
|
|
|
|
|
try: |
|
|
|
data = render_page(path) |
|
|
|
data = render_page_by_language(path) |
|
|
|
except frappe.DoesNotExistError, e: |
|
|
|
doctype, name = get_doctype_from_path(path) |
|
|
|
if doctype and name: |
|
|
@@ -59,47 +59,6 @@ def render(path, http_status_code=None): |
|
|
|
|
|
|
|
return build_response(path, data, http_status_code or 200) |
|
|
|
|
|
|
|
def set_lang(): |
|
|
|
"""Set user's lang if not Guest or use default lang""" |
|
|
|
frappe.local.lang = getattr(frappe.local, "user_lang", None) or frappe.db.get_default("lang") |
|
|
|
|
|
|
|
def render_403(e, pathname): |
|
|
|
path = "message" |
|
|
|
frappe.local.message = """<p><strong>{error}</strong></p> |
|
|
|
<p> |
|
|
|
<a href="/login?redirect-to=/{pathname}" class="btn btn-primary">{login}</a> |
|
|
|
</p>""".format(error=cstr(e.message), login=_("Login"), pathname=frappe.local.path) |
|
|
|
frappe.local.message_title = _("Not Permitted") |
|
|
|
return render_page(path), e.http_status_code |
|
|
|
|
|
|
|
def get_doctype_from_path(path): |
|
|
|
doctypes = frappe.db.sql_list("select name from tabDocType") |
|
|
|
|
|
|
|
parts = path.split("/") |
|
|
|
|
|
|
|
doctype = parts[0] |
|
|
|
name = parts[1] if len(parts) > 1 else None |
|
|
|
|
|
|
|
if doctype in doctypes: |
|
|
|
return doctype, name |
|
|
|
|
|
|
|
# try scrubbed |
|
|
|
doctype = doctype.replace("_", " ").title() |
|
|
|
if doctype in doctypes: |
|
|
|
return doctype, name |
|
|
|
|
|
|
|
return None, None |
|
|
|
|
|
|
|
def add_csrf_token(data): |
|
|
|
if is_ajax() or frappe.session.user == "Guest" or not frappe.local.session.data.csrf_token: |
|
|
|
pass |
|
|
|
|
|
|
|
else: |
|
|
|
data = data.replace("<!-- csrf_token -->", '<script>frappe.csrf_token = "{0}";</script>'.format( |
|
|
|
frappe.local.session.data.csrf_token)) |
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
def build_response(path, data, http_status_code, headers=None): |
|
|
|
# build response |
|
|
|
response = Response() |
|
|
@@ -114,6 +73,23 @@ def build_response(path, data, http_status_code, headers=None): |
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
def render_page_by_language(path): |
|
|
|
translated_languages = frappe.get_hooks("translated_languages_for_website") |
|
|
|
user_lang = guess_language(translated_languages) |
|
|
|
if translated_languages and user_lang in translated_languages: |
|
|
|
try: |
|
|
|
if path and path != "index": |
|
|
|
lang_path = '{0}/{1}'.format(user_lang, path) |
|
|
|
else: |
|
|
|
lang_path = user_lang # index |
|
|
|
|
|
|
|
return render_page(lang_path) |
|
|
|
except frappe.DoesNotExistError: |
|
|
|
return render_page(path) |
|
|
|
|
|
|
|
else: |
|
|
|
return render_page(path) |
|
|
|
|
|
|
|
def render_page(path): |
|
|
|
"""get page html""" |
|
|
|
out = None |
|
|
@@ -234,3 +210,40 @@ def clear_cache(path=None): |
|
|
|
|
|
|
|
for method in frappe.get_hooks("website_clear_cache"): |
|
|
|
frappe.get_attr(method)(path) |
|
|
|
|
|
|
|
def render_403(e, pathname): |
|
|
|
path = "message" |
|
|
|
frappe.local.message = """<p><strong>{error}</strong></p> |
|
|
|
<p> |
|
|
|
<a href="/login?redirect-to=/{pathname}" class="btn btn-primary">{login}</a> |
|
|
|
</p>""".format(error=cstr(e.message), login=_("Login"), pathname=frappe.local.path) |
|
|
|
frappe.local.message_title = _("Not Permitted") |
|
|
|
return render_page(path), e.http_status_code |
|
|
|
|
|
|
|
def get_doctype_from_path(path): |
|
|
|
doctypes = frappe.db.sql_list("select name from tabDocType") |
|
|
|
|
|
|
|
parts = path.split("/") |
|
|
|
|
|
|
|
doctype = parts[0] |
|
|
|
name = parts[1] if len(parts) > 1 else None |
|
|
|
|
|
|
|
if doctype in doctypes: |
|
|
|
return doctype, name |
|
|
|
|
|
|
|
# try scrubbed |
|
|
|
doctype = doctype.replace("_", " ").title() |
|
|
|
if doctype in doctypes: |
|
|
|
return doctype, name |
|
|
|
|
|
|
|
return None, None |
|
|
|
|
|
|
|
def add_csrf_token(data): |
|
|
|
if is_ajax() or frappe.session.user == "Guest" or not frappe.local.session.data.csrf_token: |
|
|
|
pass |
|
|
|
|
|
|
|
else: |
|
|
|
data = data.replace("<!-- csrf_token -->", '<script>frappe.csrf_token = "{0}";</script>'.format( |
|
|
|
frappe.local.session.data.csrf_token)) |
|
|
|
|
|
|
|
return data |