Mercurial > hg > ltpdarepo
changeset 59:2850a914aef9
Add back the support for saved queries. Still incomplete and experimental.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Sat, 13 Aug 2011 20:27:24 +0200 |
parents | bce8c427a5c5 |
children | 1bfd1f5ec9ba |
files | src/ltpdarepo/query.py src/ltpdarepo/templates/namedquery.html src/ltpdarepo/templates/queries/create.html src/ltpdarepo/templates/queries/index.html src/ltpdarepo/templates/query.html src/ltpdarepo/templates/query/index.html src/ltpdarepo/templates/query/save.html src/ltpdarepo/views/queries.py |
diffstat | 8 files changed, 88 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ltpdarepo/query.py Sat Aug 13 20:27:24 2011 +0200 +++ b/src/ltpdarepo/query.py Sat Aug 13 20:27:24 2011 +0200 @@ -8,7 +8,7 @@ from wtforms.validators import ValidationError class IQuery(Form): - querystring = HiddenField() + query = HiddenField() database = HiddenField() name = TextField("Name", validators=[validators.Required()]) @@ -20,14 +20,14 @@ class Query(dict): - def __init__(self, name=None, db=None, querystring=None): - super(Query, self).__init__(name=name, db=db, querystring=querystring) + def __init__(self, name=None, database=None, query=None): + super(Query, self).__init__(name=name, database=database, query=query) self.__dict__ = self def create(self): curs = g.db.cursor() curs.execute("""INSERT INTO queries (name, db, querystring) - VALUES (%(name)s, %(db)s, %(querystring)s)""", dict(self)) + VALUES (%(name)s, %(database)s, %(query)s)""", dict(self)) g.db.commit() @staticmethod
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/templates/namedquery.html Sat Aug 13 20:27:24 2011 +0200 @@ -0,0 +1,7 @@ +{% extends "objs.html" %} + +{% block above %} +<div class="querystring"> + <pre>{{ query }}</pre> +</div> +{% endblock %}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/templates/queries/create.html Sat Aug 13 20:27:24 2011 +0200 @@ -0,0 +1,9 @@ +{% import 'forms.html' as forms %} +{% extends "layout.html" %} +{% block title %}Save query{% endblock %} +{% block body %} +<h2>Query for database «{{ database.id }}»</h2> +{# <p class="discrete">{{ database.description }}</p> #} +<pre class="querystring">{{ query }}</pre> +{{ forms.render(form) }} +{% endblock %}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/templates/queries/index.html Sat Aug 13 20:27:24 2011 +0200 @@ -0,0 +1,11 @@ +{% extends "layout.html" %} +{% block title %} Queries {% endblock %} +{% block body %} +<h2>Queries</h2> +<p class="discrete">Queries for database «{{ database }}»</p> +<ul> +{% for query in queries %} + <li><a href="{{ url_for('manage.queries.view', database=database, query=query) }}">{{ query }}</a></li> +{% endfor %} +</ul> +{% endblock %}
--- a/src/ltpdarepo/templates/query.html Sat Aug 13 20:27:24 2011 +0200 +++ b/src/ltpdarepo/templates/query.html Sat Aug 13 20:27:24 2011 +0200 @@ -9,6 +9,19 @@ var criteria = {{ criteria|tojson|safe }}; $(function () { $.dropdown.replace($("select")); + $("#save").live('click', function () { + var query = $(".query form").serialize(); + var form = $(document.createElement('form')).attr({ + 'action': '{{ url_for('manage.queries.create', database=database.id) }}', + 'method': 'POST' }); + form.append($(document.createElement('input')).attr({ + 'type': 'hidden', + 'name': 'query', + 'value': query })); + $("body").append(form); + form.submit(); + form.remove(); + }); }); </script> {% endblock %} @@ -58,6 +71,10 @@ <input type="submit" value="search" class="search"></input> + {% if 'admin' in g.identity.roles %} + <input id="save" type="submit" name="save" value="save query"></input> + {% endif %} + </form> </div> {% endblock %}
--- a/src/ltpdarepo/templates/query/index.html Sat Aug 13 20:27:24 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -{% extends "layout.html" %} -{% block title %} Queries {% endblock %} -{% block body %} -<h2>Queries</h2> -<p class="discrete">Queries for database «{{ database }}»</p> -<ul> -{% for query in queries %} - <li><a href="{{ url_for('manage.queries.view', database=database, query=query) }}">{{ query }}</a></li> -{% endfor %} -</ul> -{% endblock %}
--- a/src/ltpdarepo/templates/query/save.html Sat Aug 13 20:27:24 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -{% import 'forms.html' as forms %} -{% extends "layout.html" %} -{% block title %}Save query{% endblock %} -{% block body %} -<h2>Save query</h2> -{{ forms.render(form) }} -{% endblock %}
--- a/src/ltpdarepo/views/queries.py Sat Aug 13 20:27:24 2011 +0200 +++ b/src/ltpdarepo/views/queries.py Sat Aug 13 20:27:24 2011 +0200 @@ -1,10 +1,13 @@ from flask import Blueprint, render_template, g, abort, redirect, request, json, session, url_for from ltpdarepo.security import require -from ltpdarepo.query import Query +from ltpdarepo.query import Query, IQuery +from ltpdarepo.database import Database app = Blueprint('manage.queries', __name__) +from .browse import MAPPING, RMAPPING + @app.route('/<database>/queries/') @require('admin') @@ -12,7 +15,7 @@ curs = g.db.cursor() curs.execute("""SELECT name FROM queries WHERE db=%s""", database) queries = [row[0] for row in curs.fetchall()] - return render_template('query/index.html', queries=queries, database=database) + return render_template('queries/index.html', queries=queries, database=database) @app.route('/<database>/queries/<query>') @@ -23,6 +26,41 @@ # not found abort(404) return query.querystring + + +@app.route('/<database>/queries/+', methods=['GET', 'POST']) +@require('admin') +def create(database): + db = Database.load(database) + if db is None: + # not found + abort(404) + + if 'name' not in request.values: + from urlparse import parse_qs + parsed = parse_qs(request.values['query'], keep_blank_values=True, strict_parsing=True) + fields = parsed['field'] + ops = parsed['operator'] + values = parsed['value'] + # query json representation + query = json.dumps(zip(fields, ops, values)) + # simple string representation + querystring = ' AND '.join("%s %s '%s'" % (MAPPING.get(f), o, v) for f, o, v in zip(fields, ops, values)) + + form = IQuery() + form.query.data = query + form.database.data = database + form.csrf.data = session['_token'] + return render_template('queries/create.html', form=form, query=querystring, database=db) + + form = IQuery() + if request.method == 'POST' and form.validate(): + query = Query() + form.update(query) + query.create() + return redirect(url_for('.index', database=database)) + return render_template('queries/create.html', form=form, query='') + module = app