Beispiel #1
0
def api_gists():
    """Gists Endpoint"""
    if request.method == 'GET':
        gists = GistModel.query().fetch(20)
        resp = Response(json.dumps(gists, cls=NDBModelEncoder), mimetype='application/json', status=200)
        return resp
    elif request.method == 'POST':
        # data = request.get_json(force=True)
        gist = GistModel()
        gist.added_by = request.form['nick']
        gist.gist = request.form['gist']
        if request.files['img'] is not None:
            gcs_filename = BUCKET + '/blobstore_demo'
            blob_key = CreateFile(gcs_filename)
            gist.image = blob_key
        key = gist.put()

        resp = jsonify(dict(gist.to_dict(), **{'id': key.urlsafe()}))
        resp.status_code = 201
        return resp
    else:
        raise RuntimeError("Unimplemented HTTP method")
Beispiel #2
0
def api_gists():
    """Gists Endpoint"""
    if request.method == "GET":
        gists = GistModel.query().fetch(20)
        resp = Response(json.dumps(gists, cls=NDBModelEncoder), mimetype="application/json", status=200)
        return resp
    elif request.method == "POST":
        gist = GistModel()
        gist.added_by = request.form["nick"]
        gist.gist = request.form["gist"]

        # add image if available
        image = request.files["img"]
        if image is not None:
            filename = secure_filename(image.filename)
            gcs_filename = "/" + get_default_gcs_bucket_name() + "/" + filename
            blob_key = CreateFile(gcs_filename, request.files["img"])
            gist.image = BlobKey(blob_key)
        key = gist.put()
        resp = Response(json.dumps(gist, cls=NDBModelEncoder), mimetype="application/json", status=201)
        return resp
    else:
        raise RuntimeError("Unimplemented HTTP method")