def to_request_data(self, field, data): """ We use the url factory to get an identifier for the file which we use as the name. We also store it in the 'default' field so we can check if something has been uploaded (the identifier doesn't match the name) """ mimetype = '' if isinstance(data, SchemaFile): default = util.encode_file_resource_path(None, self.url_ident_factory(data)) mimetype = data.mimetype elif data is not None: default = util.encode_file_resource_path(None, data) else: default = '' return {'name': [default], 'default':[default], 'mimetype':[mimetype]}
def pre_parse_incoming_request_data(self, field, data): """ File uploads are wierd; in out case this means assymetric. We store the file in a temporary location and just store an identifier in the field. This at least makes the file look symmetric. """ if data is None: data = {} if data.get('remove', [None])[0] is not None: data['name'] = [''] data['mimetype'] = [''] return data fieldstorage = data.get('file', [''])[0] if getattr(fieldstorage,'file',None): # XXX Can we reuse the key from a previous temp upload to avoid # creating an additional temp file? key = uuid.uuid4().hex cache_tag = uuid.uuid4().hex self.filestore.put(key, fieldstorage.file, cache_tag, [('Content-Type', fieldstorage.type), ('Filename', fieldstorage.filename)]) data['name'] = [util.encode_file_resource_path('tmp', key)] data['mimetype'] = [fieldstorage.type] return data
def pre_parse_incoming_request_data(self, field, data): """ File uploads are wierd; in out case this means assymetric. We store the file in a temporary location and just store an identifier in the field. This at least makes the file look symmetric. """ if data is None: data = {} if data.get('remove', [None])[0] is not None: data['name'] = [''] data['mimetype'] = [''] return data fieldstorage = data.get('file', [''])[0] if getattr(fieldstorage, 'file', None): # XXX Can we reuse the key from a previous temp upload to avoid # creating an additional temp file? key = uuid.uuid4().hex cache_tag = uuid.uuid4().hex self.filestore.put(key, fieldstorage.file, cache_tag, [('Content-Type', fieldstorage.type), ('Filename', fieldstorage.filename)]) data['name'] = [util.encode_file_resource_path('tmp', key)] data['mimetype'] = [fieldstorage.type] return data
def test_file_resource_path(self): tests = [ (None, 'foo', 'foo'), (None, '@foo', '@@foo'), (None, 'f@o', 'f@o'), ('bar', 'foo', '@bar/foo'), ('bar', '@foo', '@bar/@foo'), ('bar', 'f@o', '@bar/f@o'), ] for (name, key, path) in tests: encoded = util.encode_file_resource_path(name, key) self.assertTrue(encoded == path) self.assertTrue(util.decode_file_resource_path(encoded) == (name, key))
def to_request_data(self, field, data): """ We use the url factory to get an identifier for the file which we use as the name. We also store it in the 'default' field so we can check if something has been uploaded (the identifier doesn't match the name) """ filename = '' mimetype = '' if isinstance(data, SchemaFile): default = util.encode_file_resource_path( None, self.url_ident_factory(data)) mimetype = data.mimetype filename = data.filename elif data is not None: default = util.encode_file_resource_path(None, data) else: default = '' return { 'name': [default], 'default': [default], 'mimetype': [mimetype], 'filename': [filename] }
def test_file_resource_path(self): tests = [ (None, 'foo', 'foo'), (None, '@foo', '@@foo'), (None, 'f@o', 'f@o'), ('bar', 'foo', '@bar/foo'), ('bar', '@foo', '@bar/@foo'), ('bar', 'f@o', '@bar/f@o'), ] for (name, key, path) in tests: encoded = util.encode_file_resource_path(name, key) self.assertTrue(encoded == path) self.assertTrue( util.decode_file_resource_path(encoded) == (name, key))