예제 #1
0
파일: admin.py 프로젝트: jamesfoley/cms
    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("")
예제 #2
0
파일: admin.py 프로젝트: dan-gamble/cms
    def redactor_data(self, request, **kwargs):
        if not self.has_change_permission(request):
            return HttpResponseForbidden(
                "You do not have permission to view these files.")

        file_type = kwargs.get('file_type', 'files')
        page = kwargs.get('page', 1)

        # Make sure we serve the correct data
        if file_type == 'files':
            file_objects = File.objects.all()
        elif file_type == 'images':
            file_objects = File.objects.filter(
                file__regex=r'(?i)\.(png|gif|jpg|jpeg)$')

        # Sort objects by title
        file_objects = file_objects.order_by('title')

        # Create paginator
        paginator = Paginator(file_objects, 15)

        # Create files usable by the CMS
        json_data = {
            'page': page,
            'pages': paginator.page_range,
        }

        if file_type == 'files':
            json_data['objects'] = [{
                'title': file_object.title,
                'url': permalinks.create(file_object)
            } for file_object in paginator.page(page)]
        elif file_type == 'images':
            json_data['objects'] = [{
                'title':
                file_object.title,
                'url':
                permalinks.create(file_object),
                'thumbnail':
                get_thumbnail(file_object.file,
                              '100x75',
                              crop="center",
                              quality=99).url
            } for file_object in paginator.page(page)]

        # Return files as ajax
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
예제 #3
0
    def get_preview(self, obj):
        '''Generates a thumbnail of the image, falling back to an appropriate
        icon if it is not an image file or if thumbnailing fails.'''
        icon = obj.icon
        permalink = permalinks.create(obj)
        if obj.is_image():
            try:
                thumbnail = get_thumbnail(obj.file, '100x66', quality=99)
                return format_html(
                    '<img cms:permalink="{}" src="{}" width="{}" height="{}" alt="" title="{}"/>',
                    permalink,
                    thumbnail.url,
                    thumbnail.width,
                    thumbnail.height,
                    obj.title
                )

            # AttributeError will be raised if thumbnail returns None - the
            # others can be raised with bad files.
            except (IOError, TypeError, AttributeError):
                pass

        return format_html(
            '<img cms:permalink="{}" src="{}" width="56" height="66" alt="" title="{}"/>',
            permalink,
            icon,
            obj.title
        )
예제 #4
0
파일: admin.py 프로젝트: lewiscollard/cms
    def get_preview(self, obj):
        """Generates a thumbnail of the image."""
        _, extension = os.path.splitext(obj.file.name)
        extension = extension.lower()[1:]
        icon = FILE_ICONS.get(extension, UNKNOWN_FILE_ICON)
        permalink = permalinks.create(obj)
        if icon == IMAGE_FILE_ICON:
            try:
                thumbnail = get_thumbnail(obj.file, '100x66', quality=99)
            except IOError:
                pass
            else:
                try:
                    return '<img cms:permalink="{}" src="{}" width="{}" height="{}" alt="" title="{}"/>'.format(
                        permalink,
                        thumbnail.url,
                        thumbnail.width,
                        thumbnail.height,
                        obj.title
                    )
                except TypeError:
                    pass
        else:
            icon = staticfiles_storage.url(icon)

        return '<img cms:permalink="{}" src="{}" width="66" height="66" alt="" title="{}"/>'.format(
            permalink,
            icon,
            obj.title
        )
예제 #5
0
파일: admin.py 프로젝트: lewiscollard/cms
 def response_add(self, request, obj, *args, **kwargs):
     """Returns the response for a successful add action."""
     if "_redactor" in request.GET:
         context = {"permalink": permalinks.create(obj),
                    "title": obj.title}
         return render(request, "admin/media/file/filebrowser_add_success.html", context)
     return super(FileAdminBase, self).response_add(request, obj, *args, **kwargs)
예제 #6
0
파일: admin.py 프로젝트: dan-gamble/cms
    def get_preview(self, obj):
        """Generates a thumbnail of the image."""
        _, extension = os.path.splitext(obj.file.name)
        extension = extension.lower()[1:]
        icon = FILE_ICONS.get(extension, UNKNOWN_FILE_ICON)
        permalink = permalinks.create(obj)
        if icon == IMAGE_FILE_ICON:
            try:
                thumbnail = get_thumbnail(obj.file, '100x66', quality=99)
            except IOError:
                return '<img cms:permalink="{}" src="{}" width="66" height="66" alt="" title="{}"/>'.format(
                    permalink, icon, obj.title)
            else:
                try:
                    return '<img cms:permalink="{}" src="{}" width="{}" height="{}" alt="" title="{}"/>'.format(
                        permalink, thumbnail.url, thumbnail.width,
                        thumbnail.height, obj.title)
                except TypeError:
                    return '<img cms:permalink="{}" src="{}" width="66" height="66" alt="" title="{}"/>'.format(
                        permalink, icon, obj.title)
        else:
            icon = staticfiles_storage.url(icon)

        return '<img cms:permalink="{}" src="{}" width="66" height="66" alt="" title="{}"/>'.format(
            permalink, icon, obj.title)
예제 #7
0
 def response_add(self, request, obj, post_url_continue=None):
     '''Returns the response for a successful add action.'''
     if '_tinymce' in request.GET:
         context = {'permalink': permalinks.create(obj),
                    'title': obj.title}
         return render(request, 'admin/media/file/filebrowser_add_success.html', context)
     return super().response_add(request, obj, post_url_continue=post_url_continue)
