示例#1
0
 def hasRelatedMedia(self):
     """
     Check that the canonical translation has some related images
     or videos.
     """
     context = self.context.getCanonical()
     related_items = context.getRelatedItems()
     media = [i for i in related_items if IATImage.providedBy(i)]
     return media and True or False
示例#2
0
 def hasRelatedMedia(self):
     """
     Check that the canonical translation has some related images
     or videos.
     """
     context = self.context.getCanonical()
     related_items = context.getRelatedItems()
     media = [i for i in related_items if IATImage.providedBy(i)]
     return media and True or False
示例#3
0
 def getRelatedImages(self, obj, image_scale):
     """
     Return html for a video, slideshow or image from the related
     items of the canonical translation.
     """
     scaled = list()
     obj = obj.getCanonical()
     related_items = obj.getRelatedItems()
     images = [i for i in related_items if IATImage.providedBy(i)]
     # Limit to three
     images = images[:3]
     for image in images:
         scaled_img = image.getField('image').getScale(image,
             scale=image_scale)
         scaled.append(scaled_img.absolute_url())
     return scaled
示例#4
0
    def getRelatedMedia(self, image_scale):
        """
        Return html for a slideshow or image from the related items of
        the canonical translation.
        """
        context = self.context.getCanonical()
        related_items = context.getRelatedItems()

        self.image_scale = image_scale
        if related_items:
            self.images = [i for i in related_items if IATImage.providedBy(i)]
            if len(self.images) == 1:
                image = self.images[0]
                return image.tag(scale=image_scale)
            else:
                return self.slideswitch_template()
示例#5
0
 def getRelatedImages(self, obj, image_scale):
     """
     Return html for a video, slideshow or image from the related
     items of the canonical translation.
     """
     scaled = list()
     obj = obj.getCanonical()
     related_items = obj.getRelatedItems()
     images = [i for i in related_items if IATImage.providedBy(i)]
     # Limit to three
     images = images[:3]
     for image in images:
         scaled_img = image.getField('image').getScale(image,
                                                       scale=image_scale)
         scaled.append(scaled_img.absolute_url())
     return scaled
示例#6
0
    def getRelatedMedia(self, image_scale):
        """
        Return html for a slideshow or image from the related items of
        the canonical translation.
        """
        context = self.context.getCanonical()
        related_items = context.getRelatedItems()

        self.image_scale = image_scale
        if related_items:
            self.images = [
                i for i in related_items if IATImage.providedBy(i)]
            if len(self.images) == 1:
                image = self.images[0]
                return image.tag(scale=image_scale)
            else:
                return self.slideswitch_template()
示例#7
0
def GrayscaleTransformations(event):
    """
    Changing the response body by converting
    the images to grayscale and changing the
    css colors definitions to colors from gray
    shades palette
    """
    request = event.request
    response = request.response
    context = request.get('PUBLISHED')
    resp_body = response.getBody()
    resp_body = getattr(context, 'GET', lambda: '')()

    if hasattr(context, 'im_self'):
        context = context.im_self
    if isinstance(context, ImageScale):
        context = context.data
    if hasattr(context, 'context'):
        context = context.context

    content_type = getattr(context, 'content_type', '')
    if callable(content_type):
        content_type = content_type() or ''
    if not content_type:
        content_type = response.headers.get('content-type') or ''

    if isinstance(context, FilesystemFile):
        resp_body = context().read()
        content_type = context.getContentType().split(';')[0]

    filename = getattr(context, 'getId', lambda: False)()
    filename = getattr(context, 'filename', filename)
    if not filename:
        try:
            filename = context.__name__
        except AttributeError:
            return

    images_content_types = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']

    browser_resource_image = False
    if isinstance(context, browserresourcefile) and \
       context.content_type.split(';')[0] in images_content_types:
        browser_resource_image = True

    if content_type:
        content_type = content_type.split(';')[0].strip()

    if 'javascript' in content_type:
        return

    if isinstance(context, FSImage) or \
       IOFSImage.providedBy(context) or \
       IATImage.providedBy(context) or \
       content_type in images_content_types or \
       browser_resource_image:

        try:
            path = '/'.join(context.getPhysicalPath())
        except AttributeError:
            path = filename

        try:
            resp_body = utils.get_resource(request, response, path)
        except NotFound:
            image_body = resp_body
            if not image_body:
                if hasattr(context, 'data'):
                    image_body = context.data
                elif isinstance(context, FSImage):
                    image_body = context._readFile(True)

            if image_body:
                resp_body = utils.image_to_grayscale(image_body, path)
            else:
                log.debug('Image doesn\'t contain any data: %s' % (path))

            if queryUtility(IResourceDirectory, name=u''):
                utils.store_resource(path, resp_body)

    elif IBrowserView.providedBy(request.get('PUBLISHED')) or \
        content_type in ['text/html', 'text/css'] or \
        isinstance(context, (File, FSFile, ATFile, FSPageTemplate)) and \
            context.content_type.split(';')[0] in ['text/html', 'text/css']:

        if hasattr(aq_base(context), 'data'):
            resp_body = context.data
            if hasattr(resp_body, 'data'):
                resp_body = resp_body.data

        if isinstance(context, FSFile):
            resp_body = context._readFile(0)

        if content_type == 'text/css' or \
           isinstance(context, (File, FSFile, ATFile, FSPageTemplate)) and \
           context.content_type.split(';')[0] == 'text/css':

            try:
                resp_body = utils.get_resource(request, response, filename)
            except (NotFound, AttributeError):
                resp_body = utils.transform_style_properties(resp_body)
                resp_body = utils.transform_css_url(resp_body)
                if queryUtility(IResourceDirectory, name=u''):
                    utils.store_resource(filename, resp_body)
        else:
            if not resp_body:
                rendered_body = utils.render_object_html(context, request)
                if rendered_body:
                    resp_body = rendered_body
            resp_body = utils.add_bodyclass(resp_body)
            resp_body = utils.transform_style_properties(resp_body)
            resp_body = utils.transform_css_url(resp_body)
            resp_body = utils.transform_img_src(resp_body)

    response.setBody(resp_body)