コード例 #1
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)
コード例 #2
0
   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)