changeset 29:d45fa969ebf9

Improve stored queries support.
author Daniele Nicolodi <daniele@grinta.net>
date Mon, 27 Jun 2011 17:14:47 +0200
parents 9e6494e126b3
children 1b37af4b842c
files src/ltpdarepo/__init__.py src/ltpdarepo/query.py src/ltpdarepo/templates/databases/view.html src/ltpdarepo/templates/query/index.html src/ltpdarepo/views/queries.py
diffstat 5 files changed, 75 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/__init__.py	Mon Jun 27 17:14:47 2011 +0200
+++ b/src/ltpdarepo/__init__.py	Mon Jun 27 17:14:47 2011 +0200
@@ -112,6 +112,9 @@
 from .views.databases import module
 app.register_module(module, url_prefix='/manage/databases')
 
+from .views.queries import module
+app.register_module(module, url_prefix='/manage/databases')
+
 from .views.users import module
 app.register_module(module, url_prefix='/manage/users')
 
--- a/src/ltpdarepo/query.py	Mon Jun 27 17:14:47 2011 +0200
+++ b/src/ltpdarepo/query.py	Mon Jun 27 17:14:47 2011 +0200
@@ -1,13 +1,25 @@
 from flask import g
 
+from MySQLdb.cursors import DictCursor
+
+from ltpdarepo import connection
 from ltpdarepo.form import Form
 from wtforms.fields import TextField, HiddenField
-
+from wtforms import validators
+from wtforms.validators import ValidationError
 
 class IQuery(Form):
     querystring = HiddenField()
-    name = TextField("Name")
+    database = HiddenField()
+    name = TextField("Name", validators=[validators.Required()])
 
+    def validate_name(form, field):
+        conn = connection()
+        curs = conn.cursor()
+        query = Query.load(database=form.database.data, name=field.data)
+        if query is not None:
+            raise ValidationError(u"Query with this name already exists.")
+    
 
 class Query(dict):
     def __init__(self, name=None, db=None, querystring=None):
@@ -15,6 +27,20 @@
         self.__dict__ = self
 
     def create(self):
-        curs = g.db.cursor()
+        conn = connection()
+        curs = con.cursor()
         curs.execute("""INSERT INTO queries (name, db, querystring)
                         VALUES (%(name)s, %(db)s, %(querystring)s)""", dict(self))
+
+    @staticmethod
+    def load(database, name):
+        conn = connection()
+        curs = conn.cursor(DictCursor)
+        curs.execute("""SELECT querystring
+                        FROM queries WHERE db=%s and name=%s""", (database, name))
+        query = curs.fetchone()
+        if query is None:
+            return None
+        obj = Query()
+        obj.update(query)
+        return obj
--- a/src/ltpdarepo/templates/databases/view.html	Mon Jun 27 17:14:47 2011 +0200
+++ b/src/ltpdarepo/templates/databases/view.html	Mon Jun 27 17:14:47 2011 +0200
@@ -5,8 +5,9 @@
 <p class="field"><span class="label">Name:</span> {{database.name }}</p>
 <p class="field"><span class="label">Description:</span> {{ database.description }}</p>
 <ul>
-<li><a href="{{ url_for('manage.databases.edit', database=database.id) }}">Edit</a></li>
-<li><a href="{{ url_for('manage.databases.permissions', database=database.id) }}">Permissions</a></li>
-<li><a href="{{ url_for('manage.databases.drop', database=database.id) }}">Drop</a></li>
+  <li><a href="{{ url_for('manage.databases.edit', database=database.id) }}">Edit</a></li>
+  <li><a href="{{ url_for('manage.databases.permissions', database=database.id) }}">Permissions</a></li>
+  <li><a href="{{ url_for('manage.queries.index', database=database.id) }}">Saved queries</a></li>
+  <li><a href="{{ url_for('manage.databases.drop', database=database.id) }}">Drop</a></li>
 </ul>
 {% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ltpdarepo/templates/query/index.html	Mon Jun 27 17:14:47 2011 +0200
@@ -0,0 +1,11 @@
+{% extends "layout.html" %}
+{% block title %} Queries {% endblock %}
+{% block body %}
+<h2>Queries</h2>
+<p class="discrete">Manage queries for database &llaquo;{{ database }}&rlaquo;:</p>
+<ul>
+{% for query in queries %}
+  <li><a href="{{ url_for('manage.queries.view', database=database, query=query) }}">{{ query }}</a></li>
+{% endfor %}
+</ul>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ltpdarepo/views/queries.py	Mon Jun 27 17:14:47 2011 +0200
@@ -0,0 +1,28 @@
+from flask import Module, render_template, g, abort
+
+from ltpdarepo.security import require
+from ltpdarepo.query import Query
+
+app = Module(__name__, 'manage.queries')
+
+
+@app.route('/<database>/queries/')
+@require('admin')
+def index(database):
+    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)
+
+
+@app.route('/<database>/queries/<query>')
+@require('admin')
+def view(database, query):
+    query = Query.load(database=database, name=query)
+    if query is None:
+        # not found
+        abort(404)
+    return query.querystring
+    
+
+module = app