changeset 22:f27d3de11568

Rework objects download.
author Daniele Nicolodi <daniele@grinta.net>
date Mon, 27 Jun 2011 15:52:18 +0200
parents 19f233ab545f
children 05ab4b93f132
files src/ltpdarepo/templates/obj.html src/ltpdarepo/views/browse.py
diffstat 2 files changed, 23 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/templates/obj.html	Mon Jun 27 14:13:37 2011 +0200
+++ b/src/ltpdarepo/templates/obj.html	Mon Jun 27 15:52:18 2011 +0200
@@ -7,6 +7,6 @@
   <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>
+  <tr><th>binary:</th><td><a href="{{ url_for('browse.download', frmt='mat', database=database.id, objid=obj.id) }}">download</a></td></tr>
 </table>
 {% endblock %}
--- a/src/ltpdarepo/views/browse.py	Mon Jun 27 14:13:37 2011 +0200
+++ b/src/ltpdarepo/views/browse.py	Mon Jun 27 15:52:18 2011 +0200
@@ -182,36 +182,32 @@
         if db is None:
             # not found
             abort(404)
-            
+
+        if frmt not in ('xml', 'mat'):
+            # 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.execute("""SELECT xml FROM `%s`.objs WHERE id=%%s""" % database, objid)
+            mimetype = 'text/xml'
+        if frmt == 'mat':
             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
+            curs.execute("""SELECT mat FROM `%s`.bobjs WHERE obj_id=%%s""" % database, objid)
+            mimetype = 'application/matlab'
+
+        data = curs.fetchone()[0]
+        if data is None:
+            # not found
+            abort(404)
 
-        # unauthorized
-        abort(403)
-
+        # construct response
+        response = make_response(data)
+        response.mimetype = mimetype
+        filename = '%s-%s.%s' % (database, objid, frmt)
+        response.headers.add('Content-Disposition' , 'attachment', filename=filename)
+        return response
+        
 
 @app.route('/<database>/search')
 @require('user')