Пример #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,
    ))
Пример #2
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')
Пример #3
0
def test_create_needs_name(user):
    with pytest.raises(ModelError):
        Collection.create(dict(name=''))
    with pytest.raises(ModelError):
        Collection.create({})