# HG changeset patch # User Daniele Nicolodi # Date 1313260044 -7200 # Node ID 2850a914aef9a81841be79ecf37f87064d694629 # Parent bce8c427a5c5e87323b21dd8a00222fc4e69d684 Add back the support for saved queries. Still incomplete and experimental. diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/query.py --- 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 diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/namedquery.html --- /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 %} +
+
{{ query }}
+
+{% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/queries/create.html --- /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 %} +

Query for database «{{ database.id }}»

+{#

{{ database.description }}

#} +
{{ query }}
+{{ forms.render(form) }} +{% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/queries/index.html --- /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 %} +

Queries

+

Queries for database «{{ database }}»

+ +{% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/query.html --- 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(); + }); }); {% endblock %} @@ -58,6 +71,10 @@ + {% if 'admin' in g.identity.roles %} + + {% endif %} + {% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/query/index.html --- 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 %} -

Queries

-

Queries for database «{{ database }}»

- -{% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/templates/query/save.html --- 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 %} -

Save query

-{{ forms.render(form) }} -{% endblock %} diff -r bce8c427a5c5 -r 2850a914aef9 src/ltpdarepo/views/queries.py --- 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('//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('//queries/') @@ -23,6 +26,41 @@ # not found abort(404) return query.querystring + + +@app.route('//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