From de5cf4dc6a9b4669414920f0e2f49908afbe7e7b Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Wed, 28 Mar 2012 21:38:06 -0500 Subject: [PATCH] custom_retrieve_field_properties, thanks Guyris, issue 710 --- VERSION | 2 +- gluon/dal.py | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/VERSION b/VERSION index 558fa9f4..bc31a9e3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 1.99.7 (2012-03-28 21:01:13) dev +Version 1.99.7 (2012-03-28 21:37:42) dev diff --git a/gluon/dal.py b/gluon/dal.py index 4e32156b..60c52f65 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -7517,6 +7517,7 @@ class Field(Expression): compute=None, custom_store=None, custom_retrieve=None, + custom_retrieve_fileproperties=None, custom_delete=None, ): self.db = None @@ -7562,6 +7563,7 @@ class Field(Expression): self.isattachment = True self.custom_store = custom_store self.custom_retrieve = custom_retrieve + self.custom_retrieve_fileproperties = custom_retrieve_fileproperties self.custom_delete = custom_delete if self.label is None: self.label = fieldname.replace('_',' ').title() @@ -7624,14 +7626,11 @@ class Field(Expression): raise http.HTTP(404) if self.authorize and not self.authorize(row): raise http.HTTP(403) - try: - m = regex_content.match(name) - if not m or not self.isattachment: - raise TypeError, 'Can\'t retrieve %s' % name - filename = base64.b16decode(m.group('name'), True) - filename = regex_cleanup_fn.sub('_', filename) - except (TypeError, AttributeError): - filename = name + m = regex_content.match(name) + if not m or not self.isattachment: + raise TypeError, 'Can\'t retrieve %s' % name + fileproperties = self.retrieve_fileproperties(name,path) + filename = fileproperties['filename'] if isinstance(self.uploadfield, str): # ## if file is in DB return (filename, cStringIO.StringIO(row[self.uploadfield] or '')) elif isinstance(self.uploadfield,Field): @@ -7639,6 +7638,25 @@ class Field(Expression): query = self.uploadfield == name data = self.uploadfield.table(query)[blob_uploadfield_name] return (filename, cStringIO.StringIO(data)) + else: + # ## if file is on filesystem + return (filename, open(os.path.join(fileproperties['path'], name), 'rb')) + + def retrieve_fileproperties(self, name, path=None): + if self.custom_retrieve_fileproperties: + return self.custom_retrieve_fileproperties(name, path) + try: + m = regex_content.match(name) + if not m or not self.isattachment: + raise TypeError, 'Can\'t retrieve %s fileproperties' % name + filename = base64.b16decode(m.group('name'), True) + filename = regex_cleanup_fn.sub('_', filename) + except (TypeError, AttributeError): + filename = name + if isinstance(self.uploadfield, str): # ## if file is in DB + return dict(path=None,filename=filename) + elif isinstance(self.uploadfield,Field): + return dict(path=None,filename=filename) else: # ## if file is on filesystem if path: @@ -7652,7 +7670,8 @@ class Field(Expression): f = m.group('field') u = m.group('uuidkey') path = os.path.join(path,"%s.%s" % (t,f),u[:2]) - return (filename, open(os.path.join(path, name), 'rb')) + return dict(path=path,filename=filename) + def formatter(self, value): if value is None or not self.requires: