# HG changeset patch # User Daniele Nicolodi # Date 1309182738 -7200 # Node ID f27d3de115682decaea6d51b9602aac032595144 # Parent 19f233ab545fe912b2c4ca109693fc8c0283bec4 Rework objects download. diff -r 19f233ab545f -r f27d3de11568 src/ltpdarepo/templates/obj.html --- 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 @@ {{ field }}:{{ obj[field] }} {% endfor %} xml:download - binary:download + binary:download {% endblock %} diff -r 19f233ab545f -r f27d3de11568 src/ltpdarepo/views/browse.py --- 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('//search') @require('user')