示例#1
0
文件: views.py 项目: stencila/hub
def snippets(request, id=None):
    api = API(request)
    if id is None:
        if api.get:
            snippets = Snippet.list()
            return api.respond(
            	snippets,
            	detail=0,
            	template='snippets/list.html',
            	context={
            		'languages':['js','jl','py','r']
            	}
            )
    else:
        if api.get:
            snippet = Snippet.get(
                id=id
            )
            return api.respond(
            	snippet,
            	template='snippets/get.html'
            )
        elif api.put:
            snippet = Snippet.put(
                user=request.user,
                id=id,
                spec=api.data
            )
            return api.respond(
            	snippet,
            	detail=0
            )

    raise API.MethodNotAllowedError(
        method=request.method
    )
示例#2
0
def snippet(slug):
    """ Retrieves a named text snippet """
    try:
        return Snippet.get(slug)
    except Exception:
        return ''