<s> import logging <EOL> import cgi <EOL> import sys <EOL> import traceback <EOL> import hashlib <EOL> import os <EOL> from functools import wraps <EOL> import webapp2 <EOL> from webapp2_extras import sessions , json , auth , i18n <EOL> from jinja2 . runtime import TemplateNotFound <EOL> import settings <EOL> import utils <EOL> from handlers import jinja_environment <EOL> def get_json_error ( code , key = None , message = None ) : <EOL> logging . debug ( json . encode ( get_error ( code , key = key , message = message ) ) ) <EOL> return json . encode ( get_error ( code , key = key , message = message ) ) <EOL> def get_error ( code , key = None , message = None ) : <EOL> if message : <EOL> text = message <EOL> elif key : <EOL> text = settings . API_CODES [ code ] [ key ] <EOL> if not message and not key : <EOL> text = settings . API_CODES [ code ] <EOL> try : <EOL> text = i18n . gettext ( text ) <EOL> except Exception : <EOL> pass <EOL> return { '<STR_LIT:code>' : code , '<STR_LIT>' : text } <EOL> def with_login ( func ) : <EOL> @ wraps ( func ) <EOL> def _with_login ( * args , ** kwargs ) : <EOL> self = args [ <NUM_LIT:0> ] <EOL> if not self . logged_in and issubclass ( args [ <NUM_LIT:0> ] . __class__ , BaseAPIHandler ) : <EOL> return args [ <NUM_LIT:0> ] . prep_json_response ( <NUM_LIT> ) <EOL> elif not args [ <NUM_LIT:0> ] . logged_in and issubclass ( args [ <NUM_LIT:0> ] . __class__ , BaseHandler ) : <EOL> return args [ <NUM_LIT:0> ] . prep_html_response ( "<STR_LIT>" , { '<STR_LIT:code>' : <NUM_LIT> } ) <EOL> func ( * args , ** kwargs ) <EOL> return _with_login <EOL> class BaseHandler ( webapp2 . RequestHandler ) : <EOL> def head ( self ) : <EOL> pass <EOL> def handle_exception ( self , exception , debug_mode ) : <EOL> logging . exception ( "<STR_LIT>" + exception ) <EOL> self . response . clear ( ) <EOL> if debug_mode : <EOL> lines = '<STR_LIT>' . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) <EOL> self . response . write ( '<STR_LIT>' % ( cgi . escape ( lines , quote = True ) ) ) <EOL> else : <EOL> if isinstance ( exception , webapp2 . HTTPException ) : <EOL> code = exception . code <EOL> else : <EOL> code = <NUM_LIT> <EOL> self . response . set_status ( code ) <EOL> self . response . out . write ( jinja_environment . get_template ( "<STR_LIT>" ) . render ( { '<STR_LIT:code>' : <NUM_LIT> } ) ) <EOL> def dispatch ( self ) : <EOL> self . session_store = sessions . get_store ( request = self . request ) <EOL> i18n . get_i18n ( ) . set_locale ( self . locale ) <EOL> try : <EOL> webapp2 . RequestHandler . dispatch ( self ) <EOL> finally : <EOL> self . session_store . save_sessions ( self . response ) <EOL> @ webapp2 . cached_property <EOL> def session ( self ) : <EOL> session = self . session_store . get_session ( ) <EOL> if len ( session ) == <NUM_LIT:0> : <EOL> for k , v in settings . COOKIE_TEMPLATE . iteritems ( ) : <EOL> session [ k ] = v <EOL> session [ '<STR_LIT:id>' ] = hashlib . md5 ( os . urandom ( <NUM_LIT:16> ) ) . hexdigest ( ) <EOL> return session <EOL> @ webapp2 . cached_property <EOL> def locale ( self ) : <EOL> if '<STR_LIT>' in self . session : <EOL> return self . session [ '<STR_LIT>' ] <EOL> else : <EOL> header = self . request . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> locales = [ locale . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] for locale in header . split ( '<STR_LIT:U+002C>' ) ] <EOL> available = settings . AVAILABLE_LOCALES <EOL> for locale in locales : <EOL> if locale in available : <EOL> self . session [ '<STR_LIT>' ] = locale <EOL> return locale <EOL> for locale in locales : <EOL> len1 = len ( locale ) <EOL> locales2 = [ l for l in available if l [ <NUM_LIT:0> : ( len1 - <NUM_LIT:1> ) ] == locale ] <EOL> if len ( locales2 ) > <NUM_LIT:0> : <EOL> self . session [ '<STR_LIT>' ] = locales2 [ <NUM_LIT:0> ] <EOL> return locales2 [ <NUM_LIT:0> ] <EOL> for l2 in [ locale [ <NUM_LIT:0> : <NUM_LIT:2> ] for locale in locales ] : <EOL> locales2 = [ l for l in available if l [ <NUM_LIT:0> : <NUM_LIT:2> ] == l2 ] <EOL> if len ( locales2 ) > <NUM_LIT:0> : <EOL> self . session [ '<STR_LIT>' ] = locales2 [ <NUM_LIT:0> ] <EOL> return locales2 [ <NUM_LIT:0> ] <EOL> self . session [ '<STR_LIT>' ] = available [ <NUM_LIT:0> ] <EOL> return available [ <NUM_LIT:0> ] <EOL> @ webapp2 . cached_property <EOL> def auth ( self ) : <EOL> return auth . get_auth ( ) <EOL> @ webapp2 . cached_property <EOL> def current_user ( self ) : <EOL> """<STR_LIT>""" <EOL> user_dict = self . auth . get_user_by_session ( ) <EOL> return self . auth . store . user_model . get_by_id ( user_dict [ '<STR_LIT>' ] ) <EOL> @ webapp2 . cached_property <EOL> def logged_in ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . auth . get_user_by_session ( ) is not None <EOL> def session_inc_pageviews ( self ) : <EOL> self . session [ '<STR_LIT>' ] = self . session [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> def prep_html_response ( self , template_name , template_vars = { } ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> values = { <EOL> '<STR_LIT>' : self . uri_for , <EOL> '<STR_LIT>' : self . logged_in , <EOL> '<STR_LIT>' : self . current_user if self . logged_in else None <EOL> } <EOL> values . update ( template_vars ) <EOL> logging . info ( "<STR_LIT>" % self . session ) <EOL> logging . info ( "<STR_LIT>" % values ) <EOL> try : <EOL> template = jinja_environment . get_template ( template_name ) <EOL> self . response . out . write ( template . render ( ** values ) ) <EOL> except TemplateNotFound : <EOL> self . abort ( <NUM_LIT> ) <EOL> class BaseAPIHandler ( BaseHandler ) : <EOL> def handle_exception ( self , exception , debug_mode ) : <EOL> logging . exception ( exception ) <EOL> if isinstance ( exception , webapp2 . HTTPException ) : <EOL> self . response . set_status ( exception . code ) <EOL> else : <EOL> self . response . set_status ( <NUM_LIT> ) <EOL> if debug_mode : <EOL> lines = '<STR_LIT>' . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) <EOL> result = get_error ( <NUM_LIT> , message = lines ) <EOL> else : <EOL> result = get_error ( <NUM_LIT> , key = '<STR_LIT>' ) <EOL> self . response . clear ( ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> self . response . write ( json . encode ( result ) ) <EOL> def put ( self ) : <EOL> pass <EOL> def post ( self ) : <EOL> pass <EOL> def delete ( self ) : <EOL> pass <EOL> def prep_json_response ( self , code , key = None , message = None ) : <EOL> self . response . set_status ( code ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> self . response . write ( get_json_error ( code , key = key , message = message ) ) <EOL> class BaseRESTHandler ( BaseAPIHandler ) : <EOL> @ with_login <EOL> def put ( self , obj_t , * args ) : <EOL> kvs = json . decode ( self . request . body ) <EOL> cls = getattr ( sys . modules [ '<STR_LIT>' ] , obj_t ) <EOL> obj = utils . to_dict ( cls . put ( kvs ) ) <EOL> return self . prep_json_response ( <NUM_LIT:200> , message = obj ) <EOL> def get ( self , obj_t , identifier , * args ) : <EOL> cls = getattr ( sys . modules [ '<STR_LIT>' ] , obj_t ) <EOL> obj = utils . to_dict ( cls . get ( int ( identifier ) ) ) <EOL> return self . prep_json_response ( <NUM_LIT:200> , message = obj ) <EOL> def post ( self , obj_t , identifier , * args ) : <EOL> kvs = json . decode ( self . request . body ) <EOL> cls = getattr ( sys . modules [ '<STR_LIT>' ] , obj_t ) <EOL> obj = utils . to_dict ( cls . post ( int ( identifier ) , kvs ) ) <EOL> return self . prep_json_response ( <NUM_LIT:200> , message = obj ) <EOL> @ with_login <EOL> def delete ( self , obj_t , identifier , * args ) : <EOL> cls = getattr ( sys . modules [ '<STR_LIT>' ] , obj_t ) <EOL> obj = cls . delete1 ( int ( identifier ) ) <EOL> return self . prep_json_response ( <NUM_LIT:200> , message = json . encode ( obj ) ) <EOL> def handle_404 ( request , response , exception ) : <EOL> response . set_status ( <NUM_LIT> ) <EOL> response . out . write ( jinja_environment . get_template ( "<STR_LIT>" ) . render ( { '<STR_LIT:code>' : <NUM_LIT> } ) ) <EOL> def handle_500 ( request , response , exception ) : <EOL> response . set_status ( <NUM_LIT> ) <EOL> response . out . write ( jinja_environment . get_template ( "<STR_LIT>" ) . render ( { '<STR_LIT:code>' : <NUM_LIT> } ) ) </s>
