Exemplo n.º 1
0
    def clean_logo_file(self):
        ALLOWED_LOGO_EXT = ('.jpg', '.jpeg', '.gif', '.png')

        logo_file = self.cleaned_data['logo_file']
        if logo_file:
            try:
                extension = splitext(logo_file.name)[1]

                # check the extension
                if extension.lower() not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(
                        _('The logo must be of jpg, gif, or png image type.'))

                # check the image header
                image_type = '.%s' % imghdr.what('', logo_file.read())
                if image_type not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(
                        _('The logo is an invalid image. Try uploading another logo.'
                          ))

                max_upload_size = get_max_file_upload_size()
                if logo_file.size > max_upload_size:
                    raise forms.ValidationError(
                        _('Please keep filesize under %(max_upload_size)s. Current filesize %(logo_size)s'
                          ) %
                        {
                            'max_upload_size': filesizeformat(max_upload_size),
                            'logo_size': filesizeformat(logo_file.size)
                        })
            except IOError:
                logo_file = None

        return logo_file
Exemplo n.º 2
0
    def clean_photo_upload(self):
        photo_upload = self.cleaned_data['photo_upload']
        if photo_upload:
            extension = splitext(photo_upload.name)[1]

            # check the extension
            if extension.lower() not in ALLOWED_LOGO_EXT:
                raise forms.ValidationError(
                    _('The photo must be of jpg, gif, or png image type.'))

            # check the image header
            image_type = '.%s' % imghdr.what('', photo_upload.read())
            if image_type not in ALLOWED_LOGO_EXT:
                raise forms.ValidationError(
                    _('The photo is an invalid image. Try uploading another photo.'
                      ))

            max_upload_size = get_max_file_upload_size()
            if photo_upload.size > max_upload_size:
                raise forms.ValidationError(
                    _('Please keep filesize under %(max_upload_size)s. Current filesize %(upload_size)s'
                      ) % {
                          'max_upload_size': filesizeformat(max_upload_size),
                          'upload_size': filesizeformat(photo_upload.size)
                      })

        return photo_upload
Exemplo n.º 3
0
    def clean_header_image(self):
        header_image = self.cleaned_data["header_image"]
        if header_image:
            extension = splitext(header_image.name)[1]

            # check the extension
            if extension.lower() not in ALLOWED_IMG_EXT:
                raise forms.ValidationError(_("The header image must be of jpg, gif, or png image type."))

            # check the image header_image
            image_type = ".%s" % imghdr.what("", header_image.read())
            if image_type not in ALLOWED_IMG_EXT:
                raise forms.ValidationError(_("The header image is an invalid image. Try uploading another image."))

            max_upload_size = get_max_file_upload_size()
            if header_image.size > max_upload_size:
                raise forms.ValidationError(
                    _("Please keep filesize under %(max_upload_size)s. Current filesize %(header_image)s")
                    % {
                        "max_upload_size": filesizeformat(max_upload_size),
                        "header_image": filesizeformat(header_image.size),
                    }
                )

        return header_image
Exemplo n.º 4
0
    def clean_header_image(self):
        header_image = self.cleaned_data['header_image']
        if header_image:
            extension = splitext(header_image.name)[1]

            # check the extension
            if extension.lower() not in ALLOWED_IMG_EXT:
                raise forms.ValidationError(
                    _('The header image must be of jpg, gif, or png image type.'
                      ))

            # check the image header_image
            image_type = '.%s' % imghdr.what('', header_image.read())
            if image_type not in ALLOWED_IMG_EXT:
                raise forms.ValidationError(
                    _('The header image is an invalid image. Try uploading another image.'
                      ))

            max_upload_size = get_max_file_upload_size()
            if header_image.size > max_upload_size:
                raise forms.ValidationError(
                    _('Please keep filesize under %(max_upload_size)s. Current filesize %(header_image)s'
                      ) % {
                          'max_upload_size': filesizeformat(max_upload_size),
                          'header_image': filesizeformat(header_image.size)
                      })

        return header_image
Exemplo n.º 5
0
    def clean_logo(self):
        logo = self.cleaned_data["logo"]
        if logo:
            try:
                extension = splitext(logo.name)[1]

                # check the extension
                if extension.lower() not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(_("The logo must be of jpg, gif, or png image type."))

                # check the image header
                image_type = ".%s" % imghdr.what("", logo.read())
                if image_type not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(_("The logo is an invalid image. Try uploading another logo."))

                max_upload_size = get_max_file_upload_size()
                if logo.size > max_upload_size:
                    raise forms.ValidationError(
                        _("Please keep filesize under %(max_upload_size)s. Current filesize %(logo_size)s")
                        % {"max_upload_size": filesizeformat(max_upload_size), "logo_size": filesizeformat(logo.size)}
                    )
            except IOError:
                logo = None

        return logo
