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 builds(request, id=None): api = API(request) if id is None: if api.get: builds = Build.list() return api.respond( builds, template='builds/builds.html' ) elif api.post: build = Build.create( user=request.user, package=api.required('package'), flavour=api.required('flavour'), version=api.required('version'), commit=api.required('commit'), platform=api.optional('platform'), url=api.optional('url') ) return api.respond(build) else: if api.get: build = Build.objects.get( id=id ) return api.respond(build) raise API.MethodNotAllowedError( method=request.method )
def content(request, address): ''' Get or set the content of a stencil ''' api = API(request) if api.get: component = Component.get( id=None, user=request.user, action=READ, address=address ) result = component.content_get( format=api.optional('format', 'html'), ) elif api.put: component = Component.get( id=None, user=request.user, action=UPDATE, address=address ) result = component.content_set( user=request.user, format=api.required('format'), content=api.required('content'), revision=api.required('revision') ) return api.respond(result)
def tokens(request, id=None): ''' List, create, read, update, delete user tokens ''' api = API(request) if id is None: if api.get: # List all tokens for the user tokens = UserToken.objects.filter( user=request.user ) return api.respond(tokens) elif api.post: # Create a token for the user token = UserToken.objects.create( user=request.user, name=api.optional('name'), notes=api.optional('notes') ) return api.respond(token) else: # Get token, ensuring it is associated with the # request user token = UserToken.get_or_raise( id=id, user=request.user ) if api.get: # Read it return api.respond(token) elif api.patch: # Update it token.name = api.optional('name') token.notes = api.optional('notes') token.save() return api.respond(token) elif api.delete: # Delete it token.delete() return api.respond()