 from flask import Flask , Response <EOL> from werkzeug . routing import BaseConverter , ValidationError <EOL> from base64 import urlsafe_b64encode , urlsafe_b64decode <EOL> from bson . objectid import ObjectId <EOL> from bson . errors import InvalidId <EOL> import datetime <EOL> import mmh3 <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> try : <EOL> from bson . objectid import ObjectId <EOL> except : <EOL> pass <EOL> class APIEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , ( datetime . datetime , datetime . date ) ) : <EOL> return obj . ctime ( ) <EOL> elif isinstance ( obj , datetime . time ) : <EOL> return obj . isoformat ( ) <EOL> elif isinstance ( obj , ObjectId ) : <EOL> return str ( obj ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> def jsonify ( data ) : <EOL> return Response ( json . dumps ( data , cls = APIEncoder ) , <EOL> mimetype = '<STR_LIT:application/json>' ) 
 class ObjectIDConverter ( BaseConverter ) : 
 from flask import Flask , Response <EOL> from werkzeug . routing import BaseConverter , ValidationError <EOL> from base64 import urlsafe_b64encode , urlsafe_b64decode <EOL> from bson . objectid import ObjectId <EOL> from bson . errors import InvalidId <EOL> import datetime <EOL> import mmh3 <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> try : <EOL> from bson . objectid import ObjectId <EOL> except : <EOL> pass <EOL> class APIEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , ( datetime . datetime , datetime . date ) ) : <EOL> return obj . ctime ( ) <EOL> elif isinstance ( obj , datetime . time ) : <EOL> return obj . isoformat ( ) <EOL> elif isinstance ( obj , ObjectId ) : <EOL> return str ( obj ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> def jsonify ( data ) : <EOL> return Response ( json . dumps ( data , cls = APIEncoder ) , <EOL> mimetype = '<STR_LIT:application/json>' ) <EOL> class ObjectIDConverter ( BaseConverter ) : <EOL> def to_python ( self , value ) : <EOL> try : <EOL> return ObjectId ( urlsafe_b64decode ( value ) ) <EOL> except ( InvalidId , ValueError , TypeError ) : <EOL> raise ValidationError ( ) <EOL> def to_url ( self , value ) : <EOL> return urlsafe_b64encode ( value . binary ) <EOL> def genPrimaryKey64 ( data ) : <EOL> return "<STR_LIT>" % ( mmh3 . hash128 ( data ) & <NUM_LIT> ) </s>
