コード例 #1
0
ファイル: test_short_queries.py プロジェクト: spookey/shorter
    def test_searched():
        assert Short.query.count() == 0
        one = Short.create(symbol="one", target="_cherry", delay=3)
        two = Short.create(symbol="two", target="_eggplant", delay=2)
        thr = Short.create(symbol="thr", target="_peach", delay=1)
        assert Short.query.count() == 3

        assert Short.searched("one").all() == [one]
        assert Short.searched("two").all() == [two]

        assert Short.searched("err").all() == [one]
        assert Short.searched("egg").all() == [two]

        assert Short.searched("t").all() == [two, thr]
        assert Short.searched("p").all() == [two, thr]
        assert Short.searched("_").all() == [one, two, thr]
        assert Short.searched("_", rev=True).all() == [thr, two, one]

        assert Short.searched("_", field="delay", rev=False).all() == [
            thr,
            two,
            one,
        ]
        assert Short.searched("_", field="delay", rev=True).all() == [
            one,
            two,
            thr,
        ]
コード例 #2
0
def show(page=None, field=None, sort=None):
    term = request.args.get("q", None)
    form = ShortFindForm(term=term)
    if form.validate_on_submit():
        target = form.action()
        if target:
            return redirect(target)

    query = (
        Short.searched(term, field=field, rev=sort == "desc")
        if term is not None
        else Short.ordered(field=field, rev=sort == "desc")
    )
    if not query:
        abort(404)

    return render_template(
        "plus/show.html",
        title="Show",
        form=form,
        elements=query.paginate(
            page=page, per_page=current_app.config["PAGINATION"]
        ),
    )