def edit(id):
    artwork = get_model().read(id)

    if request.method == 'POST':
        data = request.form.to_dict(flat=True)

        photo_url = upload_image_file(request.files.get('photo'))

        if photo_url:
            data['photoUrl'] = photo_url

        artwork = get_model().update(data, id)

        return redirect(url_for('.view', id=artwork['id']))

    return render_template("form.html", action="Edit", artwork=artwork)
def list():
    token = request.args.get('page_token', None)
    if token:
        token = token.encode('utf-8')

    artworks, next_page_token = get_model().list(cursor=token)

    return render_template("list.html",
                           artworks=artworks,
                           next_page_token=next_page_token)
def add():
    if request.method == 'POST':
        data = request.form.to_dict(flat=True)

        # If an image was uploaded, update the data to point to the new image.
        # [START image_url]
        photo_url = upload_image_file(request.files.get('photo'))
        # [END image_url]

        # [START image_url2]
        if photo_url:
            data['photoUrl'] = photo_url
        # [END image_url2]

        artwork = get_model().create(data)

        return redirect(url_for('.view', id=artwork['id']))

    return render_template("form.html", action="Add", artwork={})
def view(id):
    artwork = get_model().read(id)
    return render_template("view.html", artwork=artwork)
def delete(id):
    get_model().delete(id)
    return redirect(url_for('.list'))