Example #1
0
    def _validate(self, value):
        if (IFileDataNoValue.providedBy(value) or not value) and self.required:
            raise RequiredMissing()

        if IFileDataClear.providedBy(value) or \
                IFileDataNoValue.providedBy(value):
            return

        super(ImageField, self)._validate(value)

        if not (IImage.providedBy(value) or IFileData.providedBy(value)):
            raise WrongType(value, (IImage, IFileData))

        if self.mimeTypes:
            if value.mimeType not in self.mimeTypes:
                raise NotAllowedFileType(value.mimeType, self.mimeTypes)

        if not self.scale and (self.maxWidth > 0 or self.maxHeight > 0):
            if IImage.providedBy(value):
                width, height = value.width, value.height
            else:
                width, height = getImageSize(value.data)

            if self.maxWidth > 0 and width > self.maxWidth:
                raise ImageDimensionExceeded(width, ('width', self.maxWidth))

            if self.maxHeight > 0 and height > self.maxHeight:
                raise ImageDimensionExceeded(
                    height, ('height', self.maxHeight))
Example #2
0
    def set(self, object, value, _getattr=getattr, _setattr=setattr):
        if self.readonly:
            raise TypeError("Can't set values on read-only fields "
                            "(name=%s, class=%s.%s)"
                            % (self.__name__,
                               object.__class__.__module__,
                               object.__class__.__name__))

        if IFile.providedBy(value):
            _setattr(object, self.__name__, value)

        elif IFileData.providedBy(value):
            data = _getattr(object, self.__name__, None)
            if not IFile.providedBy(data):
                data = File()
                data.data = value.data
                data.mimeType = value.mimeType
                data.filename = value.filename
                data.disablePreview = value.disablePreview
                data.disablePrint = value.disablePrint
                _setattr(object, self.__name__, data)
            else:
                data = removeSecurityProxy(data)
                data.data = value.data
                data.mimeType = value.mimeType
                data.filename = value.filename
                data.disablePreview = value.disablePreview
                data.disablePrint = value.disablePrint
                _setattr(object, self.__name__, data)
                notify(ObjectModifiedEvent(data))

        elif IFileDataClear.providedBy(value):
            data = _getattr(object, self.__name__, None)
            if IFile.providedBy(data):
                data.clear()
            else:
                _setattr(object, self.__name__, File())

        elif IFileDataNoValue.providedBy(value):
            pass
        else:
            self.set(object, FileData(value), _getattr, _setattr)
Example #3
0
    def toFieldValue(self, value):
        value.disablePreview = False
        value.disablePrint = False
        if getattr(self.widget, 'disable_preview', None) is not None:
            if 'true' in self.widget.disable_preview_widget.value:
                value.disablePreview = True
        if getattr(self.widget, 'disable_print', None) is not None:
            if 'true' in self.widget.disable_print_widget.value:
                value.disablePrint = True

        if IFileData.providedBy(value) or IFileDataClear.providedBy(value) or \
           IFileDataNoValue.providedBy(value) or IFile:
            return value

        if value is None or value == '':
            # When no new file is uploaded, send a signal that we do not want
            # to do anything special.
            return interfaces.NOT_CHANGED

        return FileData(value)
Example #4
0
    def set(self, value):
        """See z3c.form.interfaces.IDataManager"""
        if self.field.readonly:
            raise TypeError("Can't set values on read-only fields "
                            "(name=%s, class=%s.%s)"
                            % (self.field.__name__,
                               self.context.__class__.__module__,
                               self.context.__class__.__name__))

        if not (IFile.providedBy(value) or
                IFileData.providedBy(value) or
                IFileDataClear.providedBy(value)):
            return

        # get the right adapter or context
        context = removeSecurityProxy(self.context)
        if self.field.interface is not None:
            context = self.field.interface(context)

        field = self.field.bind(context)
        field.set(context, value)
Example #5
0
    def set(self, object, value, _getattr=getattr, _setattr=setattr):
        if self.readonly:
            raise TypeError("Can't set values on read-only fields "
                            "(name=%s, class=%s.%s)"
                            % (self.__name__,
                               object.__class__.__module__,
                               object.__class__.__name__))

        if IImage.providedBy(value):
            _setattr(object, self.__name__, value)
        elif IFileData.providedBy(value):
            data = _getattr(object, self.__name__, None)
            if not IImage.providedBy(data):
                data = Image()
                data.data = value.data
                data.mimeType = value.mimeType
                data.filename = value.filename
                _setattr(object, self.__name__, data)
            else:
                data = removeSecurityProxy(data)
                data.data = value.data
                data.mimeType = value.mimeType
                data.filename = value.filename
                _setattr(object, self.__name__, data)

            if self.scale and (self.maxWidth < data.width or
               self.maxHeight < data.height):
                data.scale(self.maxWidth, self.maxHeight)

        elif IFileDataClear.providedBy(value):
            data = _getattr(object, self.__name__, None)
            if IImage.providedBy(data):
                data.clear()
            else:
                _setattr(object, self.__name__, Image())
        elif IFileDataNoValue.providedBy(value):
            pass
        else:
            self.set(object, FileData(value), _getattr, _setattr)
Example #6
0
    def _validate(self, value):
        if (IFileDataNoValue.providedBy(value) or not value) and self.required:
            raise RequiredMissing()

        if IFileDataClear.providedBy(value) or \
           IFileDataNoValue.providedBy(value):
            return

        super(FileField, self)._validate(value)

        if not (IFile.providedBy(value) or IFileData.providedBy(value)):
            raise WrongType(value, (IFile, IFileData))

        if self.mimeTypes:
            mt1, mt2 = value.mimeType.split('/')

            sec = self.mts.get(mt1)
            if sec:
                if '*' in sec:
                    return
                elif mt2 in sec:
                    return

            raise NotAllowedFileType(value.mimeType, self.mimeTypes)