<s> """<STR_LIT>""" <EOL> import collections <EOL> import datetime <EOL> import json <EOL> import webapp2 <EOL> from google . appengine . api import users <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext import ndb <EOL> from lib . crud import crud_model <EOL> JSON_PREFIX = "<STR_LIT>" <EOL> def JsonPrinter ( json_dict ) : <EOL> if ( isinstance ( json_dict , datetime . datetime ) or <EOL> isinstance ( json_dict , datetime . date ) ) : <EOL> return json_dict . isoformat ( ) + '<STR_LIT>' <EOL> def IsAdmin ( unused_action , unused_user , unused_entity ) : <EOL> """<STR_LIT>""" <EOL> return users . is_current_user_admin ( ) <EOL> def IsOwner ( unused_action , user , entity ) : <EOL> """<STR_LIT>""" <EOL> return hasattr ( entity , '<STR_LIT>' ) and entity . owner == user . user_id ( ) <EOL> def SingletonFactory ( default ) : <EOL> def Singleton ( query , unused_request ) : <EOL> for x in query : <EOL> return [ x ] <EOL> return [ default ] <EOL> return Singleton <EOL> def GetCrudHandler ( model , <EOL> authorized_function = None , <EOL> search_function = None , <EOL> create_function = None ) : <EOL> """<STR_LIT>""" <EOL> the_model = model <EOL> the_authorized_function = authorized_function <EOL> the_search_function = search_function <EOL> the_create_function = create_function <EOL> class SpecificHandler ( CrudHandler ) : <EOL> model = staticmethod ( the_model ) <EOL> authorized_function = staticmethod ( the_authorized_function ) <EOL> search_function = staticmethod ( the_search_function ) <EOL> create_function = staticmethod ( the_create_function ) <EOL> return SpecificHandler <EOL> def _HasProperty ( model , prop ) : <EOL> if hasattr ( model , '<STR_LIT>' ) : <EOL> return prop in model . properties ( ) <EOL> else : <EOL> return prop in model . _properties <EOL> class CrudHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> model = None <EOL> authorized_function = None <EOL> search_function = None <EOL> create_function = None <EOL> def IsAuthorizedFunction ( self , f , action , user , entity ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> f = f . get ( action , f . get ( crud_model . Actions . ANY ) ) <EOL> if f : <EOL> return self . IsAuthorizedFunction ( f , action , user , entity ) <EOL> else : <EOL> return False <EOL> elif isinstance ( f , collections . Iterable ) : <EOL> functions = f <EOL> for f in functions : <EOL> if not f ( action , user , entity ) : <EOL> return False <EOL> return True <EOL> else : <EOL> return f ( action , user , entity ) <EOL> def IsAuthorized ( self , action , user , entity ) : <EOL> """<STR_LIT>""" <EOL> if self . authorized_function : <EOL> return self . IsAuthorizedFunction ( self . authorized_function , <EOL> action , user , entity ) <EOL> if hasattr ( entity , '<STR_LIT>' ) : <EOL> return entity . IsAuthorized ( action , user ) <EOL> return True <EOL> def _GetJsonDict ( self , entity , * args , ** kwargs ) : <EOL> if hasattr ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return entity . AsJsonDict ( * args , ** kwargs ) <EOL> else : <EOL> return crud_model . JsonDictFromEntity ( entity , * args , ** kwargs ) <EOL> def _CreateNewEntity ( self , * args , ** kwargs ) : <EOL> if self . create_function : <EOL> return self . create_function ( * args , ** kwargs ) <EOL> else : <EOL> return self . model ( * args , ** kwargs ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> key_id = self . request . get ( '<STR_LIT:id>' ) <EOL> parent_id = self . request . get ( '<STR_LIT>' ) <EOL> if parent_id and hasattr ( self . model , '<STR_LIT>' ) : <EOL> if parent_id == crud_model . NEW_ENTITY_ID : <EOL> return <EOL> if issubclass ( self . model , ndb . Model ) : <EOL> parent_key = ndb . Key ( self . model . parent_model_name , long ( parent_id ) ) <EOL> else : <EOL> parent_key = db . Key . from_path ( self . model . parent_model_name , <EOL> long ( parent_id ) ) <EOL> else : <EOL> parent_key = None <EOL> if not key_id : <EOL> if parent_key : <EOL> if hasattr ( self . model , '<STR_LIT>' ) : <EOL> all_models = self . model . query ( ) . ancestor ( parent_key ) <EOL> else : <EOL> all_models = self . model . query ( ancestor = parent_key ) <EOL> else : <EOL> all_models = self . model . query ( ) <EOL> if _HasProperty ( self . model , '<STR_LIT>' ) : <EOL> all_models = all_models . filter ( '<STR_LIT>' , False ) <EOL> if self . search_function : <EOL> all_models = self . search_function ( all_models , self . request ) <EOL> if hasattr ( self . model , '<STR_LIT>' ) : <EOL> all_models = self . model . Search ( all_models , self . request ) <EOL> if isinstance ( all_models , db . Model ) or isinstance ( all_models , ndb . Model ) : <EOL> if not self . IsAuthorized ( crud_model . Actions . QUERY , user , all_models ) : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> ans = self . _GetJsonDict ( all_models ) <EOL> else : <EOL> all_models = [ x for x in all_models <EOL> if self . IsAuthorized ( crud_model . Actions . QUERY , user , x ) ] <EOL> ans = [ self . _GetJsonDict ( ex ) for ex in all_models ] <EOL> else : <EOL> if key_id == crud_model . NEW_ENTITY_ID : <EOL> ex = self . _CreateNewEntity ( parent = parent_key ) <EOL> if not self . IsAuthorized ( crud_model . Actions . CREATE , user , ex ) : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> else : <EOL> ex = self . model . get_by_id ( long ( key_id ) , parent = parent_key ) <EOL> if not self . IsAuthorized ( crud_model . Actions . READ , user , ex ) : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> ans = self . _GetJsonDict ( ex ) <EOL> if key_id == crud_model . NEW_ENTITY_ID : <EOL> ans [ '<STR_LIT:id>' ] = crud_model . NEW_ENTITY_ID <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> self . response . out . write ( JSON_PREFIX + json . dumps ( ans , default = JsonPrinter ) ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> key_id = self . request . get ( '<STR_LIT:id>' ) <EOL> parent_id = self . request . get ( '<STR_LIT>' ) <EOL> if parent_id and hasattr ( self . model , '<STR_LIT>' ) : <EOL> if parent_id == crud_model . NEW_ENTITY_ID : <EOL> return <EOL> if issubclass ( self . model , ndb . Model ) : <EOL> parent_key = ndb . Key ( self . model . parent_model_name , long ( parent_id ) ) <EOL> else : <EOL> parent_key = db . Key . from_path ( self . model . parent_model_name , <EOL> long ( parent_id ) ) <EOL> else : <EOL> parent_key = None <EOL> if key_id == crud_model . NEW_ENTITY_ID : <EOL> ex = self . _CreateNewEntity ( parent = parent_key ) <EOL> else : <EOL> ex = self . model . get_by_id ( long ( key_id ) , parent = parent_key ) <EOL> if not self . IsAuthorized ( crud_model . Actions . UPDATE , user , ex ) : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> ex . UpdateFromJsonDict ( json . loads ( self . request . body ) ) <EOL> if hasattr ( ex , '<STR_LIT>' ) : <EOL> ex . last_updated_by = user . email ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> ans = self . postEntity ( ex , json . loads ( self . request . body ) ) <EOL> else : <EOL> key = ex . put ( ) <EOL> ans = self . _GetJsonDict ( ex ) <EOL> ans [ '<STR_LIT:id>' ] = key . id ( ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> self . response . out . write ( JSON_PREFIX + json . dumps ( ans , default = JsonPrinter ) ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> parent_id = self . request . get ( '<STR_LIT>' ) <EOL> if parent_id : <EOL> if issubclass ( self . model , ndb . Model ) : <EOL> parent_key = ndb . Key ( self . model . parent_model_name , long ( parent_id ) ) <EOL> else : <EOL> parent_key = db . Key . from_path ( self . model . parent_model_name , <EOL> long ( parent_id ) ) <EOL> else : <EOL> parent_key = None <EOL> ex = self . model . get_by_id ( long ( self . request . get ( '<STR_LIT:id>' ) ) , <EOL> parent = parent_key ) <EOL> if not self . IsAuthorized ( crud_model . Actions . DELETE , user , ex ) : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> if hasattr ( ex , '<STR_LIT>' ) : <EOL> ex . delete ( ) <EOL> else : <EOL> ex . key . delete ( ) </s>
