Exemple #1
0
    def post(self, request, *args, **kwargs):
        field_component = self.parse_field_component(
            request.POST.get('fq', None))
        if not field_component:
            return

        valid, field_value, errors = self.validate_fq(field_component)
        if valid:
            form = TemporaryFileForm(field_value, request.POST, request.FILES)
            if form.is_valid():
                instance = form.save()
                is_image_type = allowed_type(
                    instance.content_type,
                    ThorFileField.handle_allowed_types(['type:image']))

                upload_icon = '<i class="fa fa-file"></i>'
                if field_value.get_upload_image is not None:
                    if callable(field_value.get_upload_image):
                        upload_icon = field_value.get_upload_image(
                            instance.file.path)
                    else:
                        upload_icon = force_text(field_value.get_upload_image)

                upload_url = instance.file.url
                if field_value.get_upload_image_url is not None:
                    if callable(field_value.get_upload_image_url):
                        upload_icon = field_value.get_upload_image_url(
                            instance.file.url)
                    else:
                        upload_icon = force_text(
                            field_value.get_upload_image_url)

                return self.json_response({
                    'success': True,
                    'file': {
                        'id': instance.id,
                        'md5sum': instance.md5sum,
                        'url': upload_url,
                        'file_name': instance.file.name,
                        'instance_type': 'image' if is_image_type else 'file',
                        'upload_icon': upload_icon,
                    },
                })
            else:
                for field, error_val in form.errors.items():
                    for error_txt in error_val:
                        errors += [force_text(error_txt)]

        return self.json_response(
            {
                'success': False,
                'errors': force_text(errors[0])
            }, status=403)
Exemple #2
0
    def post(self, request, *args, **kwargs):
        field_component = self.parse_field_component(request.POST.get('fq', None))
        if not field_component:
            return

        valid, field_value, errors = self.validate_fq(field_component)
        if valid:
            form = TemporaryFileForm(field_value, request.POST, request.FILES)
            if form.is_valid():
                instance = form.save()
                is_image_type = allowed_type(instance.content_type, ThorFileField.handle_allowed_types(['type:image']))

                upload_icon = '<i class="fa fa-file"></i>'
                if field_value.get_upload_image is not None:
                    if callable(field_value.get_upload_image):
                        upload_icon = field_value.get_upload_image(instance.file.path)
                    else:
                        upload_icon = force_text(field_value.get_upload_image)

                upload_url = instance.file.url
                if field_value.get_upload_image_url is not None:
                    if callable(field_value.get_upload_image_url):
                        upload_icon = field_value.get_upload_image_url(instance.file.url)
                    else:
                        upload_icon = force_text(field_value.get_upload_image_url)

                return self.json_response({
                    'success': True,
                    'file': {
                        'id': instance.id,
                        'md5sum': instance.md5sum,
                        'url': upload_url,
                        'file_name': instance.file.name,
                        'instance_type': 'image' if is_image_type else 'file',
                        'upload_icon': upload_icon,
                    },
                })
            else:
                for field, error_val in form.errors.items():
                    for error_txt in error_val:
                        errors += [force_text(error_txt)]

        return self.json_response({
            'success': False,
            'errors': force_text(errors[0])
        }, status=403)
Exemple #3
0
    def post(self, request, *args, **kwargs):
        field_component = self.parse_field_component(request.POST.get("fq", None))
        if not field_component:
            return

        valid, field_value, errors = self.validate_fq(field_component)
        if valid:
            form = TemporaryFileForm(field_value, request.POST, request.FILES)
            if form.is_valid():
                instance = form.save()
                is_image_type = allowed_type(instance.content_type, ThorFileField.handle_allowed_types(["type:image"]))

                upload_icon = '<i class="fa fa-file"></i>'
                if field_value.get_upload_image is not None:
                    if callable(field_value.get_upload_image):
                        upload_icon = field_value.get_upload_image(instance.file.path)
                    else:
                        upload_icon = force_text(field_value.get_upload_image)

                upload_url = instance.file.url
                if field_value.get_upload_image_url is not None:
                    if callable(field_value.get_upload_image_url):
                        upload_icon = field_value.get_upload_image_url(instance.file.url)
                    else:
                        upload_icon = force_text(field_value.get_upload_image_url)

                return self.json_response(
                    {
                        "success": True,
                        "file": {
                            "id": instance.id,
                            "md5sum": instance.md5sum,
                            "url": upload_url,
                            "file_name": instance.file.name,
                            "instance_type": "image" if is_image_type else "file",
                            "upload_icon": upload_icon,
                        },
                    }
                )
            else:
                for field, error_val in form.errors.items():
                    for error_txt in error_val:
                        errors += [force_text(error_txt)]

        return self.json_response({"success": False, "errors": force_text(errors[0])}, status=403)
Exemple #4
0
    def get_is_image(self, value):
        if self.is_image:
            # Don't use the file type guesser if it's a image.
            return True

        if value:
            from upthor.fields import ThorFileField

            if hasattr(value, 'instance') and isinstance(value.instance, TemporaryFileWrapper):
                # Case 1: Temporary-file in form.
                return allowed_type(value.instance.content_type, ThorFileField.handle_allowed_types(['type:image']))
            elif hasattr(value, "url"):
                # Case 2: Pre existing linked-file in form.
                func = getattr(value.instance, 'is_upthor_image', None)

                if func and callable(func):
                    return func(self.get_fq())
                else:
                    return bool(func)

        return False
Exemple #5
0
    def get_is_image(self, value):
        if self.is_image:
            # Don't use the file type guesser if it's a image.
            return True

        if value:
            from upthor.fields import ThorFileField

            if hasattr(value, 'instance') and isinstance(
                    value.instance, TemporaryFileWrapper):
                # Case 1: Temporary-file in form.
                return allowed_type(
                    value.instance.content_type,
                    ThorFileField.handle_allowed_types(['type:image']))
            elif hasattr(value, "url"):
                # Case 2: Pre existing linked-file in form.
                func = getattr(value.instance, 'is_upthor_image', None)

                if func and callable(func):
                    return func(self.get_fq())
                else:
                    return bool(func)

        return False