def get_context(path, args=None): context = None context_cache = {} def add_data_path(context): if not context.data: context.data = {} context.data["path"] = path # try from cache if can_cache(): context_cache = frappe.cache().hget("page_context", path) or {} context = context_cache.get(frappe.local.lang, None) if not context: context = get_route_info(path) if args: context.update(args) context = build_context(context) add_data_path(context) if can_cache(context.no_cache): context_cache[frappe.local.lang] = context frappe.cache().hset("page_context", path, context_cache) else: add_data_path(context) context.update(context.data or {}) return context
def get_context(path): context = None cache_key = "page_context:{}".format(path) def add_data_path(context): if not context.data: context.data = {} context.data["path"] = path # try from memcache if can_cache(): context = frappe.cache().get_value(cache_key) if not context: context = get_route_info(path) # permission may be required for rendering context["access"] = frappe._dict({"public_read": 1, "public_write": 1}) context = build_context(context) add_data_path(context) if can_cache(context.no_cache): frappe.cache().set_value(cache_key, context) else: context["access"] = frappe._dict({"public_read": 1, "public_write": 1}) add_data_path(context) context.update(context.data or {}) return context
def get_context(path): context = None cache_key = "page_context:{0}:{1}".format(path, frappe.local.lang) def add_data_path(context): if not context.data: context.data = {} context.data["path"] = path # try from memcache if can_cache(): context = frappe.cache().get_value(cache_key) if not context: context = get_route_info(path) context = build_context(context) add_data_path(context) if can_cache(context.no_cache): frappe.cache().set_value(cache_key, context) else: add_data_path(context) context.update(context.data or {}) return context
def get_context(path): context = None cache_key = "page_context:{}".format(path) # try from memcache if can_cache(): context = frappe.cache().get_value(cache_key) if not context: context = get_sitemap_options(path) # permission may be required for rendering context["access"] = get_access(context.pathname) context = build_context(context) if can_cache(context.no_cache): frappe.cache().set_value(cache_key, context) else: context["access"] = get_access(context.pathname) if not context.data: context.data = {} context.data["path"] = path context.update(context.data or {}) return context
def get_context(path): context = None context_cache = {} def add_data_path(context): if not context.data: context.data = {} context.data["path"] = path # try from cache if can_cache(): context_cache = frappe.cache().hget("page_context", path) or {} context = context_cache.get(frappe.local.lang, None) if not context: context = get_route_info(path) context = build_context(context) add_data_path(context) if can_cache(context.no_cache): context_cache[frappe.local.lang] = context frappe.cache().hset("page_context", path, context_cache) else: add_data_path(context) context.update(context.data or {}) return context
def get_context(path): context = None cache_key = "page_context:{}".format(path) # try from memcache if can_cache(): context = frappe.cache().get_value(cache_key) if not context: context = get_sitemap_options(path) # permission may be required for rendering context["access"] = get_access(context.pathname) context = build_context(context) if can_cache(context.no_cache): frappe.cache().set_value(cache_key, context) else: context["access"] = get_access(context.pathname) if not context.data: context.data = {} context.data["path"] = path context.update(context.data or {}) # TODO private pages return context
def get_route_info(path): sitemap_options = None cache_key = "sitemap_options:{0}:{1}".format(path, frappe.local.lang) if can_cache(): sitemap_options = frappe.cache().get_value(cache_key) if not sitemap_options: sitemap_options = build_route(path) if can_cache(sitemap_options.no_cache): frappe.cache().set_value(cache_key, sitemap_options) return sitemap_options
def get_route_info(path): sitemap_options = None cache_key = "sitemap_options:{}".format(path) if can_cache(): sitemap_options = frappe.cache().get_value(cache_key) if not sitemap_options: sitemap_options = build_route(path) if can_cache(sitemap_options.no_cache): frappe.cache().set_value(cache_key, sitemap_options) return sitemap_options
def get_page_context(path): page_context = None if can_cache(): page_context_cache = frappe.cache().hget("page_context", path) or {} page_context = page_context_cache.get(frappe.local.lang, None) if not page_context: page_context = make_page_context(path) if can_cache(page_context.no_cache): page_context_cache[frappe.local.lang] = page_context frappe.cache().hset("page_context", path, page_context_cache) return page_context
def get_sitemap_options(path): sitemap_options = None cache_key = "sitemap_options:{}".format(path) if can_cache(): sitemap_options = frappe.cache().get_value(cache_key) if not sitemap_options: sitemap_options = build_sitemap_options(path) if can_cache(sitemap_options.no_cache): frappe.cache().set_value(cache_key, sitemap_options) return frappe._dict(sitemap_options)
def get_page_context(path): page_context = None if can_cache(): page_context_cache = frappe.cache().hget("page_context", path) or {} page_context = page_context_cache.get(frappe.local.lang, None) if not page_context: page_context = make_page_context(path) if can_cache(page_context.no_cache): page_context_cache[frappe.local.lang] = page_context frappe.cache().hset("page_context", path, page_context_cache) return page_context
def get_route_info(path): sitemap_options = None cache_key = "sitemap_options:{0}:{1}".format(path, frappe.local.lang) if can_cache(): sitemap_options_cache = frappe.cache().hget("sitemap_options", path) or {} sitemap_options = sitemap_options_cache.get(frappe.local.lang, None) if not sitemap_options: sitemap_options = build_route(path) if can_cache(sitemap_options.no_cache): sitemap_options_cache[frappe.local.lang] = sitemap_options frappe.cache().hset("sitemap_options", path, sitemap_options_cache) return sitemap_options
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) if context.source: html = frappe.render_template(context.source, context) elif context.template: html = frappe.get_template(context.template).render(context) if '{index}' in html: html = html.replace('{index}', get_toc(context.route)) if '{next}' in html: html = html.replace('{next}', get_next_link(context.route)) # html = frappe.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): page_cache = frappe.cache().hget("website_page", path) or {} page_cache[frappe.local.lang] = html frappe.cache().hset("website_page", path, page_cache) return html
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) if context.source: html = frappe.render_template(context.source, context) elif context.template: html = frappe.get_template(context.template).render(context) if '{index}' in html: html = html.replace('{index}', get_toc(context.route)) if '{next}' in html: html = html.replace('{next}', get_next_link(context.route)) # html = frappe.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): page_cache = frappe.cache().hget("website_page", path) or {} page_cache[frappe.local.lang] = html frappe.cache().hset("website_page", path, page_cache) return html
def resolve(self): '''Returns endpoint and a renderer instance that can render the endpoint''' request = frappe._dict() if hasattr(frappe.local, 'request'): request = frappe.local.request or request # check if the request url is in 404 list if request.url and can_cache() and frappe.cache().hget( 'website_404', request.url): return self.path, NotFoundPage(self.path) try: resolve_redirect(self.path, request.query_string) except frappe.Redirect: return frappe.flags.redirect_location, RedirectPage(self.path) endpoint = resolve_path(self.path) custom_renderers = self.get_custom_page_renderers() renderers = custom_renderers + [ StaticPage, WebFormPage, DocumentPage, TemplatePage, ListPage, PrintPage, NotFoundPage ] for renderer in renderers: renderer_instance = renderer(endpoint, 200) if renderer_instance.can_render(): return endpoint, renderer_instance return endpoint, NotFoundPage(endpoint)
def get_route_info(path): sitemap_options = None cache_key = "sitemap_options:{0}:{1}".format(path, frappe.local.lang) if can_cache(): sitemap_options_cache = frappe.cache().hget("sitemap_options", path) or {} sitemap_options = sitemap_options_cache.get(frappe.local.lang, None) if not sitemap_options: sitemap_options = build_route(path) if can_cache(sitemap_options.no_cache): sitemap_options_cache[frappe.local.lang] = sitemap_options frappe.cache().hset("sitemap_options", path, sitemap_options_cache) return sitemap_options
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) if context.source: html = frappe.render_template(context.source, context) elif context.template: if path.endswith("min.js"): html = frappe.get_jloader().get_source(frappe.get_jenv(), context.template)[0] else: html = frappe.get_template(context.template).render(context) if "{index}" in html: html = html.replace("{index}", get_toc(context.route)) if "{next}" in html: html = html.replace("{next}", get_next_link(context.route)) # html = frappe.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): page_cache = frappe.cache().hget("website_page", path) or {} page_cache[frappe.local.lang] = html frappe.cache().hset("website_page", path, page_cache) return html
def render(path=None, http_status_code=None): """render html page""" path = resolve_path(path or frappe.local.request.path.strip('/ ')) data = None # if in list of already known 404s, send it if can_cache() and frappe.cache().hget('website_404', frappe.request.url): data = render_page('404') http_status_code = 404 else: try: data = render_page_by_language(path) except frappe.DoesNotExistError, e: doctype, name = get_doctype_from_path(path) if doctype and name: path = "print" frappe.local.form_dict.doctype = doctype frappe.local.form_dict.name = name elif doctype: path = "list" frappe.local.form_dict.doctype = doctype else: # 404s are expensive, cache them! frappe.cache().hset('website_404', frappe.request.url, True) data = render_page('404') http_status_code = 404 if not data: try: data = render_page(path) except frappe.PermissionError, e: data, http_status_code = render_403(e, path)
def render(path=None, http_status_code=None): """render html page""" path = resolve_path(path or frappe.local.request.path.strip('/ ')) data = None # if in list of already known 404s, send it if can_cache() and frappe.cache().hget('website_404', frappe.request.url): data = render_page('404') http_status_code = 404 else: try: data = render_page_by_language(path) except frappe.DoesNotExistError, e: doctype, name = get_doctype_from_path(path) if doctype and name: path = "print" frappe.local.form_dict.doctype = doctype frappe.local.form_dict.name = name elif doctype: path = "list" frappe.local.form_dict.doctype = doctype else: # 404s are expensive, cache them! frappe.cache().hset('website_404', frappe.request.url, True) data = render_page('404') http_status_code = 404 if not data: try: data = render_page(path) except frappe.PermissionError, e: data, http_status_code = render_403(e, path)
def render(path=None, http_status_code=None): """render html page""" if not path: path = frappe.local.request.path path = resolve_path(path.strip('/ ')) data = None # if in list of already known 404s, send it if can_cache() and frappe.cache().hget('website_404', frappe.request.url): data = render_page('404') http_status_code = 404 elif is_static_file(path): return get_static_file_reponse() else: try: data = render_page_by_language(path) except frappe.DoesNotExistError as e: doctype, name = get_doctype_from_path(path) if doctype and name: path = "printview" frappe.local.form_dict.doctype = doctype frappe.local.form_dict.name = name elif doctype: path = "list" frappe.local.form_dict.doctype = doctype else: # 404s are expensive, cache them! frappe.cache().hset('website_404', frappe.request.url, True) data = render_page('404') http_status_code = 404 if not data: try: data = render_page(path) except frappe.PermissionError as e: data, http_status_code = render_403(e, path) except frappe.PermissionError as e: data, http_status_code = render_403(e, path) except frappe.Redirect as e: return build_response( path, "", 301, { "Location": frappe.flags.redirect_location or (frappe.local.response or {}).get('location'), "Cache-Control": "no-store, no-cache, must-revalidate" }) except Exception: path = "error" data = render_page(path) http_status_code = 500 data = add_csrf_token(data) return build_response(path, data, http_status_code or 200)
def build_page(path): context = get_context(path) html = frappe.get_template(context.base_template_path).render(context) html = scrub_relative_urls(html) if can_cache(context.no_cache): frappe.cache().set_value("page:" + path, html) return html
def build_page(path): context = get_context(path) html = frappe.get_template(context.base_template_path).render(context) html = scrub_relative_urls(html) if can_cache(context.no_cache): frappe.cache().set_value("page:" + path, html) return html
def render(path=None, http_status_code=None): """render html page""" if not path: path = frappe.local.request.path path = resolve_path(path.strip('/ ')) data = None # if in list of already known 404s, send it if can_cache() and frappe.cache().hget('website_404', frappe.request.url): data = render_page('404') http_status_code = 404 elif is_static_file(path): return get_static_file_response() else: try: data = render_page_by_language(path) except frappe.DoesNotExistError as e: doctype, name = get_doctype_from_path(path) if doctype and name: path = "printview" frappe.local.form_dict.doctype = doctype frappe.local.form_dict.name = name elif doctype: path = "list" frappe.local.form_dict.doctype = doctype else: # 404s are expensive, cache them! frappe.cache().hset('website_404', frappe.request.url, True) data = render_page('404') http_status_code = 404 if not data: try: data = render_page(path) except frappe.PermissionError as e: data, http_status_code = render_403(e, path) except frappe.PermissionError as e: data, http_status_code = render_403(e, path) except frappe.Redirect as e: return build_response(path, "", 301, { "Location": frappe.flags.redirect_location or (frappe.local.response or {}).get('location'), "Cache-Control": "no-store, no-cache, must-revalidate" }) except Exception: path = "error" data = render_page(path) http_status_code = 500 data = add_csrf_token(data) return build_response(path, data, http_status_code or 200)
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) html = frappe.get_template(context.base_template_path).render(context) html = scrub_relative_urls(html) if can_cache(context.no_cache): frappe.cache().set_value("page:{0}:{1}".format(path, frappe.local.lang), html) return html
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) html = frappe.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): page_cache = frappe.cache().hget("website_page", path) or {} page_cache[frappe.local.lang] = html frappe.cache().hset("website_page", path, page_cache) return html
def build_page(path): if not getattr(frappe.local, "path", None): frappe.local.path = path context = get_context(path) html = frappe.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): page_cache = frappe.cache().hget("website_page", path) or {} page_cache[frappe.local.lang] = html frappe.cache().hset("website_page", path, page_cache) return html
def get_context(path): context = None cache_key = "page_context:{}".format(path) def add_data_path(context): if not context.data: context.data = {} context.data["path"] = path # try from memcache if can_cache(): context = frappe.cache().get_value(cache_key) if not context: context = get_route_info(path) # permission may be required for rendering if context.doc and context.doc.doctype == "Website Group": context["access"] = get_access(context.doc, context.pathname) else: context["access"] = frappe._dict({ "public_read": 1, "public_write": 1 }) context = build_context(context) add_data_path(context) if can_cache(context.no_cache): frappe.cache().set_value(cache_key, context) else: context["access"] = frappe._dict({"public_read": 1, "public_write": 1}) add_data_path(context) context.update(context.data or {}) return context
def render_page(path): """get page html""" out = None if can_cache(): # return rendered page page_cache = frappe.cache().hget("website_page", path) if page_cache and frappe.local.lang in page_cache: out = page_cache[frappe.local.lang] if out: frappe.local.response.from_cache = True return out return build(path)
def render_page(path): """get page html""" out = None if can_cache(): # return rendered page page_cache = frappe.cache().hget("website_page", path) if page_cache and frappe.local.lang in page_cache: out = page_cache[frappe.local.lang] if out: frappe.local.response.from_cache = True return out return build(path)
def render_page(path): """get page html""" cache_key = ("page_context:{}" if is_ajax() else "page:{}").format(path) out = None # try memcache if can_cache(): out = frappe.cache().get_value(cache_key) if out and is_ajax(): out = out.get("data") if out: frappe.local.response.from_cache = True return out return build(path)
def render_page(path): """get page html""" cache_key = ("page_context:{}" if is_ajax() else "page:{}").format(path) out = None # try memcache if can_cache(): out = frappe.cache().get_value(cache_key) if out and is_ajax(): out = out.get("data") if out: frappe.local.response.from_cache = True return out return build(path)
def render_page(path): """get page html""" cache_key = ("page_context:{}" if is_ajax() else "page:{}").format(path) out = None # try memcache if can_cache(): out = frappe.cache().get_value(cache_key) if out and is_ajax(): out = out.get("data") if out: if hasattr(frappe, "_response"): frappe._response.headers[b"X-From-Cache"] = True return out return build(path)
def render_page(path): """get page html""" out = None if can_cache(): if is_ajax(): # ajax, send context context_cache = frappe.cache().hget("page_context", path) if context_cache and frappe.local.lang in context_cache: out = context_cache[frappe.local.lang].get("data") else: # return rendered page page_cache = frappe.cache().hget("website_page", path) if page_cache and frappe.local.lang in page_cache: out = page_cache[frappe.local.lang] if out: frappe.local.response.from_cache = True return out return build(path)
def render_page(path): """get page html""" out = None if can_cache(): if is_ajax(): # ajax, send context context_cache = frappe.cache().hget("page_context", path) if context_cache and frappe.local.lang in context_cache: out = context_cache[frappe.local.lang].get("data") else: # return rendered page page_cache = frappe.cache().hget("website_page", path) if page_cache and frappe.local.lang in page_cache: out = page_cache[frappe.local.lang] if out: frappe.local.response.from_cache = True return out return build(path)
def can_cache_404(self): # do not cache 404 for custom homepages return can_cache() and self.request_url and not self.is_custom_home_page()