# HG changeset patch # User Daniele Nicolodi # Date 1308134986 -7200 # Node ID ceb5df800c512199d286954ca7a0b852d0a960fa # Parent 0f59922d36d2a4be6ef366575df6138c815a01d8 Add advanced query and query parameters save functionality. This require a new 2.5 to 2.6 database schema version upgrade to add a table where to store persistent query parameters. diff -r 0f59922d36d2 -r ceb5df800c51 src/ltpdarepo/__init__.py --- a/src/ltpdarepo/__init__.py Wed Jun 15 11:07:20 2011 +0200 +++ b/src/ltpdarepo/__init__.py Wed Jun 15 12:49:46 2011 +0200 @@ -5,7 +5,7 @@ from .security import secure, require, authenticate -SCHEMA = 2.5 +SCHEMA = 2.6 app = Flask(__name__) diff -r 0f59922d36d2 -r ceb5df800c51 src/ltpdarepo/query.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/query.py Wed Jun 15 12:49:46 2011 +0200 @@ -0,0 +1,20 @@ +from flask import g + +from ltpdarepo.form import Form +from wtforms.fields import TextField, HiddenField + + +class IQuery(Form): + querystring = HiddenField() + name = TextField("Name") + + +class Query(dict): + def __init__(self, name=None, db=None, querystring=None): + super(Query, self).__init__(name=name, db=db, querystring=querystring) + 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)) diff -r 0f59922d36d2 -r ceb5df800c51 src/ltpdarepo/static/querywidget.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/static/querywidget.js Wed Jun 15 12:49:46 2011 +0200 @@ -0,0 +1,119 @@ +;(function($) { + + // namespace + if (typeof($.querywidget) == "undefined") { + $.querywidget = { + config: {}, + initialized: false + }; + } + + // create a select menu + $.querywidget.createSelect = function (values, selectedvalue, classname, name) { + // create select + var select = $(document.createElement('select')).addClass(classname).attr('name', name); + $.each(values, function (i, val) { + var id = val; + var name = val; + if (typeof(val.name) !== 'undefined') { + id = val.val; + name = val.name; + } + // create options + var option = $(document.createElement('option')).attr('value', id).html(name); + if (i == selectedvalue) { + option.attr('selected', 'selected'); + } + select.append(option); + }); + return select; + }; + + // create label + $.querywidget.createQueryLabel = function (index) { + var name = $.querywidget.config.indexes[index].name; + return $(document.createElement('span')).html(name).addClass('querywidget field'); + }; + + // create field name + $.querywidget.createQueryField = function (index) { + var value = $.querywidget.config.indexes[index].val; + return $(document.createElement('input')).attr({ + 'type': 'hidden', + 'name': 'field', + 'value': value}) + }; + + // create operator select menu + $.querywidget.createQueryOperator = function (index, value) { + return $.querywidget.createSelect($.querywidget.config.indexes[index].operators, + value, + 'querywidget operator', + 'operator'); + }; + + // create value field + $.querywidget.createWidget = function (index) { + var type = $.querywidget.config.indexes[index].type; + switch (type) { + case 'enum': + return $.querywidget.createSelect($.querywidget.config.indexes[index].values, '', 'value', 'value'); + break; + default: + return $(document.createElement('input')).attr({ + 'autocomplete': 'off', + 'type': 'text', + 'name': 'value' + }).addClass('querywidget value'); + break; + } + }; + + // init + $(document).ready(function () { + $.querywidget.init(); + }); + + // init widget + $.querywidget.init = function () { + + // check if already initialized + if ($.querywidget.initialized == true) { + // return nothing done + return false; + } + + // set initialized + $.querywidget.initialized = true; + + // configuration + $.querywidget.config.indexes = criteria; + + $('#add').live('change', function () { + var index = $(this).find(':selected')[0].value; + var newcriteria = $(document.createElement('div')).addClass('criteria'); + newcriteria.append($.querywidget.createQueryLabel(index)); + newcriteria.append($.querywidget.createQueryField(index)); + newcriteria.append($.querywidget.createQueryOperator(index,'')); + newcriteria.append($.querywidget.createWidget(index)); + newcriteria.append( + $(document.createElement('input')) + .attr({ + 'value': '\u00D7', + 'type': 'button', + 'name': 'remove'}) + .addClass('querywidget remove') + ); + $('#criteria').append(newcriteria); + $(this).val(''); + }); + + $('.remove').live('click', function () { + $(this).parents('.criteria').remove(); + return false; + }); + + }; + + })(jQuery); + diff -r 0f59922d36d2 -r ceb5df800c51 src/ltpdarepo/static/style.css --- a/src/ltpdarepo/static/style.css Wed Jun 15 11:07:20 2011 +0200 +++ b/src/ltpdarepo/static/style.css Wed Jun 15 12:49:46 2011 +0200 @@ -6,6 +6,10 @@ padding: 0; } +.hidden { + display: none; +} + div.left { float: left; } @@ -237,7 +241,7 @@ margin: 0.3em; } -input[type=submit]:hover, #submit:hover { +input[type=submit]:hover, input[type=button]:hover, #submit:hover { background: #CFF09E; } @@ -362,8 +366,8 @@ .search { margin: 1.2em 0; + float: left; border: 4px solid #EEE; - float: left; -moz-border-radius: 4px; } @@ -411,6 +415,81 @@ /* actions */ ul.actions li { - margin-left: 2em; - line-height: 1.6em; + line-height: 1.6em; +} + +ul.actions li a { + text-decoration: none; +} + +ul.actions li a:hover { + border-bottom: 1px solid; +} + +/* query builder */ + +div.criteria { + margin: 3px; + border: 4px solid #EEE; + -moz-border-radius: 4px; + float: left; + clear: both; + height: 27px; +} + +div.criteria > * { + border: 1px solid #BFBFBF; + margin: 0; + float: left; + line-height: 1.2em; + font-size: 90%; +} + +div.criteria .field { + padding: 5px 10px; + width: 12em; + border-right: none; + line-height: 1.3em; } + +div.criteria .operator { + width: 5em; + border-right: none; +} + +div.criteria select { + padding: 4px; +} + +div.criteria .value { + width: 24em; + padding: 5px 10px; + border-right: none; + line-height: 13px !important; + vertical-align: bottom; +} + +div.criteria select.value { + padding: 4px; +} + +div.criteria .remove { + padding: 4px 5px; +} + +div.query { + margin: 0.5em; +} + +div.criteria select, div.criteria input { + font-size: 90%; +} + +select, input { + font-size: 100%; + margin: 3px; +} + +#save-search-criteria { + float: right; +} diff -r 0f59922d36d2 -r ceb5df800c51 src/ltpdarepo/templates/database.html --- a/src/ltpdarepo/templates/database.html Wed Jun 15 11:07:20 2011 +0200 +++ b/src/ltpdarepo/templates/database.html Wed Jun 15 12:49:46 2011 +0200 @@ -6,14 +6,24 @@

Search database «{{ database.id }}»

Search objects by name

-