Esempio n. 1
0
    def redactor_upload(self, request, file_type):
        if not self.has_change_permission(request):
            return HttpResponseForbidden("You do not have permission to upload this file.")

        if request.method != "POST":
            return HttpResponseNotAllowed(["POST"])

        image_content_types = ["image/gif", "image/jpeg", "image/png", "image/bmp"]

        try:

            if file_type == "image":
                if request.FILES.getlist("file")[0].content_type not in image_content_types:
                    raise Exception()

            new_file = File(title=request.FILES.getlist("file")[0].name, file=request.FILES.getlist("file")[0])
            new_file.save()

            if file_type == "image":
                return HttpResponse(json.dumps({"filelink": permalinks.create(new_file)}))
            else:
                return HttpResponse(
                    json.dumps(
                        {"filelink": permalinks.create(new_file), "filename": request.FILES.getlist("file")[0].name}
                    )
                )

        except:
            return HttpResponse("")
Esempio n. 2
0
    def save(self, user):
        from cms.apps.media.models import File

        # Get the related class
        (app_label, model_name) = self.cleaned_data["model"].split(".")
        related_class = get_model(app_label, model_name)

        # Check permissions
        change_perm = "%s.%s" % (app_label, related_class._meta.get_change_permission())
        if not user.has_perm(change_perm):
            raise PermissionDenied
        # Create a new file
        file = File()
        file.filename.save(self.cleaned_data["filefile"].name, self.cleaned_data["filefile"], save=False)
        file.save()

        # Attach file to the related object
        related_obj = related_class.objects.get(pk=self.cleaned_data["id"])
        related_obj.files.add(file)
        return (related_obj, file)
Esempio n. 3
0
    def test_setting_value(self):
        setting = Setting(
            type='string',
            string='Testing',
        )
        self.assertEquals(setting.value, 'Testing')

        setting = Setting(
            type='text',
            text='Line\nbreak',
        )
        self.assertEquals(setting.value, 'Line<br />break')
        # Ensure it's marked as safe
        self.assertIsInstance(setting.value, SafeString)

        setting = Setting(
            type='number',
            number=1,
        )
        self.assertEquals(setting.value, 1)

        setting = Setting(
            type='html',
            html='<p>Hi!</p>',
        )
        self.assertEquals(setting.value, '<p>Hi!</p>')
        self.assertIsInstance(setting.value, SafeString)

        fake_file = File(
            title='hi',
        )
        fake_file.id = 1

        setting = Setting(
            type='image',
            image=fake_file,
        )
        self.assertEquals(setting.value, fake_file)
        self.assertIsInstance(setting.value, File)
Esempio n. 4
0
    def redactor_upload(self, request, file_type):
        if not self.has_change_permission(request):
            return HttpResponseForbidden("You do not have permission to upload this file.")

        if request.method != 'POST':
            return HttpResponseNotAllowed(['POST'])

        image_content_types = [
            'image/gif',
            'image/jpeg',
            'image/png',
            'image/bmp'
        ]

        try:

            if file_type == 'image':
                if request.FILES.getlist('file')[0].content_type not in image_content_types:
                    raise Exception()

            new_file = File(
                title=request.FILES.getlist('file')[0].name,
                file=request.FILES.getlist('file')[0]
            )
            new_file.save()

            if file_type == 'image':
                return HttpResponse(json.dumps({
                    'filelink': permalinks.create(new_file)
                }))
            else:
                return HttpResponse(json.dumps({
                    'filelink': permalinks.create(new_file),
                    'filename': request.FILES.getlist('file')[0].name,
                }))

        except:
            return HttpResponse('')
Esempio n. 5
0
    def redactor_upload(self, request, file_type):
        if not self.has_change_permission(request):
            return HttpResponseForbidden(
                "You do not have permission to upload this file.")

        if request.method != 'POST':
            return HttpResponseNotAllowed(['POST'])

        image_content_types = [
            'image/gif', 'image/jpeg', 'image/png', 'image/bmp'
        ]

        try:

            if file_type == 'image':
                if request.FILES.getlist(
                        'file')[0].content_type not in image_content_types:
                    raise Exception()

            new_file = File(title=request.FILES.getlist('file')[0].name,
                            file=request.FILES.getlist('file')[0])
            new_file.save()

            if file_type == 'image':
                return HttpResponse(
                    json.dumps({'filelink': permalinks.create(new_file)}))
            else:
                return HttpResponse(
                    json.dumps({
                        'filelink':
                        permalinks.create(new_file),
                        'filename':
                        request.FILES.getlist('file')[0].name,
                    }))

        except:
            return HttpResponse('')