Beispiel #1
0
def collections():
    # pylint: disable=no-member
    q = Collection.query

    if request.method == 'PUT':
        c = q.get_or_404(Collection.create(_get_json_or_400()))
        return jsonify(collection_to_resource(c)), 201

    # Treat all other methods as "GET"

    # Where do we start?
    page_start = request.args.get('after')

    q = q.with_permission(Permission.READ).order_by(Collection.id)
    if page_start is not None and page_start != '':
        q = q.filter(Collection.id > Collection.decode_key(page_start))
    q = q.limit(current_app.config['PAGE_SIZE'])

    cs = list(q)
    page_end = cs[-1].encoded_key if len(cs) > 0 else page_start

    resources = [collection_to_resource(c) for c in cs]
    next_url = url_for('api.collections', after=page_end)

    return jsonify(dict(
        resources=resources, next=next_url,
    ))
Beispiel #2
0
def collection_delete(key):
    # pylint: disable=no-member
    c = Collection.query.get_for_current_user_or_404(Collection.decode_key(key))
    try:
        c.delete()
    except ModelError:
        raise Unauthorized('no delete permission')
    return redirect(url_for('ui.index'))
Beispiel #3
0
def collection(key):
    # pylint: disable=no-member
    c = Collection.query.get_for_current_user_or_404(
        Collection.decode_key(key)
    )

    resource = dict(name=c.name)

    return jsonify(resource)
Beispiel #4
0
def collection_create():
    # pylint: disable=no-member

    if request.method == 'POST':
        d = dict(name=request.form['name'])
        c = Collection.query.get_or_404(Collection.create(d))
        return redirect(url_for('ui.collection', key=c.encoded_key))

    # treat other methods as "GET"
    return render_template('collection_create.html')
Beispiel #5
0
def collection(key):
    # pylint: disable=no-member
    c = Collection.query.get_for_current_user_or_404(Collection.decode_key(key))
    return render_template('collection.html', collection=c)
Beispiel #6
0
def test_create_needs_name(user):
    with pytest.raises(ModelError):
        Collection.create(dict(name=''))
    with pytest.raises(ModelError):
        Collection.create({})