from flask import Blueprint, request, render_template
from recipyGui import recipyGui, db
from forms import SearchForm

routes = Blueprint('routes', __name__, template_folder='templates')


@recipyGui.route('/')
def index():
    form = SearchForm()

    query = request.args.get('query', '')

    if not query:
        # Return all runs, ordered by date (oldest run first)
        # TODO: find out iftinydb supports sorting by date
        runs = db.all()[::-1]
    else:
        # Search runs using the query string
        # TODO: fix search
        runs = []

    return render_template('list.html', runs=runs, query=query, form=form)


@recipyGui.route('/run_details')
def run_details():
        form = SearchForm()
        query = request.args.get('query', '')
        run_id = int(request.args.get('id'))

        r = db.get(eid=run_id)

        return render_template('details.html', query=query, form=form, run=r)

@@ -111,16 +111,95 @@
 archForm
+%0Afrom tinydb import where%0Aimport re%0Afrom dateutil.parser import parse%0Aimport os
 %0A%0Aroutes
@@ -395,250 +395,272 @@
     
-# Return all runs, ordered by date (oldest run first)%0A        # TODO: find out iftinydb supports sorting by date%0A        runs = db.all()%5B::-1%5D%0A    else:%0A        # Search runs using the query string%0A        # TODO: fix search%0A        runs = %5B%5D
+runs = db.all()%0A    else:%0A        # Search run outputs using the query string%0A        runs = db.search(where('outputs').any(lambda x: re.match(%22.+%25s.+%22 %25 query, x)))%0A    runs = sorted(runs, key = lambda x: parse(x%5B'date'%5D.replace('%7BTinyDate%7D:', '')), reverse=True)
 %0A%0A  

Fix searching