# HG changeset patch # User Daniele Nicolodi # Date 1309187687 -7200 # Node ID d45fa969ebf92a255275260f49aaa5a90fa3e433 # Parent 9e6494e126b3f053e255a9b643717a00b868ac27 Improve stored queries support. diff -r 9e6494e126b3 -r d45fa969ebf9 src/ltpdarepo/__init__.py --- 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') diff -r 9e6494e126b3 -r d45fa969ebf9 src/ltpdarepo/query.py --- 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 diff -r 9e6494e126b3 -r d45fa969ebf9 src/ltpdarepo/templates/databases/view.html --- 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 @@

Name: {{database.name }}

Description: {{ database.description }}

{% endblock %} diff -r 9e6494e126b3 -r d45fa969ebf9 src/ltpdarepo/templates/query/index.html --- /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 %} +

Queries

+

Manage queries for database &llaquo;{{ database }}&rlaquo;:

+ +{% endblock %} diff -r 9e6494e126b3 -r d45fa969ebf9 src/ltpdarepo/views/queries.py --- /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('//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('//queries/') +@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