<s> from __future__ import unicode_literals <EOL> import frappe <EOL> from frappe import _ <EOL> import frappe . sessions <EOL> from frappe . utils import cstr <EOL> import mimetypes , json <EOL> from werkzeug . wrappers import Response <EOL> from werkzeug . routing import Map , Rule , NotFound <EOL> from frappe . website . context import get_context <EOL> from frappe . website . utils import get_home_page , can_cache , delete_page_cache <EOL> from frappe . website . router import clear_sitemap <EOL> from frappe . translate import guess_language <EOL> class PageNotFoundError ( Exception ) : pass <EOL> def render ( path , http_status_code = None ) : <EOL> """<STR_LIT>""" <EOL> path = resolve_path ( path . strip ( "<STR_LIT>" ) ) <EOL> try : <EOL> data = render_page_by_language ( path ) <EOL> except frappe . DoesNotExistError , e : <EOL> doctype , name = get_doctype_from_path ( path ) <EOL> if doctype and name : <EOL> path = "<STR_LIT>" <EOL> frappe . local . form_dict . doctype = doctype <EOL> frappe . local . form_dict . name = name <EOL> elif doctype : <EOL> path = "<STR_LIT:list>" <EOL> frappe . local . form_dict . doctype = doctype <EOL> else : <EOL> path = "<STR_LIT>" <EOL> http_status_code = e . http_status_code <EOL> try : <EOL> data = render_page ( path ) <EOL> except frappe . PermissionError , e : <EOL> data , http_status_code = render_403 ( e , path ) <EOL> except frappe . PermissionError , e : <EOL> data , http_status_code = render_403 ( e , path ) <EOL> except frappe . Redirect , e : <EOL> return build_response ( path , "<STR_LIT>" , <NUM_LIT> , { <EOL> "<STR_LIT>" : frappe . flags . redirect_location , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } ) <EOL> except Exception : <EOL> path = "<STR_LIT:error>" <EOL> data = render_page ( path ) <EOL> http_status_code = <NUM_LIT> <EOL> data = add_csrf_token ( data ) <EOL> return build_response ( path , data , http_status_code or <NUM_LIT:200> ) <EOL> def build_response ( path , data , http_status_code , headers = None ) : <EOL> response = Response ( ) <EOL> response . data = set_content_type ( response , data , path ) <EOL> response . status_code = http_status_code <EOL> response . headers [ b"<STR_LIT>" ] = path . encode ( "<STR_LIT:utf-8>" ) <EOL> response . headers [ b"<STR_LIT>" ] = frappe . local . response . from_cache or False <EOL> if headers : <EOL> for key , val in headers . iteritems ( ) : <EOL> response . headers [ bytes ( key ) ] = val . encode ( "<STR_LIT:utf-8>" ) <EOL> return response <EOL> def render_page_by_language ( path ) : <EOL> translated_languages = frappe . get_hooks ( "<STR_LIT>" ) <EOL> user_lang = guess_language ( translated_languages ) <EOL> if translated_languages and user_lang in translated_languages : <EOL> try : <EOL> if path and path != "<STR_LIT:index>" : <EOL> lang_path = '<STR_LIT>' . format ( user_lang , path ) <EOL> else : <EOL> lang_path = user_lang <EOL> return render_page ( lang_path ) <EOL> except frappe . DoesNotExistError : <EOL> return render_page ( path ) <EOL> else : <EOL> return render_page ( path ) <EOL> def render_page ( path ) : <EOL> """<STR_LIT>""" <EOL> out = None <EOL> if can_cache ( ) : <EOL> page_cache = frappe . cache ( ) . hget ( "<STR_LIT>" , path ) <EOL> if page_cache and frappe . local . lang in page_cache : <EOL> out = page_cache [ frappe . local . lang ] <EOL> if out : <EOL> frappe . local . response . from_cache = True <EOL> return out <EOL> return build ( path ) <EOL> def build ( path ) : <EOL> if not frappe . db : <EOL> frappe . connect ( ) <EOL> try : <EOL> return build_page ( path ) <EOL> except frappe . DoesNotExistError : <EOL> hooks = frappe . get_hooks ( ) <EOL> if hooks . website_catch_all : <EOL> path = hooks . website_catch_all [ <NUM_LIT:0> ] <EOL> return build_page ( path ) <EOL> else : <EOL> raise <EOL> def build_page ( path ) : <EOL> if not getattr ( frappe . local , "<STR_LIT:path>" , None ) : <EOL> frappe . local . path = path <EOL> context = get_context ( path ) <EOL> html = frappe . get_template ( context . template ) . render ( context ) <EOL> if can_cache ( context . no_cache ) : <EOL> page_cache = frappe . cache ( ) . hget ( "<STR_LIT>" , path ) or { } <EOL> page_cache [ frappe . local . lang ] = html <EOL> frappe . cache ( ) . hset ( "<STR_LIT>" , path , page_cache ) <EOL> return html <EOL> def resolve_path ( path ) : <EOL> if not path : <EOL> path = "<STR_LIT:index>" <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> path = path [ : - <NUM_LIT:5> ] <EOL> if path == "<STR_LIT:index>" : <EOL> path = get_home_page ( ) <EOL> frappe . local . path = path <EOL> if path != "<STR_LIT:index>" : <EOL> path = resolve_from_map ( path ) <EOL> return path <EOL> def resolve_from_map ( path ) : <EOL> m = Map ( [ Rule ( r [ "<STR_LIT>" ] , endpoint = r [ "<STR_LIT>" ] , defaults = r . get ( "<STR_LIT>" ) ) <EOL> for r in frappe . get_hooks ( "<STR_LIT>" ) ] ) <EOL> urls = m . bind_to_environ ( frappe . local . request . environ ) <EOL> try : <EOL> endpoint , args = urls . match ( "<STR_LIT:/>" + path ) <EOL> path = endpoint <EOL> if args : <EOL> frappe . local . no_cache = <NUM_LIT:1> <EOL> frappe . local . form_dict . update ( args ) <EOL> except NotFound : <EOL> pass <EOL> return path <EOL> def set_content_type ( response , data , path ) : <EOL> if isinstance ( data , dict ) : <EOL> response . mimetype = '<STR_LIT:application/json>' <EOL> response . charset = '<STR_LIT:utf-8>' <EOL> data = json . dumps ( data ) <EOL> return data <EOL> response . mimetype = '<STR_LIT>' <EOL> response . charset = '<STR_LIT:utf-8>' <EOL> if "<STR_LIT:.>" in path : <EOL> content_type , encoding = mimetypes . guess_type ( path ) <EOL> if content_type : <EOL> response . mimetype = content_type <EOL> if encoding : <EOL> response . charset = encoding <EOL> return data <EOL> def clear_cache ( path = None ) : <EOL> frappe . cache ( ) . delete_value ( "<STR_LIT>" ) <EOL> delete_page_cache ( path ) <EOL> if not path : <EOL> clear_sitemap ( ) <EOL> frappe . clear_cache ( "<STR_LIT>" ) <EOL> frappe . cache ( ) . delete_value ( "<STR_LIT>" ) <EOL> frappe . cache ( ) . delete_value ( "<STR_LIT>" ) <EOL> for method in frappe . get_hooks ( "<STR_LIT>" ) : <EOL> frappe . get_attr ( method ) ( path ) <EOL> def render_403 ( e , pathname ) : <EOL> path = "<STR_LIT:message>" <EOL> frappe . local . message = """<STR_LIT>""" . format ( error = cstr ( e . message ) , login = _ ( "<STR_LIT>" ) , pathname = frappe . local . path ) <EOL> frappe . local . message_title = _ ( "<STR_LIT>" ) <EOL> return render_page ( path ) , e . http_status_code <EOL> def get_doctype_from_path ( path ) : <EOL> doctypes = frappe . db . sql_list ( "<STR_LIT>" ) <EOL> parts = path . split ( "<STR_LIT:/>" ) <EOL> doctype = parts [ <NUM_LIT:0> ] <EOL> name = parts [ <NUM_LIT:1> ] if len ( parts ) > <NUM_LIT:1> else None <EOL> if doctype in doctypes : <EOL> return doctype , name <EOL> doctype = doctype . replace ( "<STR_LIT:_>" , "<STR_LIT:U+0020>" ) . title ( ) <EOL> if doctype in doctypes : <EOL> return doctype , name <EOL> return None , None <EOL> def add_csrf_token ( data ) : <EOL> return data . replace ( "<STR_LIT>" , '<STR_LIT>' . format ( <EOL> frappe . local . session . data . csrf_token ) ) </s>
