def upgrade_to_ghost(self, content):
     container = content.get_container()
     if not IGhostFolder.providedBy(container):
         return None
     if container.get_link_status() is not None:
         logger.warning(
             u"Invalid Ghost Folder, not transforming: %s.",
             content_path(content))
         return None
     content_id = content.getId()
     container_haunted = container.get_haunted()
     content_haunted = container_haunted._getOb(content_id, None)
     if not IImage.providedBy(content_haunted):
         logger.warning(
             u"Original found for %s, but is not an image, not transforming it.",
             content_path(content))
         return None
     # Prevent quota system to fail if the image is an old one.
     # XXX Quota will be out of sync after the upgrade.
     if (content.hires_image is not None and
         content.hires_image.meta_type != 'Silva File'):
         content.hires_image = None
     ghost = get_factory(content_haunted)(
                 ghost=content,
                 container=container,
                 auto_delete=True,
                 auto_publish=True).modify(
         content_haunted, content_id).verify()
     logger.info(
         u"Image converted to ghost asset: %s.",
         content_path(content))
     return ghost
Example #2
0
 def __call__(self, tree):
     for block in tree.xpath('//div[contains(@class, "image")]'):
         images = block.xpath('descendant::img')
         assert len(images) == 1, u"Invalid image construction"
         image = images[0]
         if 'reference' in image.attrib:
             name, reference = self.get_reference(image.attrib['reference'])
             if reference is not None and reference.target_id:
                 content = reference.target
                 image_url = absoluteURL(content, self.request)
                 if IImage.providedBy(content):
                     resolution = None
                     if 'resolution' in image.attrib:
                         resolution = image.attrib['resolution']
                         del image.attrib['resolution']
                     if resolution == 'hires':
                         image_url += '?hires'
                         size = content.get_dimensions(hires=True)
                     elif resolution == 'thumbnail':
                         image_url += '?thumbnail'
                         size = content.get_dimensions(thumbnail=True)
                     else:
                         size = content.get_dimensions()
                     if size.height and size.width:
                         image.attrib['height'] = str(size.height)
                         image.attrib['width'] = str(size.width)
                 image.attrib['src'] = image_url
             del image.attrib['reference']
Example #3
0
 def _check_image(self, target):
     source = self.source
     if source is not None:
         is_image = IImage.providedBy(target)
         have_image = IImageIncluable.providedBy(source)
         if is_image and not have_image:
             alsoProvides(source, IImageIncluable)
         elif not is_image and have_image:
             noLongerProvides(source, IImageIncluable)
Example #4
0
 def _check_image(self, target):
     source = self.source
     if source is not None:
         is_image = IImage.providedBy(target)
         have_image = IImageIncluable.providedBy(source)
         if is_image and not have_image:
             alsoProvides(source, IImageIncluable)
         elif not is_image and have_image:
             noLongerProvides(source, IImageIncluable)
Example #5
0
    def render(self, item):
        content = item.getObject()

        if not IImage.providedBy(content):
            return

        if content.thumbnail_image is None:
            return

        anchor = '<a href="%s"><img src="%s/thumbnail_image" /></a>' % (
            item.getURL(), content.thumbnail_image.get_download_url())
        return '<div class="searchresult-thumbnail">%s</div>' % anchor
Example #6
0
    def render(self, item):
        content = item.getObject()

        if not IImage.providedBy(content):
            return

        if content.thumbnail_image is None:
            return

        anchor = '<a href="%s"><img src="%s/thumbnail_image" /></a>' % (
            item.getURL(), content.thumbnail_image.get_download_url())
        return '<div class="searchresult-thumbnail">%s</div>' % anchor
Example #7
0
 def _get_image(self):
     text = unicode(self.context.body)
     if text:
         tree = lxml.html.fromstring(text)
         results = tree.xpath("//img[@reference][1]")
         if results:
             image = results[0]
             reference_service = getUtility(IReferenceService)
             reference = reference_service.get_reference(self.context, name=image.attrib["reference"])
             content = reference.target
             if IImage.providedBy(content):
                 return content
     return None
Example #8
0
 def update(self):
     need(ISlideshowResources)
     self.contents = []
     for content in self.container.get_ordered_publishables(IMediaContent):
         version = content.get_viewable()
         if version is None:
             continue
         info = {"title": version.get_title(), "text": version.get_text().strip(), "image": None}
         asset = version.get_asset()
         if IImage.providedBy(asset):
             dimensions = asset.get_dimensions()
             info.update(
                 {"image": asset.url(request=self.request), "width": dimensions.width, "height": dimensions.height}
             )
         self.contents.append(info)
