<s> from schematics . types . base import GeoPointType <EOL> from schematics . types . compound import ListType , DictType , ModelType <EOL> import tornado . gen <EOL> import tornado . web <EOL> import tornado . escape <EOL> from schematics . exceptions import ValidationError , ModelConversionError <EOL> from bson . objectid import ObjectId <EOL> import json <EOL> import datetime <EOL> from mingus . serializers import JSONSerializer <EOL> class ModelParams ( object ) : <EOL> def __init__ ( self , fields , args , kwargs , arguments ) : <EOL> self . fields = fields <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . arguments = arguments <EOL> def getFieldKeys ( self ) : <EOL> return self . fields . keys ( ) <EOL> def getParams ( self ) : <EOL> """<STR_LIT>""" <EOL> types = [ DictType , GeoPointType , ListType , ModelType ] <EOL> fieldkeys = self . fields . keys ( ) <EOL> j = JSONSerializer ( ) <EOL> if '<STR_LIT:q>' in self . arguments : <EOL> params = tornado . escape . to_unicode ( self . arguments [ '<STR_LIT:q>' ] [ <NUM_LIT:0> ] ) <EOL> params = tornado . escape . json_decode ( params ) <EOL> self . arguments = params <EOL> else : <EOL> for k , v in self . arguments . items ( ) : <EOL> if type ( v ) is list and k in fieldkeys and self . fields [ k ] not in types : <EOL> self . arguments [ k ] = self . arguments [ k ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf-8>" ) <EOL> if len ( self . args ) : <EOL> self . arguments [ '<STR_LIT>' ] = self . args [ <NUM_LIT:0> ] <EOL> return self . arguments <EOL> def getParamsParse ( self ) : <EOL> """<STR_LIT>""" <EOL> j = JSONSerializer ( ) <EOL> if '<STR_LIT>' in self . arguments : <EOL> params = tornado . escape . to_unicode ( self . arguments [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> params = tornado . escape . json_decode ( params ) <EOL> self . arguments [ '<STR_LIT>' ] = params <EOL> else : <EOL> for k , v in self . arguments . items ( ) : <EOL> self . arguments = j . deserialize ( k ) <EOL> break <EOL> if len ( self . args ) : <EOL> self . arguments [ '<STR_LIT>' ] = self . args [ <NUM_LIT:0> ] <EOL> return self . arguments <EOL> class Model ( object ) : <EOL> responseDict = "<STR_LIT>" <EOL> def __init__ ( self , collection , schematic , params ) : <EOL> self . collection = collection <EOL> self . schematic = schematic <EOL> self . params = params <EOL> def setResponseDict ( self ) : <EOL> """<STR_LIT>""" <EOL> def pagination ( self ) : <EOL> params = self . params . getParams ( ) <EOL> if '<STR_LIT>' in params : <EOL> limit = params . get ( '<STR_LIT>' , <NUM_LIT:50> ) <EOL> if int ( limit [ <NUM_LIT:0> ] ) > <NUM_LIT:100> : <EOL> limit = <NUM_LIT:100> <EOL> else : <EOL> limit = int ( limit [ <NUM_LIT:0> ] ) <EOL> else : <EOL> limit = <NUM_LIT:50> <EOL> if '<STR_LIT>' in params : <EOL> page = int ( params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> page = <NUM_LIT:1> <EOL> pagination = { } <EOL> pagination [ '<STR_LIT>' ] = page <EOL> pagination [ '<STR_LIT>' ] = limit <EOL> pagination [ '<STR_LIT>' ] = page + <NUM_LIT:1> <EOL> previous = page - <NUM_LIT:1> <EOL> if previous < <NUM_LIT:1> : <EOL> previous = None <EOL> pagination [ '<STR_LIT>' ] = previous <EOL> return pagination <EOL> def setResponseDictSuccess ( self , result , meta = None ) : <EOL> if meta : <EOL> self . responseDict = { "<STR_LIT:status>" : "<STR_LIT>" , "<STR_LIT>" : meta , "<STR_LIT>" : result } <EOL> else : <EOL> self . responseDict = { "<STR_LIT:status>" : "<STR_LIT>" , "<STR_LIT:object>" : result } <EOL> def setResponseDictErrors ( self , errors ) : <EOL> self . responseDict = { "<STR_LIT:status>" : "<STR_LIT>" , "<STR_LIT>" : errors } <EOL> def getIdDict ( self ) : <EOL> params = self . params . getParams ( ) <EOL> if '<STR_LIT:id>' in params : <EOL> oid = { '<STR_LIT:id>' : int ( params [ '<STR_LIT:id>' ] ) } <EOL> elif '<STR_LIT>' in params : <EOL> oid = { '<STR_LIT>' : ObjectId ( params [ '<STR_LIT>' ] ) } <EOL> else : <EOL> oid = None <EOL> return oid <EOL> def getSlace ( self , ** pagination ) : <EOL> end = pagination [ '<STR_LIT>' ] * pagination [ '<STR_LIT>' ] <EOL> start = end - pagination [ '<STR_LIT>' ] <EOL> slace = { '<STR_LIT:start>' : start , '<STR_LIT:end>' : end } <EOL> return slace <EOL> def getResponseDict ( self ) : <EOL> return self . responseDict <EOL> def blacklist ( self ) : <EOL> f = self . schematic ( ) <EOL> try : <EOL> f_whitelist = [ field for field in f . to_primitive ( role = '<STR_LIT>' ) . keys ( ) ] <EOL> print ( f_whitelist ) <EOL> blacklist = { k : <NUM_LIT:0> for k in self . params . getFieldKeys ( ) if k not in f_whitelist } <EOL> print ( blacklist ) <EOL> except : <EOL> blacklist = None <EOL> return blacklist <EOL> def queryset ( self ) : <EOL> f = self . schematic ( ) <EOL> params = self . params . getParams ( ) <EOL> try : <EOL> query = f . queryset ( ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( query ) <EOL> for k , v in query . items ( ) : <EOL> params [ k ] = v <EOL> print ( '<STR_LIT>' ) <EOL> print ( params ) <EOL> except : <EOL> pass <EOL> return params <EOL> class ResourceModel ( Model ) : <EOL> @ tornado . gen . coroutine <EOL> def getlist ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> blacklist = self . blacklist ( ) <EOL> params = self . queryset ( ) <EOL> pagination = self . pagination ( ) <EOL> slace = self . getSlace ( ** pagination ) <EOL> start , end = ( slace [ '<STR_LIT:start>' ] , slace [ '<STR_LIT:end>' ] ) <EOL> try : <EOL> count = yield self . collection . find ( params ) [ start : end ] . count ( ) <EOL> cursor = self . collection . find ( params , blacklist ) . sort ( [ ( '<STR_LIT>' , - <NUM_LIT:1> ) ] ) [ start : end ] <EOL> meta = { <EOL> "<STR_LIT>" : pagination [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : pagination [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : pagination [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : pagination [ '<STR_LIT>' ] , <EOL> } <EOL> meta [ '<STR_LIT>' ] = str ( count ) <EOL> if count < pagination [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] = None <EOL> objects = [ ] <EOL> while ( yield cursor . fetch_next ) : <EOL> obj = cursor . next_object ( ) <EOL> obj [ '<STR_LIT>' ] = uri + str ( obj [ "<STR_LIT>" ] ) <EOL> objects . append ( obj ) <EOL> results = [ document for document in objects ] <EOL> self . setResponseDictSuccess ( results , meta ) <EOL> except Exception as e : <EOL> self . setResponseDictErrors ( "<STR_LIT>" ) <EOL> return <EOL> @ tornado . gen . coroutine <EOL> def getobj ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> oid = self . getIdDict ( ) <EOL> blacklist = self . blacklist ( ) <EOL> try : <EOL> result = yield self . collection . find_one ( oid , blacklist ) <EOL> result [ '<STR_LIT>' ] = str ( result [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT>' ] = uri <EOL> self . setResponseDictSuccess ( result ) <EOL> except Exception as e : <EOL> self . setResponseDictErrors ( "<STR_LIT>" ) <EOL> return <EOL> @ tornado . gen . coroutine <EOL> def post ( self ) : <EOL> params = self . params . getParamsParse ( ) <EOL> if '<STR_LIT>' in params : <EOL> try : <EOL> self . bulk ( ) <EOL> self . setResponseDictSuccess ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> except ValidationError as e : <EOL> self . setResponseDictErrors ( e ) <EOL> return <EOL> else : <EOL> obj = self . schematic ( params ) <EOL> try : <EOL> obj . validate ( ) <EOL> obj . created = datetime . datetime . utcnow ( ) <EOL> obj . _id = ObjectId ( ) <EOL> result = yield self . collection . insert ( obj . to_native ( ) ) <EOL> self . setResponseDictSuccess ( { "<STR_LIT>" : str ( result ) } ) <EOL> except ValidationError as e : <EOL> self . setResponseDictErrors ( e . messages ) <EOL> return <EOL> @ tornado . gen . coroutine <EOL> def put ( self ) : <EOL> params = self . params . getParamsParse ( ) <EOL> obj = { key : value for key , value in params . items ( ) if key is not '<STR_LIT>' } <EOL> oid = self . getIdDict ( ) <EOL> obj_validate = self . schematic ( params ) <EOL> try : <EOL> obj_validate . validate ( ) <EOL> result = yield self . collection . update ( oid , obj , upsert = False ) <EOL> self . setResponseDictSuccess ( { "<STR_LIT>" : str ( result ) } ) <EOL> except ValidationError as e : <EOL> self . setResponseDictErrors ( e . messages ) <EOL> return <EOL> @ tornado . gen . coroutine <EOL> def delete ( self ) : <EOL> oid = self . getIdDict ( ) <EOL> try : <EOL> result = yield self . collection . remove ( oid ) <EOL> self . setResponseDictSuccess ( { "<STR_LIT>" : str ( result ) } ) <EOL> except ValidationError as e : <EOL> self . setResponseDictErrors ( e ) <EOL> return <EOL> @ tornado . gen . coroutine <EOL> def bulk ( self ) : <EOL> params = self . params . getParams ( ) <EOL> data = params [ '<STR_LIT>' ] <EOL> try : <EOL> bulk = self . collection . initialize_ordered_bulk_op ( ) <EOL> data = params [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in data : <EOL> for obj in data [ '<STR_LIT>' ] : <EOL> obj [ '<STR_LIT>' ] = datetime . datetime . utcnow ( ) <EOL> obj [ '<STR_LIT>' ] = ObjectId ( ) <EOL> bulk . insert ( obj ) <EOL> result = yield bulk . execute ( ) <EOL> except BulkWriteError as e : <EOL> self . setResponseDictErrors ( e ) <EOL> '''<STR_LIT>''' <EOL> @ tornado . gen . coroutine <EOL> def patch ( self ) : <EOL> params = self . params . getParamsParse ( ) <EOL> obj = { key : value for key , value in params . items ( ) if key is not '<STR_LIT>' } <EOL> oid = self . getIdDict ( ) <EOL> try : <EOL> result = yield self . collection . update ( oid , { '<STR_LIT>' : obj } , upsert = True ) <EOL> self . setResponseDictSuccess ( { "<STR_LIT>" : params [ '<STR_LIT>' ] } ) <EOL> except ValidationError as e : <EOL> self . setResponseDictErrors ( e ) <EOL> return <EOL> try : <EOL> from services . resource import models_factory <EOL> except : <EOL> models_factory = { k : ResourceModel for k in [ "<STR_LIT:GET>" , "<STR_LIT:POST>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> models = models_factory </s>
