<s> import uuid <EOL> import six <EOL> from pyramid import httpexceptions <EOL> from pyramid . settings import asbool <EOL> from pyramid . security import NO_PERMISSION_REQUIRED , Authenticated <EOL> from cliquet . errors import raise_invalid <EOL> from cliquet . events import ACTIONS <EOL> from cliquet . utils import build_request , reapply_cors , hmac_digest <EOL> from cliquet . storage import exceptions as storage_exceptions <EOL> from kinto . authorization import RouteFactory <EOL> from kinto . views . buckets import Bucket <EOL> from kinto . views . collections import Collection <EOL> def create_bucket ( request , bucket_id ) : <EOL> """<STR_LIT>""" <EOL> bucket_put = ( request . method . lower ( ) == '<STR_LIT>' and <EOL> request . path . endswith ( '<STR_LIT>' ) ) <EOL> if bucket_put : <EOL> return <EOL> already_created = request . bound_data . setdefault ( '<STR_LIT>' , { } ) <EOL> if bucket_id in already_created : <EOL> return <EOL> bucket = resource_create_object ( request = request , <EOL> resource_cls = Bucket , <EOL> uri = '<STR_LIT>' % bucket_id , <EOL> resource_name = '<STR_LIT>' , <EOL> obj_id = bucket_id ) <EOL> already_created [ bucket_id ] = bucket <EOL> def create_collection ( request , bucket_id ) : <EOL> subpath = request . matchdict . get ( '<STR_LIT>' ) <EOL> if not ( subpath and subpath . startswith ( '<STR_LIT>' ) ) : <EOL> return <EOL> collection_id = subpath . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> collection_uri = '<STR_LIT>' % ( bucket_id , collection_id ) <EOL> already_created = request . bound_data . setdefault ( '<STR_LIT>' , { } ) <EOL> if collection_uri in already_created : <EOL> return <EOL> collection_put = ( request . method . lower ( ) == '<STR_LIT>' and <EOL> request . path . endswith ( collection_id ) ) <EOL> if collection_put : <EOL> return <EOL> backup_matchdict = request . matchdict <EOL> request . matchdict = dict ( bucket_id = bucket_id , <EOL> id = collection_id , <EOL> ** request . matchdict ) <EOL> collection = resource_create_object ( request = request , <EOL> resource_cls = Collection , <EOL> uri = collection_uri , <EOL> resource_name = '<STR_LIT>' , <EOL> obj_id = collection_id ) <EOL> already_created [ collection_uri ] = collection <EOL> request . matchdict = backup_matchdict <EOL> def resource_create_object ( request , resource_cls , uri , resource_name , obj_id ) : <EOL> """<STR_LIT>""" <EOL> context = RouteFactory ( request ) <EOL> context . get_permission_object_id = lambda r , i : uri <EOL> resource = resource_cls ( request , context ) <EOL> if not resource . model . id_generator . match ( obj_id ) : <EOL> error_details = { <EOL> '<STR_LIT:location>' : '<STR_LIT:path>' , <EOL> '<STR_LIT:description>' : "<STR_LIT>" % resource_name <EOL> } <EOL> raise_invalid ( resource . request , ** error_details ) <EOL> data = { '<STR_LIT:id>' : obj_id } <EOL> try : <EOL> obj = resource . model . create_record ( data ) <EOL> resource . request . current_resource_name = resource_name <EOL> resource . postprocess ( data , action = ACTIONS . CREATE ) <EOL> except storage_exceptions . UnicityError as e : <EOL> obj = e . record <EOL> return obj <EOL> def default_bucket ( request ) : <EOL> if request . method . lower ( ) == '<STR_LIT>' : <EOL> path = request . path . replace ( '<STR_LIT:default>' , '<STR_LIT>' ) <EOL> subrequest = build_request ( request , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:path>' : path <EOL> } ) <EOL> return request . invoke_subrequest ( subrequest ) <EOL> if Authenticated not in request . effective_principals : <EOL> raise httpexceptions . HTTPForbidden ( ) <EOL> settings = request . registry . settings <EOL> if asbool ( settings [ '<STR_LIT>' ] ) : <EOL> raise httpexceptions . HTTPMethodNotAllowed ( ) <EOL> bucket_id = request . default_bucket_id <EOL> path = request . path . replace ( '<STR_LIT>' , '<STR_LIT>' % bucket_id ) <EOL> querystring = request . url [ ( request . url . index ( request . path ) + <EOL> len ( request . path ) ) : ] <EOL> create_bucket ( request , bucket_id ) <EOL> create_collection ( request , bucket_id ) <EOL> subrequest = build_request ( request , { <EOL> '<STR_LIT>' : request . method , <EOL> '<STR_LIT:path>' : path + querystring , <EOL> '<STR_LIT:body>' : request . body <EOL> } ) <EOL> subrequest . bound_data = request . bound_data <EOL> try : <EOL> response = request . invoke_subrequest ( subrequest ) <EOL> except httpexceptions . HTTPException as error : <EOL> is_redirect = error . status_code < <NUM_LIT> <EOL> if error . content_type == '<STR_LIT:application/json>' or is_redirect : <EOL> response = reapply_cors ( subrequest , error ) <EOL> else : <EOL> raise error <EOL> return response <EOL> def default_bucket_id ( request ) : <EOL> settings = request . registry . settings <EOL> secret = settings [ '<STR_LIT>' ] <EOL> digest = hmac_digest ( secret , request . prefixed_userid ) <EOL> return six . text_type ( uuid . UUID ( digest [ : <NUM_LIT:32> ] ) ) <EOL> def get_user_info ( request ) : <EOL> user_info = { <EOL> '<STR_LIT:id>' : request . prefixed_userid , <EOL> '<STR_LIT>' : request . default_bucket_id <EOL> } <EOL> return user_info <EOL> def includeme ( config ) : <EOL> config . add_view ( default_bucket , <EOL> route_name = '<STR_LIT>' , <EOL> permission = NO_PERMISSION_REQUIRED ) <EOL> config . add_view ( default_bucket , <EOL> route_name = '<STR_LIT>' , <EOL> permission = NO_PERMISSION_REQUIRED ) <EOL> config . add_route ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> config . add_route ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> config . add_request_method ( default_bucket_id , reify = True ) <EOL> config . add_request_method ( get_user_info ) <EOL> config . add_api_capability ( <EOL> "<STR_LIT>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> url = "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
