def _process_form_value_with_suffix(self, form, suffix=u''): """ add suffix parameter & use it """ posted = form._cw.form if self.input_name(form, u'__detach') in posted: # drop current file value on explictily asked to detach return None try: value = posted[self.input_name(form, suffix)] except KeyError: # raise UnmodifiedField instead of returning None, since the later # will try to remove already attached file if any raise ff.UnmodifiedField() # value is a 2-uple (filename, stream) try: filename, stream = value except ValueError: raise ff.UnmodifiedField() # XXX avoid in memory loading of posted files. Requires Binary handling changes... value = Binary(stream.read()) if not value.getvalue(): # usually an unexistant file value = None else: # set filename on the Binary instance, may be used later in hooks value.filename = ff.normalize_filename(filename) return value
def _process_form_value(self, form): posted = form._cw.form if self.input_name(form, u'__detach') in posted: # drop current file value on explictily asked to detach return None try: value = posted[self.input_name(form)] except KeyError: # raise UnmodifiedField instead of returning None, since the later # will try to remove already attached file if any raise UnmodifiedField() # value is a 2-uple (filename, stream) or a list of such # tuples (multiple files) try: if isinstance(value, list): value = value[0] form.warning('mutiple files provided, however ' 'only the first will be picked') filename, stream = value except ValueError: raise UnmodifiedField() # XXX avoid in memory loading of posted files. Requires Binary handling changes... value = Binary(stream.read()) if not value.getvalue(): # usually an unexistant file value = None else: # set filename on the Binary instance, may be used later in hooks value.filename = normalize_filename(filename) return value
def test_npts_import(self): with self.admin_access.repo_cnx() as cnx: orig = self._create_npts(cnx) cnx.commit() for ext, fmt in (('.csv', 'text/csv'),): if not is_supported(ext): continue fname = self.datapath('npts' + ext) blob = Binary(open(fname, 'rb').read()) blob.filename = fname ts = cnx.create_entity('NonPeriodicTimeSeries', data=blob) self.assertEqual(orig.timestamped_array(), ts.timestamped_array())
def resize(self, size): size = tuple(int(s) for s in size.split('x')) idownloadable = self.entity.cw_adapt_to('IDownloadable') ctype = idownloadable.download_content_type() fmt = ctype and ctype.split('/', 1)[1] or None if fmt is None: self.error('unable to resize') raise UnResizeable data = idownloadable.download_data() pilimg = pilopen(Binary(data)) pilimg.thumbnail(size, ANTIALIAS) stream = Binary() pilimg.save(stream, fmt) stream.seek(0) stream.filename = idownloadable.download_file_name() return stream
def test_ts_import(self): with self.admin_access.repo_cnx() as cnx: orig = self._create_ts(cnx, granularity=u'daily') cnx.commit() for ext, fmt in (('.xls', 'application/vnd.ms-excel'), ('.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), ('.csv', 'text/csv')): if not is_supported(ext): continue fname = self.datapath('ts' + ext) blob = Binary(open(fname, 'rb').read()) blob.filename = fname ts = cnx.create_entity('TimeSeries', granularity=u'daily', start_date=datetime(2009, 10, 1), data=blob) self.assertEqual(orig.timestamped_array(), ts.timestamped_array())
def thumbnail(self, shadow=False, size=None): if size is None: size = self._cw.vreg.config['image-thumb-size'] size = tuple(int(s) for s in size.split('x')) idownloadable = self.entity.cw_adapt_to('IDownloadable') data = idownloadable.download_data() try: pilimg = pilopen(Binary(data)) except IOError: raise UnResizeable if shadow: self.warning('[1.15.0] the shadow parameter is now unused ' 'and you should use css rules to lay shadows out', DeprecationWarning) pilimg.thumbnail(size, ANTIALIAS) stream = Binary() pilimg.save(stream, 'png') stream.seek(0) ithumbnail = self.entity.cw_adapt_to('IThumbnail') stream.filename = ithumbnail.thumbnail_file_name() return stream
def generate_bytes(self, entity, attrname, index, format=None): fakefile = Binary(("%s%s" % (attrname, index)).encode('ascii')) fakefile.filename = u"file_%s" % attrname return fakefile