Example #9
0
    def convert(self, context):
        image = None
        attributes = {'alignment': self.getattr('alignment', 'default'),
                      'alt': self.getattr('title', '')}

        if self.hasattr('reference'):
            # We have a reference
            reference_name = str(self.getattr('reference'))
            reference_name, reference = context.get_reference(
                reference_name, read_only=True)
            assert reference is not None, "Invalid reference"
            attributes['_silva_target'] = reference.target_id
            attributes['_silva_reference'] = reference_name
            image = reference.target
            if image is not None:
                attributes['src'] = absoluteURL(image, context.request)
            else:
                site = IVirtualSite(context.request)
                attributes['src'] = site.get_root_url() + \
                    "/++resource++Products.SilvaDocument/broken-link.jpg"
                attributes['alt'] = u'Referenced image is missing.'
        elif self.hasattr('path'):
            path = self.getattr('path')
            src = IPath(context.request).pathToUrlPath(str(path))
            attributes['src'] = src
            try:
                image = context.model.unrestrictedTraverse(src.split('/'))
            except:
                pass
        else:
            raise ValueError('Invalid silva image tag')

        if image and IImage.providedBy(image):
            attributes['width'], attributes['height'] = \
                image.get_dimensions(image.image)

        return html.img(self.content.convert(context), **attributes)
   def convert(self, context):
       if hasattr(self, 'should_be_removed') and self.should_be_removed:
           return Frag()

       title = self.getattr('alt', '')
       alignment = self.getattr('alignment', 'default')
       if alignment == 'default':
           alignment = ''

       if self.hasattr('_silva_reference'):
           reference_name = str(self.getattr('_silva_reference'))
           reference_name, reference = context.get_reference(reference_name)
           if reference is not None:
               target_id = self.getattr('_silva_target', '0')
               try:
                   target_id = int(str(target_id))
               except ValueError:
                   target_id = 0
           else:
               reference_name, reference = context.get_reference('new')
               target_id = 0

           # The target changed, update it
           if target_id != reference.target_id:
               reference.set_target_id(target_id)

           return silva.image(
               self.content.convert(context),
               reference=reference_name,
               alignment=alignment,
               title=title)

       # This is an old url-based image link
       src = getattr(self.attr, 'src', None)
       if src is None:
           src = 'unknown'
       elif hasattr(src, 'content'):
           src = src.content
       src = urlparse(str(src))[2]
       src = IPath(context.request).urlToPath(str(src))
       if src.endswith('/image'):
           src = src[:-len('/image')]
       # turn path into relative if possible, traverse to the object to
       # fix an IE problem that adds the current location in front of paths
       # in an attempt to make them absolute, which leads to nasty paths
       # such as '/silva/index/edit/index/edit/foo.jpg'
       try:
           obj = context.model.unrestrictedTraverse(src.split('/'))
           # bail out if obj is not a Silva Image, otherwise the old
           # href value would be lost
           if not IImage.providedBy(obj):
               raise NotFound(src)
       except (KeyError, NotFound):
           pass
       else:
           modelpath = context.model.aq_parent.getPhysicalPath()
           src = '/'.join(Path(modelpath, obj.getPhysicalPath()))

       return silva.image(
           self.content.convert(context),
           path=src,
           alignment=alignment,
           title=title)
Example #11
0
 def assertIsBlobImage(self, content):
     self.assertTrue(IImage.providedBy(content))
     self.assertTrue(IBlobFile.providedBy(content.hires_image))
Example #12
0
 def assertIsZODBImage(self, content):
     self.assertTrue(IImage.providedBy(content))
     self.assertTrue(IZODBFile.providedBy(content.hires_image))
    def sax_img(self, node):
        """Unfortunately <image> is a special case, since height and width
        are not stored in the document but in the Image object itself, and
        need to be retrieved here.
        """
        attributes = {}
        options = self.getOptions()
        request = self.getExported().request
        if node.attributes:
            attributes = get_dict(node.attributes)

        if options.external_rendering:
            rewritten_path = None
            if 'reference' in attributes:
                service = getUtility(IReferenceService)
                reference = service.get_reference(
                    self.context, name=attributes['reference'])
                image = reference.target
                if options.upgrade30:
                    attributes['data-silva-target'] = str(reference.target_id)
                    attributes['data-silva-reference'] = reference.tags[1]
                    reference.tags[0] = u"body image"
                    reference._p_changed = True
                elif image is not None:
                    rewritten_path = absoluteURL(image, request)
            else:
                document = self.context.get_content()
                image = document.unrestrictedTraverse(
                    attributes['path'].split('/'), None)
                if options.upgrade30:
                    attributes['data-silva-url'] = attributes['path']
                elif image is not None:
                    path = IPath(document)
                    rewritten_path = path.pathToUrlPath(attributes['path'])
            if not options.upgrade30:
                if not rewritten_path:
                    site = IVirtualSite(request)
                    rewritten_path = site.get_root_url() + \
                        "/++resource++Products.SilvaDocument/broken-link.jpg"
                    attributes['title'] = _(u'Referenced image is missing')
                attributes['rewritten_path'] = rewritten_path

            if image is not None:
                if IImage.providedBy(image):
                    resolution = options.image_res
                    attributes['title'] = image.get_title()
                    if resolution and not options.upgrade30:
                        attributes['rewritten_path'] += '?%s' % resolution
                        if resolution == 'hires':
                            width, height = image.get_dimensions()
                        attributes['width'] = str(width)
                        attributes['height'] = str(height)

        else:
            if 'reference' in attributes:
                attributes['reference'] = self.get_reference(
                    attributes['reference'])

        if attributes.has_key('alignment'):
            if not attributes['alignment']:
                attributes['alignment'] = 'default'
        else:
            attributes['alignment'] = 'default'
        self.startElementNS(NS_DOCUMENT_URI, node.nodeName, attributes)
        self.endElementNS(NS_DOCUMENT_URI, node.nodeName)
 def assertIsBlobImage(self, content):
     self.assertTrue(IImage.providedBy(content))
     self.assertTrue(IBlobFile.providedBy(content.hires_image))
 def assertIsZODBImage(self, content):
     self.assertTrue(IImage.providedBy(content))
     self.assertTrue(IZODBFile.providedBy(content.hires_image))