def test_remove_version(self): myfile = StringIO('hello data!') myfile.filename = 'my.txt' self.add_bfile(id='mybfile', title='My bfile', uploaded_file=myfile) mybfile = self.portal.myfolder.mybfile myfile2 = StringIO('new data') myfile2.filename = 'other.txt' mybfile._save_file(myfile2, contributor='contributor') mybfile.remove_version(1) rm_ver = mybfile._versions[1] self.assertEqual(rm_ver.open().read(), '') self.assertEqual(rm_ver.size, None) self.assertEqual(rm_ver.removed, True) self.assertTrue(mybfile.current_version is mybfile._versions[0]) myfile3 = StringIO('even newer data') myfile3.filename = 'other.txt' mybfile._save_file(myfile3, contributor='contributor') self.assertTrue(mybfile.current_version is mybfile._versions[2]) mybfile.remove_version(2) self.assertTrue(mybfile.current_version is mybfile._versions[0]) mybfile.remove_version(0) self.assertTrue(mybfile.current_version is None)
def transform(self, env, source, params): self.log.debug('<map:transform src="%s"> process()' % source.uri) if "use-request-parameters" in params: self.useRequestParameters = (params["use-request-parameters"] == "true") data = source.read() if isinstance(data, str): fd = StringIO(data) if hasattr(source, "filename"): fd.filename = source.filename else: fd.filename = source.uri xslt = etree.parse(fd) #xslt = etree.fromstring(data) else: xslt = data transform = etree.XSLT(xslt) doc = env.response.body if self.useRequestParameters: rparams = env.objectModel["request"].params encoding = env.objectModel["request"].formEncoding if encoding is not None: params.update(dict([(k, v.decode(encoding)) for k, v in rparams.items()])) else: params.update(rparams) params = dict([(k, '"%s"' % v) for k, v in params.items()]) self.log.debug("Stylesheet parameters:\n%s" % "\n".join([" %s: %s" % (k, v) for k, v in params.items()])) env.response.body = transform(doc, **params).getroot()
def versions(self): """ Exports NyExFile versions """ languages = self.data.get('_languages', ()) if len(languages) > 1: self.logger.debug("\t Object %s has %r languages: %r", self.data.get('id', 'ERROR'), len(languages), languages) if not languages: languages = [x['id'] for x in self.data['_objects']] for language in languages: nyfile = self.data.pop(language, None) if not nyfile: continue extfile = getattr(nyfile, '_ext_file', None) if not extfile: continue versions = getattr(nyfile, 'versions', []) if versions: versions = versions.objectValues() for version in versions: filename = '/'.join(version.filename) if not filename: continue if version.is_broken(): self.logger.warn('\t BROKEN VERSION: %s (%s)', version.getId(), filename) continue sfile = StringIO(version.data) self.logger.debug('\t VERSION FILENAME: %s', filename) sfile.filename = version.filename[-1] sfile.headers = {'content-type': version.content_type} sfile.releasedate = self.data['releasedate'] yield sfile filename = '/'.join(extfile.filename) if not filename: continue if extfile.is_broken(): self.logger.warn('\t BROKEN EXTFILE: %s (%s)', language, filename) continue sfile = StringIO(extfile.data) self.logger.debug('\t FILENAME: %s', filename) sfile.filename = extfile.filename[-1] sfile.headers = {'content-type': extfile.content_type} sfile.releasedate = self.data['releasedate'] yield sfile
def test_file_creation_1(self): """Same file can be added multiple times when title is provided""" portal = self.layer['portal'] request = self.layer['request'] tp = portal.table_page folder = portal.folder tp.manage_setLocalRoles('user0', ('Contributor',)) with open(__file__) as f: file = StringIO(f.read()) file.filename = os.path.basename(__file__) request.form['att'] = file request.form['title_att'] = "A python file" request.form['form.submitted'] = "1" view = getMultiAdapter((tp, request), name='edit-record') view() self.assertTrue('a-python-file' in folder.objectIds()) file.seek(0) request.form['att'] = file request.form['title_att'] = "A python file" request.form['form.submitted'] = "1" view() self.assertTrue('a-python-file-1' in folder.objectIds()) file.seek(0) request.form['att'] = file request.form['title_att'] = "A python file" request.form['form.submitted'] = "1" request.form['title_att'] = "Another python file" view() self.assertTrue('another-python-file' in folder.objectIds())
def test_required(self): portal = self.layer['portal'] request = self.layer['request'] tp = portal.table_page configuration = tp.getPageColumns() configuration[0]['options'] = ['required'] tp.setPageColumns(configuration) # no file provided request.form['form.submitted'] = "1" view = getMultiAdapter((tp, request), name='edit-record') self.assertTrue('The field "Attachment" is required' in view()) # existing file provided request.form['form.submitted'] = "1" request.form['existing_att'] = portal.folder.attachment.UID() view = getMultiAdapter((tp, request), name='edit-record') view() self.assertEqual(request.response.status, 302) # new file provided del request.form['existing_att'] with open(__file__) as f: file = StringIO(f.read()) filename = os.path.basename(__file__) file.filename = filename request.form['form.submitted'] = "1" request.form['att'] = file view = getMultiAdapter((tp, request), name='edit-record') view() self.assertEqual(request.response.status, 302)
def test_file_creation_2(self): """When title is not provided, file id is used and we can't add multiple files (same as Plone)""" portal = self.layer['portal'] request = self.layer['request'] tp = portal.table_page folder = portal.folder tp.manage_setLocalRoles('user0', ('Contributor',)) with open(__file__) as f: file = StringIO(f.read()) filename = os.path.basename(__file__) file.filename = filename request.form['att'] = file request.form['form.submitted'] = "1" view = getMultiAdapter((tp, request), name='edit-record') view() file_content = folder[filename] self.assertTrue(file_content.getId() in folder.objectIds()) self.assertEqual(len(folder.objectIds()), 2) file.seek(0) request.form['att'] = file request.form['form.submitted'] = "1" view() # we still have two files self.assertEqual(len(folder.objectIds()), 2) storage = IDataStorage(tp) self.assertEqual(storage[0]['att'], storage[1]['att'])
def migrate(self, field): """ Migrate OFS.Image to blob """ storage = field.getStorage() try: zfile = storage.get(self.field, self.context) except AttributeError: logger.info('\t There is no %s to migrate', self.field) return False if not isinstance(zfile, self.ztype): logger.info('\t %s is already a Blob', self.field) return False data = StringIO(zfile.data) filename = getattr(zfile, 'filename', self.context.getId()) if isinstance(filename, unicode): filename = filename.encode('utf-8') data.filename = filename ctype = getattr(zfile, 'content_type', None) if ctype: if isinstance(ctype, unicode): ctype = ctype.encode('utf-8') data.content_type = ctype field.getMutator(self.context)(data) logger.info('\t %s is now a Blob', self.field) return True
def parse_images(images): """Get a list of cStringIO files from images """ to_return = [] for image in images: mimetype = image.attrib['mimetype'] filename = image.attrib.get('filename', gen_default_filename(mimetype)) data = image.find('data') content_length = data.attrib['binlength'] or '0' content_length = int(content_length) bin_data = base64.urlsafe_b64decode(data.text) img = StringIO(bin_data) img.mimetype = mimetype img.filename = filename img.content_length = content_length to_return.append(img) return to_return
def preparefile(rel_filename, ctype='text/xml'): """ Prepare a file for upload """ ofile = loadfile(rel_filename) fp = StringIO(ofile.get('data')) fp.filename = ofile.get('name') return fp
def load_test_file(filename, globals_): """ Load data from a test file """ home = package_home(globals_) filename = os.path.sep.join([home, filename]) data = StringIO(open(filename, 'rb').read()) data.filename = os.path.basename(filename) return data
def test_view_zip(self): """ Custom view of zipfile """ from zipfile import ZipFile zfile = open(os.path.join(os.path.dirname(__file__), "fixtures", "one_file_zip.zip"), "rb") upload_file = StringIO(zfile.read()) upload_file.filename = "test.zip" upload_file.headers = {"content-type": "application/zip"} namelist = ZipFile(zfile).namelist() zfile.close() addNyBFile( self.portal.myfolder, submitted=1, contributor="contributor", id="test", title="test", uploaded_file=upload_file, ) transaction.commit() # The result is a simple html with the contents of the zipfile (filepaths) self.browser.go("http://localhost/portal/myfolder/test/download/en/1/test.zip?action=view") html_content = self.browser.get_html() for filepath in namelist: self.assertTrue(filepath in html_content) zfile.close()
def add_file(location_obj, name, data): f = StringIO(data) f.filename = name if '.' in name: name = name.rsplit('.', 1)[0] return addNyBFile(location_obj, id=name, uploaded_file=f, _send_notifications=False)
def preparefile(rel_filename, ctype='application/pdf'): """ Prepare a file for upload """ ofile = loadfile(rel_filename) fp = StringIO(ofile.get('data')) fp.filename = ofile.get('name') return fp
def test_send_data(self): data = 'some test data' f = StringIO(data) f.filename = 'my.txt' f.headers = {'content-type': 'text/plain'} bf = make_blobfile(f, somerandomkw='thevalue') ok_headers = {'Content-Length': 14, 'Content-Type': 'text/plain', 'Content-Disposition': ("attachment;filename*=" "UTF-8''my.txt")} response = MockResponse() ret = bf.send_data(response) self.assertEqual(response.headers, ok_headers) self.assertEqual(ret, 'some test data') ok_headers['Content-Disposition'] = "attachment" ret = bf.send_data(response, set_filename=False) self.assertEqual(response.headers, ok_headers) self.assertEqual(ret, 'some test data') self.assertEqual(response.headers.get('X-Sendfile'), None) request = MockRequest() request.set_header('X-NaayaEnableSendfile', 'on') ret = bf.send_data(response, REQUEST=request) self.assertEqual(ret, "[body should be replaced by front-end server]") self.assertEqual(response.headers.get('X-Sendfile'), bf._current_filename())
def afterSetUp(self): addNyForum( self.portal, id='forum_id', title='myforum', categories=['Test category', 'Test category 2'], description='Description 1', file_max_size='15', ) forum = self.portal.forum_id myfile = StringIO('some test data') myfile.filename = 'the_file.txt' addNyForumTopic(forum, id='topic_id', title='topic title', category='Test category', description='Test Description', attachment=myfile, sort_reverse=True, ) topic = forum.topic_id addNyForumMessage(topic, id='message_id', title='Message title', description='Message Description', attachment=myfile, ) message = topic.message_id message.replyMessage(title='Reply to message_id', description='Reply message description', attachment=myfile, ) transaction.commit()
def test_change_file(self): myfile = StringIO('hello data!') myfile.filename = 'my.txt' self.add_bfile(id='mybfile', title='My bfile', uploaded_file=myfile) mybfile = self.portal.myfolder.mybfile myfile2 = StringIO('new data') myfile2.filename = 'other.txt' mybfile._save_file(myfile2, contributor='contributor') self.assertEqual(len(mybfile._versions), 2) cv = mybfile.current_version self.assertEqual(cv.filename, 'other.txt') self.assertEqual(cv.size, 8) self.assertEqual(cv.open().read(), 'new data')
def loadfile(rel_filename): """A helper method for loading test data files.""" home = package_home(product_globals) filename = os.path.sep.join([home, rel_filename]) data = StringIO(open(filename, 'r').read()) data.filename = os.path.basename(rel_filename) return data
def create_object(context, portal_type, data): """ """ title = get_text_line() if data.get('type_in_title'): pt = api.portal.get_tool('portal_types') title = '%s: %s' % ( pt.getTypeInfo(portal_type).title, title ) data['title'] = title unique_id = generate_unique_id(context, title, portal_type) args = {'id': unique_id} if portal_type in ['Image', 'File']: myfile = StringIO(decodestring( 'R0lGODlhAQABAPAAAPj8+AAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==')) ext = portal_type == 'Image' and 'gif' or 'dat' myfile.filename = '.'.join((get_text_line().split(' ')[-1], ext)) args.update({'file': myfile}) new_id = context.invokeFactory(portal_type, **args) obj = context[new_id] if IDexterityContent.providedBy(obj): populate_dexterity(obj, data) else: populate_archetype(obj, data) if data.get('publish', True): wftool = api.portal.get_tool('portal_workflow') try: wftool.doActionFor(obj, 'publish') except WorkflowException, e: log.warn(e)
def test_add_with_file(self): myfile = StringIO('hello data!') myfile.filename = 'my.jpg' myfile.headers = {'content-type': 'image/jpeg'} tzinfo = self.portal.get_tzinfo() now_pre = datetime.now(tzinfo) self.add_bfile(id='mybfile', title='My bfile', uploaded_file=myfile) now_post = datetime.now(tzinfo) mybfile = self.portal.myfolder.mybfile self.assertTrue(mybfile.id, 'mybfile') self.assertTrue(mybfile.title, 'My bfile') vstore = mybfile._versions_i18n[mybfile.get_selected_language()] self.assertEqual(len(vstore), 1) bf = vstore[0].__of__(mybfile) path1 = mybfile.current_version.getPhysicalPath() path2 = bf.getPhysicalPath() self.assertTrue(path1 == path2) ver = mybfile.current_version self.assertTrue(now_pre <= pytz.utc.localize(ver.timestamp) <= now_post) self.assertEqual(ver.open().read(), 'hello data!') self.assertEqual(ver.filename, 'my.jpg') self.assertEqual(ver.size, 11) self.assertEqual(ver.content_type, 'image/jpeg')
def handle_upload(self, id, attached_file): if not attached_file: return if id in self.objectIds(): self.manage_delObjects([id, ]) attached_file.seek(0) data = attached_file.read() sfile = StringIO(data) sfile.filename = attached_file.filename[-1] content_type = mimetypes.guess_type(attached_file.filename)[0] if content_type is None: content_type = getattr(attached_file, 'headers', {}).get( 'content-type', 'application/octet-stream') sfile.headers = {'content-type': content_type} if id in self.objectIds(): self.manage_delObjects([id, ]) bf = make_blobfile(sfile, title=attached_file.filename, removed=False, timestamp=datetime.utcnow(), contributor='') bf.content_type = content_type bf.filename = id attached_file.seek(0) bf.size = len(attached_file.read()) setattr(self, id, bf)
def test_custom_table_template(self): listingblock = self._create_listingblock() _file = listingblock.get( listingblock.invokeFactory('File', 'file')) dummy = StringIO("DATA") dummy.filename = 'dummy.pdf' _file.setFile(dummy) _file.setTitle("Dummy PDF") _file.processForm() view = queryMultiAdapter((listingblock, listingblock.REQUEST), name='block_view') # The table header (th) id value is implemented as css klass self.assertNotIn('id="header-getContentType"', view.render_table()) self.assertNotIn('id="header-sortable_title"', view.render_table()) self.assertNotIn('id="header-modified"', view.render_table()) self.assertIn('class="header-getContentType', view.render_table()) self.assertIn('class="header-sortable_title', view.render_table()) self.assertIn('class="header-modified', view.render_table()) self.assertIn('class="column-getContentType', view.render_table()) self.assertIn('class="column-sortable_title', view.render_table()) self.assertIn('class="column-modified', view.render_table())
def handleDeferred(self, obj, objpath, content): """ very large files get deferred to get sent out so we need an extra handler for it """ if 'fieldvalues' in content: for fieldname, value in content['fieldvalues'].items(): if value['value'] == json.Deferred: resp = requests.post(self.sourcesite + \ '/@@migrator-exportfield', data={'path': objpath, 'field': fieldname}) largefile = False if int(resp.headers['content-length']) / 1024 / 1024 > 50: largefile = True transaction.commit() # commit before and after migr = FieldMigrator(self.site, obj, fieldname) field = obj.getField(fieldname) content = resp.content filename = resp.headers.get('filename', '') mimetype = resp.headers.get('content-type', '') if IBlobField.providedBy(field): # not a blob field here... content = StringIO(content) content.filename = filename migr.set({'value': content, 'extras': { 'filename': filename, 'mimetype': mimetype }}) if largefile: transaction.commit()
def test_validation_non_image(self): # IOError raised by PIL become LaunchpadValidationErrors. field = self.ExampleImageUpload(default_image_resource='dummy') image = StringIO('foo bar bz') image.filename = 'foo.jpg' self.assertRaises( LaunchpadValidationError, field.validate, image)
def create_object(context, portal_type, data): """ """ title = get_text_line() unique_id = generate_unique_id(context, title, portal_type) args = dict(id=unique_id) if portal_type in ['Image', 'File']: myfile = StringIO(decodestring('R0lGODlhAQABAPAAAPj8+AAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==')) ext = portal_type == 'Image' and 'gif' or 'dat' myfile.filename = '.'.join((get_text_line().split(' ')[-1], ext)) args.update({'file':myfile}) new_id= context.invokeFactory(portal_type, **args) obj = context[new_id] if IDexterityContent.providedBy(obj): if shasattr(obj, 'title'): obj.title = title populate_dexterity_type(obj, data) else: obj.setTitle(title) populate_archetype(obj, data) if data.get('publish', True): wftool = getToolByName(context, 'portal_workflow') try: wftool.doActionFor(obj, 'publish') except WorkflowException, e: log.warn(e)
def load_file(filename): import os from StringIO import StringIO from Globals import package_home filename = os.path.sep.join([package_home(globals()), filename]) data = StringIO(open(filename, 'rb').read()) data.filename = os.path.basename(filename) return data
def generate(self, env, source, params): self.uri = source.uri self.log.debug('<map:generate src="%s"> process()' % source.uri) data = source.read() if isinstance(data, str): # XXX: This code isn't obvious, but etree.parse() needs fd.filename # in order to resolve URIs in XSLT's document() function during # etree.XSLT()() calls. Anyway, this code should be rewritten fd = StringIO(data) if hasattr(source, "filename"): fd.filename = source.filename else: fd.filename = source.uri env.response.body = etree.parse(fd).getroot() else: env.response.body = data
def test_convert_excel(self, mock_invoke, mock_commit): """ Check convert_excel_file when an correct template is uploaded The conversion works and produces XML files in the envelope The original file is also uploaded Verify the content_type is 'application/vnd.ms-excel' The expected result is 1 """ xml_content = '''<?xml version="1.0" encoding="UTF-8"?> <report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://schema.eu/schema1 https://schema.eu/schema.xsd"> </report>''' mock_invoke.return_value = { 'conversionLog': '-- conversion log --', 'convertedFiles': [{'content': Mock(data=xml_content), 'fileName': 'StationsRivers.xml'}, {'content': Mock(data=xml_content), 'fileName': 'NutrientsRivers_Agg.xml'}, {'content': Mock(data=xml_content), 'fileName': 'HazSubstRivers_Agg.xml'}, {'content': Mock(data=xml_content), 'fileName': 'HazSubstRivers_Disagg.xml'}], 'resultCode': '0', 'resultDescription': 'Conversion successful.', } myfile = StringIO('-- some reporting data --') myfile.filename = 'Rivers_2011.xls' res = self.envelope.convert_excel_file(myfile) self.assertEquals(1, res) self.assertEquals(5, len(self.envelope.objectValues('Report Document'))) document = self.envelope['Rivers_2011.xls'] self.assertEquals('application/vnd.ms-excel', document.content_type) document = self.envelope['Rivers_2011_StationsRivers.xml'] self.assertEquals('text/xml', document.content_type) # NOTE don't test feedback creation here # feedback creation already tested in testFeedback.py #feedback = self.envelope['conversion_log_Rivers_2011.xls'] #self.assertEquals(feedback.meta_type, 'Report Feedback') #Now try it again to make sure there's no error in deleting old files myfile = StringIO('-- some reporting data --') myfile.filename = 'Rivers_2011.xls' res = self.envelope.convert_excel_file(myfile) self.assertEquals(1, res) self.assertEquals(5, len(self.envelope.objectValues('Report Document')))
def _update(self, portal): brains = portal.portal_catalog(meta_type=['Naaya Survey Answer']) for brain in brains: try: obj = brain.getObject() except: self.log.warning( "Couldn't get obj based on catalog information %s", brain.getURL()) continue for upload in obj.objectValues('ExtFile'): if upload is None: continue if 'blob' in getattr(upload, 'meta_type', "").lower(): continue if upload.is_broken(): self.log.warning( "\t BROKEN EXTFILE: Couldn't migrate extfile for " "%s because of broken file", obj.absolute_url() ) continue if upload.get_size(): data = upload.index_html() sfile = StringIO(data) self.log.debug('\t VERSION FILENAME: %s', '/'.join(upload.filename)) sfile.filename = upload.filename[-1] sfile.headers = { 'content-type': upload.content_type} bf = make_blobfile(sfile, title=upload.title, removed=False, timestamp=datetime.utcnow(), contributor='') bf.filename = upload.id bf.content_type = upload.content_type bf.size = len(data) upload._delete('/'.join(upload.filename)) setattr(obj, upload.id, bf) self.log.info("Migrated %s bytes to bfile: %s" % (len(data), obj.absolute_url())) obj._p_changed = True self.log.info('Migration done') return True
def _set_logo(self, subsite): # 1 x 1 px gif, black data = ( 'GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00' '\x00!\xf9\x04\x04\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00' '\x01\x00\x00\x02\x02D\x01\x00;') img = StringIO(data) img.filename = 'logo.gif' subsite.setLogo(img)
def test_add_utf8_filename(self): name = u'A\xa7A\xb6A\xa9A\xae_\x86\x90a\x83\x91a\x86\x93a\x99\xaa1.txt' myfile = StringIO('hello data!') myfile.filename = name.encode('utf-8') myfolder = self.portal.myfolder file_id = addNyBFile(myfolder, uploaded_file=myfile, submitted=1, contributor='contributor') self.assertEqual(file_id, 'assapa-c-r-_aaaa1') # as returned by unidecode
def test_add(self): from plone.resourceeditor.browser import FileManager from StringIO import StringIO r = self._make_directory() view = FileManager(r, self.layer['request']) d = StringIO('foo') d.filename = 'test.txt' info = view.add('/', d) self.assertEqual(info['code'], 0) self.assertEqual(info['error'], '') self.assertEqual(info['name'], 'test.txt') self.assertEqual(info['path'], '/') self.assertEqual(info['parent'], '/')
def test_add_exists(self): from plone.resourceeditor.browser import FileManager from StringIO import StringIO r = self._make_directory() r.writeFile('test.txt', 'boo') view = FileManager(r, self.layer['request']) d = StringIO('foo') d.filename = 'test.txt' info = view.add('/', d) self.assertEqual(info['code'], 1) self.assertNotEqual(info['error'], '') self.assertEqual(r.readFile('test.txt'), 'boo')
def testWebDavUpdate(self): image = StringIO(getImage()) image.filename = 'original.gif' base = '/'.join(self.folder.getPhysicalPath()) response = self.publish(base + '/foo-image', request_method='PUT', stdin=image, basic=self.getCredentials(), env={'CONTENT_TYPE': 'image/gif'}) self.assertEqual(response.getStatus(), 204) self.assertTrue('foo-image' in self.folder.objectIds()) fooimage = self.folder['foo-image'] self.assertEqual(fooimage.getId(), 'foo-image') self.assertEqual(fooimage.Title(), 'an image') # as opposed to during file upload, editing a file via webdav (e.g. # using the "external editor" feature) should not change the filename self.assertEqual(fooimage.getFilename(), 'original.gif')
def law_to_markdown(filein, fileout=None, name=None): ret = False if fileout is None: fileout = StringIO() ret = True parser = sax.make_parser() if name is None: orig_slug = filein.name.split('/')[-1].split('.')[0] else: orig_slug = name handler = LawToMarkdown(fileout, orig_slug=orig_slug) parser.setFeature(sax.handler.feature_external_ges, False) parser.setContentHandler(handler) parser.parse(filein) if ret: fileout.filename = handler.filename return fileout
def test_add_subfolder(self): from plone.resourceeditor.browser import FileManager from StringIO import StringIO r = self._make_directory() r.makeDirectory("alpha") view = FileManager(r, self.layer['request']) d = StringIO("foo") d.filename = "test.txt" info = view.add("/alpha", d) self.assertEqual(info['code'], 0) self.assertEqual(info['error'], '') self.assertEqual(info['name'], 'test.txt') self.assertEqual(info['path'], '/alpha') self.assertEqual(info['parent'], '/alpha')
def setUpPloneSite(self, portal): for name in ['file', 'image']: self.applyProfile(portal, 'plone.app.blob:%s-replacement' % name) # allow creating the replaced types types = getToolByName(portal, 'portal_types') assert types.getTypeInfo('Blob').product == 'plone.app.blob' types.getTypeInfo('ATFile').global_allow = True types.getTypeInfo('ATImage').global_allow = True testing.setRoles(portal, testing.TEST_USER_ID, ['Manager']) folder = portal.portal_membership.getHomeFolder(testing.TEST_USER_ID) image = StringIO(getData('image.gif')) image.filename = 'original.gif' folder.invokeFactory('Image', id='foo-image', title='an image', image=image)
def deletePersonalPortrait(self, id=None): """deletes the Portait of a member. """ authenticated_id = self.getAuthenticatedMember().getId() if not id: id = authenticated_id safe_id = self._getSafeMemberId(id) if id != authenticated_id and not _checkPermission( ManageUsers, self): raise Unauthorized # The plugable actions for how to handle the portrait. portrait_url = portal_url() + '/defaultUser.png' imgData = requests.get(portrait_url).content image = StringIO(imgData) image.filename = 'defaultUser' adapter = getMultiAdapter((self, self.REQUEST), IPortraitUploadAdapter) adapter(image, safe_id)
def testAddWithoutName(self): data = StringIO('File Contents') data.filename = "test.txt" response = self.publish('/+/zope.app.file.File=', form={ 'type_name': u'zope.app.file.File', 'field.data': data, 'field.data.used': '', 'UPDATE_SUBMIT': u'Add' }, basic='mgr:mgrpw') self.assertEqual(response.getStatus(), 302) self.assertEqual(response.getHeader('Location'), 'http://localhost/@@contents.html') root = self.getRootFolder() self.assert_('test.txt' in root) file = root['test.txt'] self.assertEqual(file.data, 'File Contents')
def testWebDavUpload(self): image = StringIO(getImage()) image.filename = 'original.gif' base = '/'.join(self.folder.getPhysicalPath()) response = self.publish(base + '/image', request_method='PUT', stdin=image, basic=self.getCredentials(), env={'CONTENT_TYPE': 'image/gif'}) self.assertEqual(response.getStatus(), 201) self.assertTrue('image' in self.folder.objectIds()) obj = self.folder.image self.assertEqual(obj.getPortalTypeName(), 'Image') self.assertTrue(IATBlobImage.providedBy(obj), 'no blob?') self.assertEqual(str(obj.getField('image').get(obj)), image.getvalue()) # on initial (webdav) upload no filename is set by the client, # so it should end up being equal to the last path/url component... self.assertEqual(obj.getFilename(), 'image')
def _update(self, portal): brains = portal.portal_catalog(meta_type=['Naaya Publication']) for brain in brains: fobj = brain.getObject() self.log.info("Migrating object %s", fobj.absolute_url()) if not hasattr(fobj, '_ext_file'): continue if fobj._ext_file.is_broken(): self.log.warning( "\t BROKEN EXTFILE: Couldn't migrate extfile for " "%s because of broken file", fobj.absolute_url()) continue if fobj._ext_file.get_size(): data = fobj._ext_file.index_html() out = StringIO() out.filename = fobj._ext_file.id out.write(data) out.seek(0) fobj._bfile = make_blobfile( out, filename=fobj._ext_file.id, content_type=fobj._ext_file.content_type) fobj._p_changed = True self.log.info("Migrated %s bytes to bfile: %s" % (len(data), fobj.absolute_url())) fobj._ext_file._delete('/'.join(fobj._ext_file.filename)) del fobj._ext_file self.log.info("Removed _ext_file for %s" % fobj.absolute_url()) self.log.info('Migrated to bfile: %s' % fobj.absolute_url()) self.log.info('Migration done') return True
def test_direct_download(self): """ In folder listing we should have a download link that allows users to download the last version directly """ file_contents = 'simple contents' uploaded_file = StringIO(file_contents) uploaded_file.filename = 'test.txt' uploaded_file.headers = {'content-type': 'text/plain; charset=utf-8'} addNyBFile(self.portal.myfolder, id='mybfile_download', title='My file', uploaded_file=uploaded_file, submitted=1, contributor='admin') transaction.commit() self.browser_do_login('admin', '') self.browser.go('http://localhost/portal/myfolder') self.assertTrue( self.portal.myfolder.mybfile_download.current_version_download_url() in self.browser.get_html())
def test_add_replace(self): from plone.resourceeditor.browser import FileManager from StringIO import StringIO r = self._make_directory() r.writeFile("test.txt", "boo") view = FileManager(r, self.layer['request']) d = StringIO("foo") d.filename = "test.txt" info = view.add("/", d, "/test.txt") self.assertEqual(info['code'], 0) self.assertEqual(info['error'], '') self.assertEqual(info['name'], 'test.txt') self.assertEqual(info['path'], '/') self.assertEqual(info['parent'], '/') self.assertEqual(r.readFile("test.txt"), "foo")
def test_main_exfile(self): """ Test the simple consultation exfile functionality """ addNySimpleConsultation(self.test_folder, id='scs') scs = self.test_folder._getOb('scs') #no exfile should exist exfile = scs.get_exfile self.failUnlessEqual(exfile(), None) #add exfile from os import path from StringIO import StringIO f = open(path.join(path.dirname(__file__), 'test.txt'), 'rb') exf = StringIO(f.read()) f.close() exf.filename = 'test.txt' self.failUnlessRaises(ValueError, scs.saveProperties, file=exfile) scs.saveProperties(title='scs', file=exf, lang='en') #test exfile presence self.failUnlessEqual(bool(exfile()), True)
def test_view(self): """Same as download, but without the attachment header""" file_contents = 'simple contents' uploaded_file = StringIO(file_contents) uploaded_file.filename = 'test.txt' uploaded_file.headers = {'content-type': 'text/plain; charset=utf-8'} addNyBFile(self.portal.myfolder, submitted=1, contributor='contributor', id='test', title="test", uploaded_file=uploaded_file, approved=True) transaction.commit() #View the file self.browser.go( 'http://localhost/portal/myfolder/test/download/1/test.txt?action=view' ) self.assertEqual(file_contents, self.browser.get_html())
def submit_letter(self, data): account = self.manager.get(data['az']) user = self.manager.get(data['az']) if user is not None: info = data.by_schema[IMessage] message = Message() grok.notify(grok.ObjectCreatedEvent(message)) from dolmen.forms.base import set_fields_data if 'attachment' in info.keys(): from StringIO import StringIO f = StringIO(decodestring(info['attachment'])) # f.filename="download" f.filename = info['filename'] info['attachment'] = f set_fields_data(IMessage, message, info) directlyProvides(message, IQuestion) user['nachrichten'].add(message) grok.notify(AfterSaveEvent(message, self.request)) self.request.response.setStatus(202) return raise KeyError('Unknown user.')
def test_random_action(self): """Radom action""" file_contents = 'simple contents' uploaded_file = StringIO(file_contents) uploaded_file.filename = 'test.txt' uploaded_file.headers = {'content-type': 'text/plain; charset=utf-8'} addNyLocalizedBFile(self.portal.myfolder, submitted=1, contributor='contributor', id='test', title="test", uploaded_file=uploaded_file, approved=True) transaction.commit() self.browser.go( 'http://localhost/portal/de/myfolder/test/download/1/test.txt?action=shop' ) self.failUnless('An error was encountered' in self.browser.get_html()) self.failUnless('NotFound' in self.browser.get_html())
def test_add_with_file(self): myfile = StringIO('hello data!') myfile.filename = 'my.jpg' myfile.headers = {'content-type': 'image/jpeg'} tzinfo = self.portal.get_tzinfo() now_pre = datetime.now(tzinfo) self.add_bfile(id='mybfile', title='My bfile', uploaded_file=myfile) now_post = datetime.now(tzinfo) mybfile = self.portal.myfolder.mybfile self.assertTrue(mybfile.id, 'mybfile') self.assertTrue(mybfile.title, 'My bfile') self.assertEqual(len(mybfile._versions), 1) self.assertTrue(mybfile.current_version.getPhysicalPath() == mybfile._versions[0].__of__(mybfile).getPhysicalPath()) ver = mybfile.current_version self.assertTrue(now_pre <= ver.timestamp <= now_post) self.assertEqual(ver.open().read(), 'hello data!') self.assertEqual(ver.filename, 'my.jpg') self.assertEqual(ver.size, 11) self.assertEqual(ver.content_type, 'image/jpeg')
def manage_addFile(self, id, file="", **kwargs): # if self.is_ext: # return manage_addExtFile(self, id=id, file=file) if self.is_blobfile: if isinstance(file, basestring): if not bool(id): raise ValueError, "Please specify id of file passed as string" f = StringIO() f.write(file) f.seek(0) f.filename = id file = f if not bool(id): # TODO: make sure of proper id id = file.filename blobfile = make_blobfile(file) self._setObject(id, blobfile, set_owner=0) blobfile.id = id return blobfile.getId() else: return NyContainer.manage_addFile(self, id, file)
def test_view_zip(self): """ Custom view of zipfile """ from zipfile import ZipFile zfile = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'one_file_zip.zip'), 'rb') upload_file = StringIO(zfile.read()) upload_file.filename = 'test.zip' upload_file.headers = {'content-type': 'application/zip'} namelist = ZipFile(zfile).namelist() zfile.close() addNyBFile(self.portal.myfolder, submitted=1, contributor='contributor', id='test', title="test", uploaded_file=upload_file) transaction.commit() #The result is a simple html with the contents of the zipfile (filepaths) self.browser.go('http://localhost/portal/myfolder/test/download/en/1/test.zip?action=view') html_content = self.browser.get_html() for filepath in namelist: self.assertTrue(filepath in html_content) zfile.close()
def afterSetUp(self): addNyForum( self.portal, id='forum_id', title='myforum', categories=['Test category', 'Test category 2'], description='Description 1', file_max_size='15', ) forum = self.portal.forum_id myfile = StringIO('some test data') myfile.filename = 'the_file.txt' addNyForumTopic( forum, id='topic_id', title='topic title', category='Test category', description='Test Description', attachment=myfile, sort_reverse=True, ) topic = forum.topic_id addNyForumMessage( topic, id='message_id', title='Message title', description='Message Description', attachment=myfile, ) message = topic.message_id message.replyMessage( title='Reply to message_id', description='Reply message description', attachment=myfile, ) transaction.commit()
def test_guess_mimetype(self): f = StringIO('some test image') f.filename = 'photo.jpg' bf = make_blobfile(f) self.assertEqual(bf.content_type, 'image/jpeg')
def _deserialize(cls, data): io = StringIO(base64.b64decode(data['data'])) io.filename = data['filename'] return io
def setUp(self): super(FileWidgetTest, self).setUp() file = StringIO('Foo Value') file.filename = 'test.txt' self._widget.request.form['field.foo'] = file
def _preparefile(self, rel_filename, ctype='text/xml'): ofile = self._loadfile(rel_filename) fp = StringIO(ofile.get('data')) fp.filename = ofile.get('name') return fp
def test_remove_version(self): myfile = StringIO('hello data!') myfile.filename = 'my.txt' self.add_localizedbfile(id='mylocalizedbfile', title='My Localized bfile', uploaded_file=myfile, lang='en') mylocalizedbfile = self.portal.myfolder.mylocalizedbfile language = 'de' self.assertRaises(KeyError, mylocalizedbfile.remove_version, 1, language) language = 'en' myfile2 = StringIO('new data') myfile2.filename = 'other.txt' mylocalizedbfile._save_file(myfile2, language, contributor='contributor') myfrfile = StringIO('new french data') myfrfile.filename = 'fr_file1.txt' mylocalizedbfile._save_file(myfrfile, 'fr', contributor='contributor') myfrfile2 = StringIO('newer french data') myfrfile2.filename = 'fr_file2.txt' mylocalizedbfile._save_file(myfrfile2, 'fr', contributor='contributor') myfrfile3 = StringIO('latest french data') myfrfile3.filename = 'fr_file3.txt' mylocalizedbfile._save_file(myfrfile3, 'fr', contributor='contributor') to_remove = mylocalizedbfile._versions[language][1] mylocalizedbfile.remove_version(1, language) self.failUnless(to_remove in mylocalizedbfile._versions[language]) self.assertEqual(to_remove.open().read(), '') self.assertEqual(to_remove.size, None) self.assertEqual(to_remove.removed, True) self.assertTrue(mylocalizedbfile.current_version is mylocalizedbfile._versions[language][0]) myfile3 = StringIO('even newer data') myfile3.filename = 'other.txt' mylocalizedbfile._save_file(myfile3, language, contributor='contributor') self.assertTrue(mylocalizedbfile.current_version is mylocalizedbfile._versions[language][2]) self.failUnlessEqual(myfile3.filename, mylocalizedbfile._versions[language][2].filename) mylocalizedbfile.remove_version(2, language) self.assertTrue(mylocalizedbfile.current_version is mylocalizedbfile._versions[language][0]) mylocalizedbfile.remove_version(0, language) self.assertTrue(mylocalizedbfile.current_version is None) language = 'fr' rm_ver = mylocalizedbfile._versions[language][1] mylocalizedbfile.remove_version(1, language) self.failUnless(rm_ver in mylocalizedbfile._versions[language]) self.assertEqual(rm_ver.open().read(), '') self.assertEqual(rm_ver.size, None) self.assertEqual(rm_ver.removed, True) mylocalizedbfile.get_selected_language = Mock(return_value=language) cv = mylocalizedbfile.current_version self.assertTrue(cv is mylocalizedbfile._versions[language][2]) mylocalizedbfile.remove_version(2, language) cv = mylocalizedbfile.current_version self.assertTrue(cv is mylocalizedbfile._versions[language][0]) mylocalizedbfile.remove_version(0, language) cv = mylocalizedbfile.current_version self.assertTrue(cv is None)
def make_file_like(name='file', content='data'): sio = StringIO(content) sio.filename = name sio.content_type = 'text/html' return sio
def asset_as_StringIO(filename): result = StringIO(asset(filename).bytes()) result.filename = filename return result
def loadFile(self, filename): filename = os.path.sep.join([package_home(globals()), filename]) data = StringIO(open(filename, 'rb').read()) data.filename = os.path.basename(filename) return data
from plone.app.contenttypes.testing import ( PLONE_APP_CONTENTTYPES_FIXTURE, PLONE_APP_CONTENTTYPES_MIGRATION_FIXTURE) from plone.app.testing import PLONE_FIXTURE from plone.app.testing import TEST_USER_ID from plone.app.testing import TEST_USER_NAME from plone.app.testing import TEST_USER_PASSWORD from plone.app.testing import layers from plone.app.testing import login from plone.app.testing import ploneSite from plone.app.testing import setRoles from plone.testing import z2 from zope.configuration import xmlconfig B64_DATA = 'R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=' GIF = StringIO(decodestring(B64_DATA)) GIF.filename = 'sample.gif' GIF.contentType = 'image/gif' GIF._width = 1 GIF._height = 1 def create(container, type_name, **kwargs): """A easy helper method to create some content since we do not have plone.api in core. """ new_id = container.invokeFactory(type_name, **kwargs) content = container[new_id] # Archetypes specific code was taken from ``plone.api`` # Switch when api has been merged into core.
def test_validation_non_image(self): # IOError raised by PIL become LaunchpadValidationErrors. field = self.ExampleImageUpload(default_image_resource='dummy') image = StringIO('foo bar bz') image.filename = 'foo.jpg' self.assertRaises(LaunchpadValidationError, field.validate, image)