Exemplo n.º 1
0
    def getImage(self, value):
        widget = self.widget
        req = widget.request

        if isinstance(value, FileUpload):

            filename = safe_basename(value.filename)

            if filename is not None and not isinstance(filename, unicode):
                # Work-around for
                # https://bugs.launchpad.net/zope2/+bug/499696
                filename = filename.decode('utf-8')

            value.seek(0)
            data = value.read()
            if data or filename:
                value = self.field._type(data=data, filename=filename)
            else:
                return self.get_missing_image()

        else:
            args = {}
            if req.get(widget.name + '.filename'):
                filename = req.get(widget.name + '.filename')
                if type(filename) in (list, set, tuple) and filename:
                    filename = filename[0]
                if not isinstance(filename, unicode):
                    filename = filename.decode('utf8')
                args['filename'] = filename
            if isinstance(value, basestring):
                if value.startswith('reference:'):
                    reference = value.replace('reference:', '')
                    obj = uuidToObject(reference)
                    if obj:
                        try:
                            try:
                                ct = obj.image.contentType
                            except AttributeError:
                                ct = None
                            value = NamedBlobImage(contentType=ct)
                            alsoProvides(value, IReferenceNamedImage)
                            value.reference = reference
                        except AttributeError:
                            pass
                elif value.startswith('data:'):
                    value = b64decode(value.split(',')[-1])
                    value = self.field._type(data=value, **args)
            else:
                try:
                    value = self.field._type(data=str(value), **args)
                except UnicodeEncodeError:
                    value = self.field._type(data=value.encode('utf8'), **args)

        return value
Exemplo n.º 2
0
def check_lead_image(obj, request=None):
    original_obj = obj
    if request is not None:
        # if request is passed in, we check for current draft to use...
        draft = getCurrentDraft(request)
        if draft is not None:
            obj = draft
    if has_image(obj):
        return
    image = find_image(obj)
    if image is not None:
        try:
            try:
                ct = image.image.contentType
            except AttributeError:
                ct = None
            value = NamedBlobImage(contentType=ct)
            alsoProvides(value, IReferenceNamedImage)
            value.reference = IUUID(image)
            original_obj.image = value
        except AttributeError:
            pass