# HG changeset patch # User Daniele Nicolodi # Date 1311632995 -7200 # Node ID 683a99c35a12ebc2c2c1d4330850ea8b48cc7bae # Parent b9c38c1704bd4986aa2326e37048cfa7a787ddb5 Refactor objects listing and implement objects table sorting. diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/__init__.py --- a/src/ltpdarepo/__init__.py Tue Jul 26 00:26:49 2011 +0200 +++ b/src/ltpdarepo/__init__.py Tue Jul 26 00:29:55 2011 +0200 @@ -74,6 +74,18 @@ app.jinja_env.globals['url_for_other_page'] = url_for_other_page +def url_for_other_order(field): + current = request.args.get('o', 'id') + reverse = int(request.args.get('r', 0)) + if current == field: + reverse = int(not reverse) + args = request.view_args.copy() + args.update(request.args) + args.update(o=field, r=int(reverse)) + return url_for(request.endpoint, **args) +app.jinja_env.globals['url_for_other_order'] = url_for_other_order + + @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/static/style.css --- a/src/ltpdarepo/static/style.css Tue Jul 26 00:26:49 2011 +0200 +++ b/src/ltpdarepo/static/style.css Tue Jul 26 00:29:55 2011 +0200 @@ -10,6 +10,10 @@ display: none; } +.small { + font-size: 90%; +} + div.left { float: left; } @@ -272,7 +276,7 @@ } td.id, td.name { - font-family: monospace; + font-family: andale mono, monospace; } td.name, td.title, td.description { diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/templates/browse.html --- a/src/ltpdarepo/templates/browse.html Tue Jul 26 00:26:49 2011 +0200 +++ b/src/ltpdarepo/templates/browse.html Tue Jul 26 00:29:55 2011 +0200 @@ -1,52 +1,1 @@ -{% extends "layout.html" %} -{% block title %}{{ database.id }}{% endblock %} -{# block head %} - - -{% endblock #} -{% block body %} -

Database «{{ database.id }}»

-

{{ database.description }}

-{% if not objs %} -

-{% else %} -

{{ batch.count }} objects

- - - - {% for field in fields %} - - {% endfor %} - - - - {% for obj in objs %} - - {% for field in fields %} - {% if field == 'name' %} - - {% else %} - - {% endif %} - {% endfor %} - - - - - {% endfor %} - -
{{ field }}
{{ obj[field] }}{{ obj[field]|string|truncate(60, False, '…') }}
details
-{% import "pagination.html" as pagination %} -{% if pagination is defined %} -{{ pagination.render(batch) }} -{% endif %} -{% endif %} -{% endblock %} +{% extends "objs.html" %} diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/templates/objs.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/templates/objs.html Tue Jul 26 00:29:55 2011 +0200 @@ -0,0 +1,53 @@ +{% import "pagination.html" as pagination %} +{% extends "layout.html" %} +{% block title %}{{ database.id }}{% endblock %} +{% block body %} +

Database «{{ database.id }}»

+

{{ database.description|default(' '|safe, true) }}

+ +{% block above %}{% endblock %} + +

{{ batch.count }} objects

+{% if pagination is defined %} + {{ pagination.render(batch) }} +{% endif %} + + + + {% for field in fields %} + + {% endfor %} + + + + {% for obj in objs %} + + {% for field in fields %} + {% if field == 'name' %} + + {% else %} + + {% endif %} + {% endfor %} + + + + + {% endfor %} + +
+ + {% if field == request.args.get('o', 'id') %} + {{ field }} + {% else %} + {{ field }} + {% endif %} + +
{{ obj[field] }}{{ obj[field]|string|truncate(60, False, '…') }}
details
+{% if pagination is defined %} + {{ pagination.render(batch) }} +{% endif %} + +{% block below %}{% endblock %} + +{% endblock %} diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/templates/query.html --- a/src/ltpdarepo/templates/query.html Tue Jul 26 00:26:49 2011 +0200 +++ b/src/ltpdarepo/templates/query.html Tue Jul 26 00:29:55 2011 +0200 @@ -1,5 +1,5 @@ -{% extends "layout.html" %} -{% block title %}{{ database.id }}{% endblock %} +{% extends "objs.html" %} + {% block head %} @@ -7,10 +7,9 @@ var criteria = {{ criteria|tojson|safe }}; {% endblock %} -{% block body %} -