예제 #8
0
파일: admin.py 프로젝트: abaelhe/cms
 def response_add(self, request, obj, *args, **kwargs):
     """Returns the response for a successful add action."""
     if "_tinymce" in request.GET:
         context = {"permalink": permalinks.create(obj), "title": obj.title}
         return render(request,
                       "admin/media/file/filebrowser_add_success.html",
                       context)
     return super(FileAdminBase, self).response_add(request, obj, *args,
                                                    **kwargs)
예제 #9
0
파일: admin.py 프로젝트: jamesfoley/cms
    def redactor_data(self, request, **kwargs):
        if not self.has_change_permission(request):
            return HttpResponseForbidden("You do not have permission to view these files.")

        file_type = kwargs.get("file_type", "files")
        page = kwargs.get("page", 1)

        # Make sure we serve the correct data
        if file_type == "files":
            file_objects = File.objects.all()
        elif file_type == "images":
            file_objects = File.objects.filter(file__regex=r"(?i)\.(png|gif|jpg|jpeg)$")

        # Sort objects by title
        file_objects = file_objects.order_by("title")

        # Create paginator
        paginator = Paginator(file_objects, 15)

        # Create files usable by the CMS
        json_data = {"page": page, "pages": paginator.page_range}

        if file_type == "files":
            json_data["objects"] = [
                {"title": file_object.title, "url": permalinks.create(file_object)}
                for file_object in paginator.page(page)
            ]
        elif file_type == "images":
            json_data["objects"] = [
                {
                    "title": file_object.title,
                    "url": permalinks.create(file_object),
                    "thumbnail": get_thumbnail(file_object.file, "100x75", crop="center", quality=99).url,
                }
                for file_object in paginator.page(page)
            ]

        # Return files as ajax
        return HttpResponse(json.dumps(json_data), content_type="application/json")
예제 #10
0
파일: admin.py 프로젝트: lewiscollard/cms
    def redactor_data(self, request, **kwargs):
        if not self.has_change_permission(request):
            return HttpResponseForbidden("You do not have permission to view these files.")

        file_type = kwargs.get('file_type', 'files')
        page = kwargs.get('page', 1)

        # Make sure we serve the correct data
        if file_type == 'files':
            file_objects = File.objects.all()
        elif file_type == 'images':
            file_objects = File.objects.filter(file__regex=r'(?i)\.(png|gif|jpg|jpeg)$')

        # Sort objects by title
        file_objects = file_objects.order_by('title')

        # Create paginator
        paginator = Paginator(file_objects, 15)

        # Create files usable by the CMS
        json_data = {
            'page': page,
            'pages': paginator.page_range,
        }

        if file_type == 'files':
            json_data['objects'] = [
                {'title': file_object.title, 'url': permalinks.create(file_object)}
                for file_object in paginator.page(page)
            ]
        elif file_type == 'images':
            json_data['objects'] = [
                {'title': file_object.title, 'url': permalinks.create(file_object), 'thumbnail': get_thumbnail(file_object.file, '100x75', crop="center", quality=99).url}
                for file_object in paginator.page(page)
            ]

        # Return files as ajax
        return HttpResponse(json.dumps(json_data), content_type='application/json')
예제 #11
0
파일: admin.py 프로젝트: lewiscollard/cms
    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('')
예제 #12
0
파일: admin.py 프로젝트: dan-gamble/cms
    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('')
예제 #13
0
파일: admin.py 프로젝트: JamesJGarner/cms
 def get_preview(self, obj):
     """Generates a thumbnail of the image."""
     _, extension = os.path.splitext(obj.file.name)
     extension = extension.lower()[1:]
     icon = FILE_ICONS.get(extension, UNKNOWN_FILE_ICON)
     permalink = permalinks.create(obj)
     if icon == IMAGE_FILE_ICON:
         try:
             thumbnail = optimizations.get_thumbnail(obj.file, 100, 66)
         except IOError:
             pass
         else:
             return '<img cms:permalink="%s" src="%s" width="%s" height="%s" alt="" title="%s"/>' % (permalink, thumbnail.url, thumbnail.width, thumbnail.height, obj.title)
     else:
         icon = optimizations.get_url(icon)
     return '<img cms:permalink="%s" src="%s" width="66" height="66" alt="" title="%s"/>' % (permalink, icon, obj.title)
예제 #14
0
파일: admin.py 프로젝트: abaelhe/cms
 def get_preview(self, obj):
     """Generates a thumbnail of the image."""
     _, extension = os.path.splitext(obj.file.name)
     extension = extension.lower()[1:]
     icon = FILE_ICONS.get(extension, UNKNOWN_FILE_ICON)
     permalink = permalinks.create(obj)
     if icon == IMAGE_FILE_ICON:
         try:
             thumbnail = optimizations.get_thumbnail(obj.file, 100, 66)
         except IOError:
             pass
         else:
             return '<img cms:permalink="%s" src="%s" width="%s" height="%s" alt="" title="%s"/>' % (
                 permalink, thumbnail.url, thumbnail.width,
                 thumbnail.height, obj.title)
     else:
         icon = optimizations.get_url(icon)
     return '<img cms:permalink="%s" src="%s" width="66" height="66" alt="" title="%s"/>' % (
         permalink, icon, obj.title)
예제 #15
0
def get_permalink_absolute(context, model):
    request = context["request"]

    return escape(request.build_absolute_uri(permalinks.create(model)))
예제 #16
0
def permalink(model):
    """Returns a permalink for the given model."""
    return permalinks.create(model)
예제 #17
0
def permalink(obj):
    '''Returns a permalink for the given object.'''
    return permalinks.create(obj)