def convertStringToPreview(self, content, content_type, instance): """ convertStringToPreview(self, content) => Utility to convert a string to HTML using the converter stuff. """ LOG.debug("convertStringToPreview...") cnv = oo_to_html() return self._convertOutput(cnv.convert_(content,), "html")
def convertStringToIndex(self, content, content_type, instance): """ convertStringToIndex(self, content, content_type, instance) => Utility to convert a string to HTML using the converter stuff. """ LOG.debug("convertStringToIndex...") cnv = oo_to_html() return self._html_to_text(cnv.convert_(content,), )
def getIndexableValue(self, field, instance): """ getIndexableValue(self, field, instance) => (possibliy big) string Return the ZCatalog-indexable string for that type. """ LOG.debug("getIndexableValue") content = field.get(instance) content_type = field.getContentType(instance) return self.convertStringToIndex(content, content_type, instance)
def fakeUpload(self): current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self._dummy request = FakeRequest() request.form.update(field_values) dummy.REQUEST = request dummy.processForm(data=1) LOG.debug("dummy size after processForm: %d" % ( dummy.Schema()['attach_doc'].get(dummy).getSize())) self._content = dummy newSecurityManager(None, current_user)
def getPreview(self, field, instance): """ getPreview(self, field, instance) => string or None Return the HTML preview (generating it if it's not already done) for this attachement. If the attachment is not previewable, or if there's a problem in the preview, return None. """ LOG.debug("getPreview") content = field.get(instance) content_type = field.getContentType(instance) return self.convertStringToPreview(content, content_type, instance)
def getIcon(self, instance): """ getIcon(self, instance) => return the underlying file class icon (object) """ name = self.getName() icon = getattr(instance, _icon_ % name, None) if not icon: handler = self._getHandler(instance) LOG.debug("getIcon for %s / %s" % (self.getFilename(instance), handler.converter_type, )) icon = handler.getIconFile(self, instance) setattr(instance, _icon_ % name, icon) return getattr(instance, icon, None)
def test_get(self): current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self.makeDummy() request = FakeRequest() request.form.update(field_values) dummy.REQUEST = request dummy.processForm(data=1) for k, v in expected_values.items(): LOG.debug(k) got = dummy.Schema()[k].get(dummy) if isinstance(got, File): got = str(got) self.assertEquals(got, v, 'field "%s"\n got:\n %r\n\n\n expected:\n %r' % (k, got, v)) newSecurityManager(None, current_user)
def test_set(self): """Same as previous but with fieldsets""" current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self.makeDummy() request = FakeRequest() request.form.update(field_values) request.form['fieldset'] = 'default' dummy.REQUEST = request dummy.processForm() for k, v in test_fields.items(): LOG.debug(k) got = dummy.Schema()[k].get(dummy) if isinstance(got, (File, ScalableImage, Image)): got = str(got) self.assertEquals(got, v['expected'], 'field "%s"\ngot:\n %r\n\n\n expected:\n %r' % (k, got, v['expected'])) newSecurityManager(None, current_user)
def _getTransformPath(self, input, output): """ _getTransformPath(self, input, output) => chain or None Try to build a transform chain from 'input' mime type to 'output' mime type. If it's not possible to build such a chain, return None. Nota: this code is taken from TransformEngine.py """ ## get a path to output mime type transform = self.getPortalTransforms() requirements = transform._policies.get(target_mt, []) path = transform._findPath(orig_mt, target_mt, list(requirements)) if not path and requirements: LOG.debug("Unable to satisfy requirements %s" % (", ".join(requirements),)) path = transform._findPath(orig_mt, target_mt) if not path: LOG.debug("NO PATH FROM %s TO %s : %s" % (orig_mt, target_mimetype, path)) return None return path
def _convertStringToMime(self, content, content_type, instance, output_mime): # Check if a transform is available trans = self.getTransforms(None, instance) ct = content_type if not trans["html_paths"].get(ct, None): raise ValueError, "No converter found for content type '%s'" % (ct,) # Convert it to plain text pt = self.getPortalTransforms(None, instance) out = pt.convertTo(target_mimetype=output_mime, orig=content, data=None, mimetype=content_type) output = out.getData() # Try to use / guess the encoding out_encoding = out.getMetadata().get("encoding", None) if out_encoding: LOG.debug("Have encoding: '%s'" % out_encoding) output = unicode(output, encoding=out_encoding) else: # Convert from encoded string to unicode for enc in AVAILABLE_ENCODINGS: try: LOG.debug("Trying encoding: '%s'" % enc) output = output.decode(enc) break except UnicodeError: LOG.debug("Encoding '%s' failed" % enc) pass # Return an encoded output return self.unicode2string(output, instance)