コード例 #1
0
ファイル: views.py プロジェクト: n3storm/django-ckeditor
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            pass

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(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))
コード例 #2
0
ファイル: views.py プロジェクト: sixthgear/django-ckeditor
def upload(request, image=False):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    # check for image
    if image and is_image(upload.name):

        #Verify that file is a valid image
        backend = image_processing.get_backend()

        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            return HttpResponse("""
                <script type='text/javascript'>
                    alert('Invalid image file.')
                    window.parent.CKEDITOR.tools.callFunction({0});
                </script>""".format(request.GET['CKEditorFuncNum']))

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user, prefix='images')
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(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))

    # check for document
    elif image != True and is_document(upload.name):
        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user, prefix='documents')
        saved_path = default_storage.save(upload_filename, upload)
        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))

    # bad file format
    else:
        return HttpResponse("""
            <script type='text/javascript'>
                alert('Invalid file format.')
                window.parent.CKEDITOR.tools.callFunction({0});
            </script>""".format(request.GET['CKEditorFuncNum']))
コード例 #3
0
ファイル: views.py プロジェクト: ulexy/ulexy
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            return HttpResponse("""
                       <script type='text/javascript'>
                            alert('Invalid image')
                            window.parent.CKEDITOR.tools.callFunction({0});
                       </script>""".format(request.GET['CKEditorFuncNum']))

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)
        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(saved_path)

        # url = utils.get_media_url(saved_path)
        url = "/%s" %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))
コード例 #4
0
ファイル: views.py プロジェクト: Nando-bog/django-ckeditor
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    backend = image_processing.get_backend()
    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
コード例 #5
0
ファイル: views.py プロジェクト: nngroup/django-ckeditor
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    backend = image_processing.get_backend()
    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
コード例 #6
0
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that file is a valid image
    backend = image_processing.get_backend()
    upload_filename = get_upload_filename(upload.name, request.user)
    
    if hasattr(settings, "CKEDITOR_RESIZE_RESOLUTION"):
        RESIZE_IMAGE = getattr(settings, "CKEDITOR_RESIZE_RESOLUTION")
        try:
            upload = backend.resize_image(upload, upload_filename)
        except IOError:
            return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid image')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))
    else:
        try:
            backend.image_verify(upload)
        except IOError:
            return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid image')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))
        

    # Open output file in which to store upload.
    #resized_image = backend.image_verify(upload)
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)
    

    #default_storage.delete()
    
    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
コード例 #7
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")
コード例 #8
0
 def handle_noargs(self, **options):
     if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
         backend = get_backend()
         for image in get_image_files():
             if not os.path.isfile(get_thumb_filename(image)):
                 self.stdout.write("Creating thumbnail for {0}".format(image))
                 try:
                     backend.create_thumbnail(image)
                 except Exception as e:
                     self.stdout.write("Couldn't create thumbnail for {0}: {1}".format(image, e))
         self.stdout.write("Finished")
     else:
         self.stdout.write("No thumbnail backend is enabled")
コード例 #9
0
 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")
コード例 #10
0
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that file is a valid image
    backend = image_processing.get_backend()
    try:
        backend.image_verify(upload)
    except utils.NotAnImageException:
        is_not_image = True
    else:
        is_not_image = False

    if is_not_image:
        backend = file_processing.get_backend()
        try:
            backend.file_verify(upload)
        except utils.NotAnPermittedFileTypeException:
            return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid file type or invalid image')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
コード例 #11
0
ファイル: views.py プロジェクト: happylyang/django-ckeditor
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            return HttpResponse("""
                       <script type='text/javascript'>
                            alert('Invalid image')
                            window.parent.CKEDITOR.tools.callFunction({0});
                       </script>""".format(request.GET['CKEditorFuncNum']))

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(saved_path)

        url = utils.get_media_url(saved_path)

        from qiniu import Auth
        from qiniu import put_file
        from utility import constants
        access_key = constants.QINIU_API
        secret_key = constants.QINIU_SK
        q = Auth(access_key, secret_key)
        bucket_name = "excake"
        token = q.upload_token(bucket_name)
        mime_type = "image/jpeg"
        ret, info = put_file(token, "/".join(url.split('/')[3:]), default_storage.path(upload_filename), mime_type=mime_type)

        # 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))
コード例 #12
0
ファイル: views.py プロジェクト: lDuffy/django-ckeditor
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that file is a valid image
    backend = image_processing.get_backend()
    name = upload.name.lower()
    extension = name[name.rfind('.')+1:]
    s = "jpg jpeg png"
    if s.find(extension) == -1:
        return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid image. Please use .jpg or .png')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))