Example #1
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.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))

        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        url = utils.get_media_url(saved_path)

        # 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))
Example #2
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 = image_processing.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))

        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        url = utils.get_media_url(saved_path)

        # this is the only customization to this function
        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))
Example #3
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.get_backend()

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

        # 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))
        url = self._custom_save_file(request, uploaded_file, backend)
        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:
            retdata = {
                'url': url,
                'uploaded': '1',
                'fileName': uploaded_file.name
            }
            return JsonResponse(retdata)
Example #4
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        absolute_path = getattr(settings,
                                'CKEDITOR_UPLOADER_ABSOLUTE_PATH_PREFIX', '')
        uploaded_file = request.FILES['upload']

        backend = image_processing.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))

        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        url = utils.get_media_url(saved_path)

        # 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, absolute_path + url))
Example #5
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.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))

        # EPFL Storage
        from media import store_image_file
        filepath, image = store_image_file(uploaded_file, prefix_path='fckeditorimage', max_width=600) #@UnusedVariable image
        filename = os.path.basename(filepath)
        from django.contrib.sites.models import Site
        url = "http://" + Site.objects.get_current().domain + os.path.join(settings.MEDIA_URL, filepath)

        # 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))
Example #6
0
def create_thumbnail_callback(sender, instance, **kwargs):
    """
    Creates a thumbnail if it hasn't been created already by CKEditor or
    Django Filer
    """
    illustration_image = getattr(instance, 'image', None)
    if illustration_image:
        backend = image_processing.get_backend()
        ImageUploadView._create_thumbnail_if_needed(backend,
                                                    illustration_image.name)
 def handle_noargs(self, **options):
     if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
         backend = 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 #8
0
 def handle(self, *args, **options):
     if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
         backend = 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 #9
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.get_backend()

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

        # 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))

        if getattr(settings, 'CKEDITOR_UPLOAD_FILE_MAX_SIZE') is not None:
            if uploaded_file.size > settings.CKEDITOR_UPLOAD_FILE_MAX_SIZE:
                return HttpResponse("""
                    <script type='text/javascript'>
                    window.parent.CKEDITOR.tools.callFunction({0}, '', 'File max size is {1}.');
                    </script>""".format(
                    ck_func_num,
                    filesizeformat(settings.CKEDITOR_UPLOAD_FILE_MAX_SIZE)))

        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        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:
            retdata = {
                'url': url,
                'uploaded': '1',
                'fileName': uploaded_file.name
            }
            return JsonResponse(retdata)
Example #10
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.get_backend()
        self._verify_file(backend, uploaded_file)
        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        url = utils.get_media_url(saved_path)

        # Respond with Javascript sending ckeditor upload url.
        return HttpResponse("""
        <script type='text/javascript'>
            window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
        </script>""".format(request.GET['CKEditorFuncNum'], url))
Example #11
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        uploaded_file = request.FILES['upload']

        backend = image_processing.get_backend()
        self._verify_file(backend, uploaded_file)
        saved_path = self._save_file(request, uploaded_file)
        self._create_thumbnail_if_needed(backend, saved_path)
        url = utils.get_media_url(saved_path)

        # Respond with Javascript sending ckeditor upload url.
        return HttpResponse("""
        <script type='text/javascript'>
            window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
        </script>""".format(request.GET['CKEditorFuncNum'], url))
Example #12
0
 def post(self, request, **kwargs):
     """
     Uploads a file and send back its URL to CKEditor.
     """
     logger.info("Enter ImageUploadView.post")
     uploaded_file = request.FILES['upload']
     backend = image_processing.get_backend()
     logger.info("ImageUploadView.post _verify_file")
     self._verify_file(backend, uploaded_file)
     logger.info("ImageUploadView.post _verify_file returned")
     saved_name = self._save_file(request, uploaded_file)
     path = getattr(settings, 'CKEDITOR_UPLOAD_PATH', "/uploads")
     url = path + saved_name
     self._create_thumbnail_if_needed(backend, url)
     logger.info("Exit ImageUploadView.post")
     # Respond with Javascript sending ckeditor upload url.
     return HttpResponse("""
     <script type='text/javascript'>
         window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
     </script>""".format(request.GET['CKEditorFuncNum'], url))