changeset 9:8e1c24d9067d

Fix object display. Implement xml and binary representations download.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 15 Jun 2011 17:48:42 +0200
parents d6bedd937d10
children ec561d030fc2
files src/ltpdarepo/static/style.css src/ltpdarepo/templates/obj.html src/ltpdarepo/views/browse.py
diffstat 3 files changed, 66 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/static/style.css	Wed Jun 15 15:56:33 2011 +0200
+++ b/src/ltpdarepo/static/style.css	Wed Jun 15 17:48:42 2011 +0200
@@ -493,3 +493,19 @@
 #save-search-criteria {
     float: right;
 }
+
+/* object display */
+
+table.obj th {
+    text-align: right;
+    border: none;
+}
+
+table.obj td {
+    text-align: left;
+    border: none;
+}
+
+table.obj tr:hover td {
+    border: none;
+}
--- a/src/ltpdarepo/templates/obj.html	Wed Jun 15 15:56:33 2011 +0200
+++ b/src/ltpdarepo/templates/obj.html	Wed Jun 15 17:48:42 2011 +0200
@@ -2,7 +2,11 @@
 {% block title %}{{ dbname }}{% endblock %}
 {% block body %}
 <h2>{{ database.id }} - {{ obj.name }}</h2>
-{% for name, value in obj.iteritems() %}
-  <p>{{ name }}: {{ value }}</p>
-{% endfor %}
+<table class="listing obj">
+  {% for field in fields %}
+  <tr><th>{{ field }}:</th><td>{{ obj[field] }}</td></tr>
+  {% endfor %}
+  <tr><th>xml:</th><td><a href="{{ url_for('browse.download', frmt='xml', database=database.id, objid=obj.id) }}">download</a></td></tr>
+  <tr><th>binary:</th><td><a href="{{ url_for('browse.download', frmt='binary', database=database.id, objid=obj.id) }}">download</a></td></tr>
+</table>
 {% endblock %}
--- a/src/ltpdarepo/views/browse.py	Wed Jun 15 15:56:33 2011 +0200
+++ b/src/ltpdarepo/views/browse.py	Wed Jun 15 17:48:42 2011 +0200
@@ -2,7 +2,7 @@
 from textwrap import dedent
 from operator import itemgetter
 
-from flask import Module, abort, g, request, render_template, redirect, current_app, url_for, json
+from flask import Module, abort, g, request, render_template, redirect, current_app, url_for, json, make_response
 from MySQLdb.cursors import DictCursor
 
 from ltpdarepo.security import require, view
@@ -167,12 +167,50 @@
         if db is None:
             # not found
             abort(404)
+        objs = Objs(database=database, where='obj_id=%s')
         curs = g.db.cursor(DictCursor)
-        curs.execute('''
-        SELECT obj_id AS id, name, obj_type AS type, experiment_title, experiment_desc, analysis_desc,
-        quantity, keywords, submitted, created FROM `%s`.objmeta WHERE obj_id = %%s''' % database, objid)
+        curs.execute(objs.query, objid)
         obj = curs.fetchone()
-        return render_template('obj.html', obj=obj, database=db)
+        return render_template('obj.html', database=db, obj=obj, fields=ALLFIELDS)
+
+
+@app.route('/<database>/<int:objid>/<frmt>')
+@require('user')
+def download(database, objid, frmt):
+    with view('database', database):
+        db = Database().load(database)
+        if db is None:
+            # not found
+            abort(404)
+            
+        if frmt == 'xml':
+            curs = g.db.cursor()
+            n = curs.execute("""SELECT xml FROM `%s`.objs WHERE id=%%s""" % database, objid)
+            if n != 1:
+                # not found
+                abort(404)
+            data = curs.fetchone()[0] or ''
+            response = make_response(data)
+            response.mimetype = 'text/xml'
+            filename = '%s-%s.%s' % (database, objid, 'xml')
+            response.headers.add('Content-Disposition' , 'attachment', filename=filename)
+            return response
+        
+        if frmt == 'binary':
+            curs = g.db.cursor()
+            n = curs.execute("""SELECT mat FROM `%s`.bobjs WHERE obj_id=%%s""" % database, objid)
+            if n != 1:
+                # not found
+                abort(404)
+            data = curs.fetchone()[0] or ''
+            response = make_response(data)
+            response.mimetype = 'application/matlab'
+            filename = '%s-%s.%s' % (database, objid, 'mat')
+            response.headers.add('Content-Disposition' , 'attachment', filename=filename)
+            return response
+
+        # unauthorized
+        abort(403)
 
 
 @app.route('/<database>/search')