Database «{{ database.id }}»

-

{{ database.description|default('—'|safe, true) }}

-
+ +{% block above %} +
{% for field, op, value in query %} @@ -54,43 +53,14 @@
- +
{% if 'admin' in g.identity.roles %}
- +
{% endif %}
- -{% if objs %} -

{{ batch.count }} objects

- - - - {% for field in fields %} - - {% endfor %} - - - - {% for obj in objs %} - - {% for field in fields %} - {% if field == 'name' %} - - {% else %} - - {% endif %} - {% endfor %} - - - - - {% endfor %} - -
{{ field }}
{{ obj[field] }}{{ obj[field]|string|truncate(60, False, '…') }}
details
-{% endif %} {% endblock %} diff -r b9c38c1704bd -r 683a99c35a12 src/ltpdarepo/views/browse.py --- a/src/ltpdarepo/views/browse.py Tue Jul 26 00:26:49 2011 +0200 +++ b/src/ltpdarepo/views/browse.py Tue Jul 26 00:29:55 2011 +0200 @@ -36,7 +36,7 @@ ALLFIELDS = FIELDS + EXTRA -MAPPING = { 'id': 'obj_id', +RMAPPING = { 'id': 'obj_id', 'name': 'name', 'type': 'obj_type', 'quantity': 'quantity', @@ -58,7 +58,7 @@ # 'vdate': '', } -MAPPING = dict(((v, k) for k, v in MAPPING.iteritems())) +MAPPING = dict(((v, k) for k, v in RMAPPING.iteritems())) def _current_page(): @@ -66,10 +66,19 @@ return int(request.args.get('p', 1)) +def _current_ordering(): + """return the current page in a paginated view""" + order = request.args.get('o', 'id') + order = RMAPPING[order] + reverse = int(request.args.get('r', 0)) + return (order, reverse) + + class Objs(object): def __init__(self, database): self._database = database self._orderby = None + self._reverse = None self._limit = None self._where = None self._values = None @@ -82,6 +91,8 @@ query += " WHERE %s" % self._where if self._orderby: query += " ORDER BY `%s`" % self._orderby + if self._reverse: + query += " DESC" if self._limit: query += " LIMIT %d,%d" % self._limit return query @@ -103,7 +114,10 @@ return self def orderby(self, order): - self._orderby = order + if isinstance(order, tuple): + self._orderby, self._reverse = order + else: + self._orderby = order return self def __getitem__(self, item): @@ -157,8 +171,8 @@ abort(404) count = Objs(database=database).count() - batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).orderby('obj_id')[batch.slice] + batch = Pagination(_current_page(), size=PAGESIZE, count=count) + objs = Objs(database=database).orderby(_current_ordering())[batch.slice] return render_template('browse.html', objs=objs, fields=FIELDS, database=db, batch=batch) @@ -263,7 +277,7 @@ # query count = Objs(database=database).filter('name LIKE %s', q).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).filter('name LIKE %s', q).orderby('obj_id')[batch.slice] + objs = Objs(database=database).filter('name LIKE %s', q).orderby(_current_ordering())[batch.slice] return render_template('browse.html', objs=objs, fields=FIELDS, database=db, batch=batch) @@ -316,7 +330,7 @@ if name is not None: curs = g.db.cursor() - curs.execute("""SELECT querystring FROM queries WHERE db=%s AND name=%s""", + curs.execute("SELECT querystring FROM queries WHERE db=%s AND name=%s", (database, name)) query = json.loads(curs.fetchone()[0]) fields, ops, values = [], [], [] @@ -342,7 +356,7 @@ # get objects count = Objs(database=database).filter(where, values).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).filter(where, values).orderby('obj_id')[batch.slice] + objs = Objs(database=database).filter(where, values).orderby(_current_ordering())[batch.slice] # collect search critaeria criteria = _indexes(database, 'objmeta')