<s> from datetime import datetime <EOL> from uuid import uuid4 <EOL> import re <EOL> import unicodedata <EOL> import urllib <EOL> from google . appengine . datastore . datastore_query import Cursor <EOL> from google . appengine . ext import blobstore <EOL> from google . appengine . ext import ndb <EOL> import flask <EOL> import config <EOL> def param ( name , cast = None ) : <EOL> '''<STR_LIT>''' <EOL> value = None <EOL> if flask . request . json : <EOL> return flask . request . json . get ( name , None ) <EOL> if value is None : <EOL> value = flask . request . args . get ( name , None ) <EOL> if value is None and flask . request . form : <EOL> value = flask . request . form . get ( name , None ) <EOL> if cast and value is not None : <EOL> if cast == bool : <EOL> return value . lower ( ) in [ '<STR_LIT:true>' , '<STR_LIT:yes>' , '<STR_LIT:1>' , '<STR_LIT>' ] <EOL> if cast == list : <EOL> return value . split ( '<STR_LIT:U+002C>' ) if len ( value ) > <NUM_LIT:0> else [ ] <EOL> return cast ( value ) <EOL> return value <EOL> def get_next_url ( ) : <EOL> next = param ( '<STR_LIT>' ) <EOL> if next : <EOL> return next <EOL> referrer = flask . request . referrer <EOL> if referrer and referrer . startswith ( flask . request . host_url ) : <EOL> return referrer <EOL> return flask . url_for ( '<STR_LIT>' ) <EOL> def retrieve_dbs ( query , order = None , limit = None , cursor = None , ** filters ) : <EOL> '''<STR_LIT>''' <EOL> limit = limit or config . DEFAULT_DB_LIMIT <EOL> cursor = Cursor . from_websafe_string ( cursor ) if cursor else None <EOL> model_class = ndb . Model . _kind_map [ query . kind ] <EOL> if order : <EOL> for o in order . split ( '<STR_LIT:U+002C>' ) : <EOL> if o . startswith ( '<STR_LIT:->' ) : <EOL> query = query . order ( - model_class . _properties [ o [ <NUM_LIT:1> : ] ] ) <EOL> else : <EOL> query = query . order ( model_class . _properties [ o ] ) <EOL> for prop in filters : <EOL> if filters . get ( prop , None ) is None : <EOL> continue <EOL> if isinstance ( filters [ prop ] , list ) : <EOL> for value in filters [ prop ] : <EOL> query = query . filter ( model_class . _properties [ prop ] == value ) <EOL> else : <EOL> query = query . filter ( model_class . _properties [ prop ] == filters [ prop ] ) <EOL> model_dbs , more_cursor , more = query . fetch_page ( limit , start_cursor = cursor ) <EOL> more_cursor = more_cursor . to_websafe_string ( ) if more else None <EOL> return list ( model_dbs ) , more_cursor <EOL> def jsonify_model_dbs ( model_dbs , more_cursor = None ) : <EOL> '''<STR_LIT>''' <EOL> result_objects = [ ] <EOL> for model_db in model_dbs : <EOL> result_objects . append ( model_db_to_object ( model_db ) ) <EOL> response_object = { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:count>' : len ( result_objects ) , <EOL> '<STR_LIT>' : datetime . utcnow ( ) . isoformat ( ) , <EOL> '<STR_LIT:result>' : result_objects , <EOL> } <EOL> if more_cursor : <EOL> response_object [ '<STR_LIT>' ] = more_cursor <EOL> response_object [ '<STR_LIT>' ] = generate_more_url ( more_cursor ) <EOL> response = jsonpify ( response_object ) <EOL> return response <EOL> def jsonify_model_db ( model_db ) : <EOL> '''<STR_LIT>''' <EOL> result_object = model_db_to_object ( model_db ) <EOL> response = jsonpify ( { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT>' : datetime . utcnow ( ) . isoformat ( ) , <EOL> '<STR_LIT:result>' : result_object , <EOL> } ) <EOL> return response <EOL> def model_db_to_object ( model_db ) : <EOL> model_db_object = { } <EOL> for prop in model_db . _PROPERTIES : <EOL> if prop == '<STR_LIT:id>' : <EOL> try : <EOL> value = json_value ( getattr ( model_db , '<STR_LIT:key>' , None ) . id ( ) ) <EOL> except : <EOL> value = None <EOL> else : <EOL> value = json_value ( getattr ( model_db , prop , None ) ) <EOL> if value is not None : <EOL> model_db_object [ prop ] = value <EOL> return model_db_object <EOL> def json_value ( value ) : <EOL> if isinstance ( value , datetime ) : <EOL> return value . isoformat ( ) <EOL> if isinstance ( value , ndb . Key ) : <EOL> return value . urlsafe ( ) <EOL> if isinstance ( value , blobstore . BlobKey ) : <EOL> return urllib . quote ( str ( value ) ) <EOL> if isinstance ( value , ndb . GeoPt ) : <EOL> return '<STR_LIT>' % ( value . lat , value . lon ) <EOL> if isinstance ( value , list ) : <EOL> return [ json_value ( v ) for v in value ] <EOL> if isinstance ( value , long ) : <EOL> if value > <NUM_LIT> or value < - <NUM_LIT> : <EOL> return str ( value ) <EOL> if isinstance ( value , ndb . Model ) : <EOL> return model_db_to_object ( value ) <EOL> return value <EOL> def jsonpify ( * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if param ( '<STR_LIT>' ) : <EOL> content = '<STR_LIT>' % ( <EOL> param ( '<STR_LIT>' ) , flask . jsonify ( * args , ** kwargs ) . data , <EOL> ) <EOL> mimetype = '<STR_LIT>' <EOL> return flask . current_app . response_class ( content , mimetype = mimetype ) <EOL> return flask . jsonify ( * args , ** kwargs ) <EOL> def generate_more_url ( more_cursor , base_url = None , cursor_name = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if not more_cursor : <EOL> return None <EOL> base_url = base_url or flask . request . base_url <EOL> args = flask . request . args . to_dict ( ) <EOL> args [ cursor_name ] = more_cursor <EOL> return '<STR_LIT>' % ( base_url , urllib . urlencode ( args ) ) <EOL> def uuid ( ) : <EOL> '''<STR_LIT>''' <EOL> return uuid4 ( ) . hex <EOL> _slugify_strip_re = re . compile ( r'<STR_LIT>' ) <EOL> _slugify_hyphenate_re = re . compile ( r'<STR_LIT>' ) <EOL> def slugify ( value ) : <EOL> if not isinstance ( value , unicode ) : <EOL> value = unicode ( value ) <EOL> value = unicodedata . normalize ( '<STR_LIT>' , value ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> value = unicode ( _slugify_strip_re . sub ( '<STR_LIT>' , value ) . strip ( ) . lower ( ) ) <EOL> return _slugify_hyphenate_re . sub ( '<STR_LIT:->' , value ) <EOL> def is_valid_username ( username ) : <EOL> return True if re . match ( '<STR_LIT>' , username ) else False <EOL> strip_filter = lambda x : x . strip ( ) if x else '<STR_LIT>' <EOL> email_filter = lambda x : x . lower ( ) . strip ( ) if x else '<STR_LIT>' </s>