Exemplo n.º 6
0
    def clean_photo_upload(self):
        photo_upload = self.cleaned_data['photo_upload']
        if photo_upload:
            extension = splitext(photo_upload.name)[1]

            # check the extension
            if extension.lower() not in ALLOWED_LOGO_EXT:
                raise forms.ValidationError(
                    _('The photo must be of jpg, gif, or png image type.'))

            # check the image header
            image_type = '.%s' % imghdr.what('', photo_upload.read())
            if image_type not in ALLOWED_LOGO_EXT:
                raise forms.ValidationError(
                    _('The photo is an invalid image. Try uploading another photo.'
                      ))

            max_upload_size = get_max_file_upload_size()
            if photo_upload.size > max_upload_size:
                raise forms.ValidationError(
                    _('Please keep filesize under %(max_upload_size)s. Current filesize %(upload_size)s'
                      ) % {
                          'max_upload_size': filesizeformat(max_upload_size),
                          'upload_size': filesizeformat(photo_upload.size)
                      })

        return photo_upload
Exemplo n.º 7
0
    def clean_logo(self):
        logo = self.cleaned_data['logo']
        if logo:
            try:
                extension = splitext(logo.name)[1]

                # check the extension
                if extension.lower() not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(
                        self.logo_extension_error_message)

                # check the image header
                image_type = '.%s' % imghdr.what('', logo.read())
                if image_type not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(self.logo_mime_error_message)

                max_upload_size = get_max_file_upload_size()
                if logo.size > max_upload_size:
                    raise forms.ValidationError(
                        _('Please keep filesize under %(max_upload_size)s. Current filesize %(logo_size)s'
                          ) %
                        {
                            'max_upload_size': filesizeformat(max_upload_size),
                            'logo_size': filesizeformat(logo.size)
                        })
            except IOError:
                logo = None

        return logo
Exemplo n.º 8
0
    def clean_logo_file(self):
        ALLOWED_LOGO_EXT = (
            '.jpg',
            '.jpeg',
            '.gif',
            '.png'
        )

        logo_file = self.cleaned_data['logo_file']
        if logo_file:
            try:
                extension = splitext(logo_file.name)[1]

                # check the extension
                if extension.lower() not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(_('The logo must be of jpg, gif, or png image type.'))

                # check the image header
                image_type = '.%s' % imghdr.what('', logo_file.read())
                if image_type not in ALLOWED_LOGO_EXT:
                    raise forms.ValidationError(_('The logo is an invalid image. Try uploading another logo.'))

                max_upload_size = get_max_file_upload_size()
                if logo_file.size > max_upload_size:
                    raise forms.ValidationError(_('Please keep filesize under %(max_upload_size)s. Current filesize %(logo_size)s') % {
                                                    'max_upload_size': filesizeformat(max_upload_size),
                                                    'logo_size': filesizeformat(logo_file.size)})
            except IOError:
                logo_file = None

        return logo_file
Exemplo n.º 9
0
 def __init__(self, *args, **kwargs):
     self.allowed_extensions = kwargs.pop('allowed_extensions',
                                          get_allowed_upload_file_exts())
     self.allowed_mimetypes = kwargs.pop(
         'allowed_mimetypes',
         get_allowed_mimetypes(self.allowed_extensions))
     self.max_size = kwargs.pop('max_size',
                                get_max_file_upload_size(file_module=True))
Exemplo n.º 10
0
    def clean_file(self):
        data = self.cleaned_data.get('file')
        max_upload_size = get_max_file_upload_size(file_module=True)
        if data.size > max_upload_size:
            raise forms.ValidationError(_('Please keep filesize under %(max_upload_size)s. Current filesize %(data_size)s') % {
                                            'max_upload_size': filesizeformat(max_upload_size),
                                            'data_size': filesizeformat(data.size)})

        return data
Exemplo n.º 11
0
    def get_context_data(self, **kwargs):
        context = super(FileTinymceCreateView, self).get_context_data(**kwargs)
        context['app_label'] = self.request.GET.get('app_label', '')
        context['model'] = self.request.GET.get('model', '')
        context['object_id'] = self.request.GET.get('object_id', 0)
        context['max_file_size'] = get_max_file_upload_size(file_module=True)
        context['upload_type'] = self.request.GET.get('type', '')
        context['accept_file_types'] = '|'.join(x[1:] for x in get_allowed_upload_file_exts(context['upload_type']))

        return context
Exemplo n.º 12
0
 def __init__(self, *args, **kwargs):
     self.allowed_extensions = kwargs.pop('allowed_extensions', get_allowed_upload_file_exts())
     self.allowed_mimetypes = kwargs.pop('allowed_mimetypes', get_allowed_mimetypes(self.allowed_extensions))
     self.max_size = kwargs.pop('max_size', get_max_file_upload_size(file_module=True))