def component_one(request, id): # TODO integrate into the above api = API(request) if api.get: component = Component.read_one( id=id, user=request.user, ) return api.respond( data=component ) elif api.patch: # Currently, PATCH is not valid for components api.raise_method_not_allowed() # api.authenticated_or_raise() # component = Component.update_one( # id=id, # user=request.user, # data=api.data # ) # return api.respond( # data=component # ) elif api.delete: api.authenticated_or_raise() Component.delete_one( id=id, user=request.user ) return api.respond() else: api.raise_method_not_allowed()
def key_list(request): ''' For: GET /keys POST /keys ''' api = API(request) api.authenticated_or_raise() if api.get: keys = Key.list_user( user=request.user ) return api.respond( data=keys, paginate=True ) elif api.post: key = Key.create_one( user=request.user, address=api.required('address'), type=api.required('type'), action=api.required('action'), users=api.required('users') ) return api.respond( data=key ) else: return api.raise_method_not_allowed()
def address_list(request): ''' For: GET /addresses POST /addresses ''' api = API(request) api.authenticated_or_raise() if api.get: addresses = Address.list( user=request.user ) return api.respond( data=addresses, paginate=100 ) elif api.post: address = Address.create_one( user=request.user, address=api.required('address'), type=api.required('type'), action=api.required('action'), users=api.required('users') ) return api.respond( data=address ) else: return api.raise_method_not_allowed()
def address_one(request, id): ''' For: GET /addresses/{id} PATCH /addresses/{id} DELETE /addresses/{id} ''' api = API(request) api.authenticated_or_raise() if api.get: address = Address.read_one( id=id, user=request.user ) return api.respond( data=address ) elif api.patch: address = Address.update_one( id=id, user=request.user, data=api.data ) return api.respond( data=address ) elif api.delete: address = Address.delete_one( id=id, user=request.user ) return api.respond() else: api.raise_method_not_allowed()
def components(request, id=None, type=None): ''' Standard API endpoint for components ''' api = API(request) if api.get: filters = {} if type: type = type[:-1] # Remove the trailing 's' that is in the URL filters['type'] = type components = Component.list( user=request.user, address=api.optional('address'), published=api.optional('published', True, lambda value: bool(int(value))), filters=filters, sort=api.optional('sort', 'id') ) return api.respond( data=components, paginate=25 ) elif api.post: api.authenticated_or_raise() component = Component.create_one( user=request.user, address=api.required('address'), type=api.required('type'), ) return api.respond( data=component ) else: api.raise_method_not_allowed()
def snapshot(request, address=None): api = API(request) api.authenticated_or_raise() if address: if api.post: snapshot = Snapshot.create( address=address, user=request.user, file=request.FILES['file'] ) return api.respond(snapshot) elif api.get: snapshot_url = Snapshot.get( address=address, user=request.user ) if snapshot_url: return redirect(snapshot_url) else: raise Http404 else: if api.get: snapshots = Snapshot.list( user=request.user ) return api.respond(snapshots)
def key_one(request, id): ''' For: GET /keys/{id} PATCH /keys/{id} DELETE /keys/{id} ''' api = API(request) api.authenticated_or_raise() if api.get: key = Key.read_one( id=id, user=request.user ) return api.respond( data=key ) elif api.patch: key = Key.update_one( id=id, user=request.user, data=api.data ) return api.respond( data=key ) elif api.delete: key = Key.delete_one( id=id, user=request.user ) return api.respond() else: api.raise_method_not_allowed()
def component_fork(request, id): ''' Fork a component For: POST /components/{id}/fork ''' api = API(request) api.authenticated_or_raise() component = Component.get( id=id, user=request.user, action=READ, ) if api.post: fork = component.fork( user=request.user, to=api.required('to') ) return api.respond(fork) else: api.raise_method_not_allowed()
def component_star(request, id): ''' Add/remove the component to/from the request user's starred list For: PUT /components/{id}/star DELETE /components/{id}/star ''' api = API(request) api.authenticated_or_raise() component = Component.get( id=id, user=request.user, action=READ ) if api.put: component.star(request.user) elif api.delete: component.unstar(request.user) else: api.raise_method_not_allowed() return api.respond({ 'stars': component.stars, 'starred': component.starred(request.user) })