def handle(self, *args, **options):
     if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
         backend = registry.get_backend()
         for image in get_image_files():
             if not self._thumbnail_exists(image):
                 self.stdout.write("Creating thumbnail for %s" % image)
                 try:
                     backend.create_thumbnail(image)
                 except Exception as e:
                     self.stdout.write("Couldn't create thumbnail for %s: %s" % (image, e))
         self.stdout.write("Finished")
     else:
         self.stdout.write("No thumbnail backend is enabled")
Example #2
0
 def handle(self, *args, **options):
     if getattr(settings, "CKEDITOR_IMAGE_BACKEND", None):
         backend = registry.get_backend()
         for image in get_image_files():
             if not self._thumbnail_exists(image):
                 self.stdout.write("Creating thumbnail for %s" % image)
                 try:
                     backend.create_thumbnail(image)
                 except Exception as e:
                     self.stdout.write(
                         f"Couldn't create thumbnail for {image}: {e}")
         self.stdout.write("Finished")
     else:
         self.stdout.write("No thumbnail backend is enabled")
Example #3
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES["upload"]

        backend = registry.get_backend()

        ck_func_num = request.GET.get("CKEditorFuncNum")
        if ck_func_num:
            ck_func_num = escape(ck_func_num)

        filewrapper = backend(storage, uploaded_file)
        allow_nonimages = getattr(settings, "CKEDITOR_ALLOW_NONIMAGE_FILES", True)
        # Throws an error when an non-image file are uploaded.
        if not filewrapper.is_image and not allow_nonimages:
            return HttpResponse(
                """
                <script type='text/javascript'>
                window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.');
                </script>""".format(
                    ck_func_num
                )
            )

        filepath = get_upload_filename(uploaded_file.name, request)

        saved_path = filewrapper.save_as(filepath)

        url = utils.get_media_url(saved_path)

        if ck_func_num:
            # Respond with Javascript sending ckeditor upload url.
            return HttpResponse(
                """
            <script type='text/javascript'>
                window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
            </script>""".format(
                    ck_func_num, url
                )
            )
        else:
            _, filename = os.path.split(saved_path)
            retdata = {"url": url, "uploaded": "1", "fileName": filename}
            return JsonResponse(retdata)
Example #4
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = registry.get_backend()

        ck_func_num = request.GET.get('CKEditorFuncNum')
        if ck_func_num:
            ck_func_num = escape(ck_func_num)

        filewrapper = backend(storage, uploaded_file)
        allow_nonimages = getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True)
        # Throws an error when an non-image file are uploaded.
        if not filewrapper.is_image and not allow_nonimages:
            return HttpResponse("""
                <script type='text/javascript'>
                window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.');
                </script>""".format(ck_func_num))

        filepath = get_upload_filename(uploaded_file.name, request.user)

        saved_path = filewrapper.save_as(filepath)

        url = utils.get_media_url(saved_path)

        if ck_func_num:
            # Respond with Javascript sending ckeditor upload url.
            return HttpResponse("""
            <script type='text/javascript'>
                window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
            </script>""".format(ck_func_num, url))
        else:
            _, filename = os.path.split(saved_path)
            retdata = {'url': url, 'uploaded': '1',
                       'fileName': filename}
            return JsonResponse(retdata)
Example #5
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.

        Exactly the same as in django-ckeditor 5.0.3 except that creates
        absolute URLs instead of relative ones.
        """
        uploaded_file = request.FILES['upload']

        backend = registry.get_backend()
        ck_func_num = escape(request.GET['CKEditorFuncNum'])

        # Throws an error when an non-image file are uploaded.
        if not getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True):
            try:
                backend.image_verify(uploaded_file)
            except utils.NotAnImageException:
                return HttpResponse("""
                    <script type='text/javascript'>
                    window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.');
                    </script>""".format(ck_func_num))

        section_file = self._save_file(request, uploaded_file)

        url = reverse('serve_file',
                      kwargs={
                          'filetype': 'sectionfile',
                          'pk': section_file.pk
                      })
        url = request.build_absolute_uri(url)

        # Respond with Javascript sending ckeditor upload url.
        return HttpResponse("""
        <script type='text/javascript'>
            window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
        </script>""".format(ck_func_num, url))