Esempio n. 1
0
def upload_attachment(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST method is allowed')

    if not request.FILES.getlist('files'):
        return HttpResponseBadRequest('No files were requested')

    try:
        attachments = []

        for file in request.FILES.getlist('files'):
            attachment = Attachment()
            attachment.file = file
            attachment.name = file.name

            if file.size > summernote_config['attachment_filesize_limit']:
                return HttpResponseBadRequest(
                    'File size exceeds the limit allowed and cannot be saved')

            attachment.save()
            attachments.append(attachment)

        return render(request, 'django_summernote/upload_attachment.json', {
            'attachments': attachments,
        })
    except IOError:
        return HttpResponseServerError('Failed to save attachment')
Esempio n. 2
0
def upload_attachment(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST method is allowed')

    if not request.FILES.getlist('files'):
        return HttpResponseBadRequest('No files were requested')

    try:
        attachments = []

        for file in request.FILES.getlist('files'):
            attachment = Attachment()
            attachment.file = file
            attachment.name = file.name

            if file.size > summernote_config['attachment_filesize_limit']:
                return HttpResponseBadRequest('File size exceeds the limit allowed and cannot be saved')

            attachment.save()
            attachments.append(attachment)

        return render(request, 'django_summernote/upload_attachment.json', {
            'attachments': attachments,
        })
    except IOError:
        return HttpResponseServerError('Failed to save attachment')
Esempio n. 3
0
    def test_attachment_as_string(self):
        from django_summernote.models import Attachment
        from django.core.files import File
        import os

        attachment = Attachment()
        with open(IMAGE_FILE, 'rb') as fp:
            djangoFile = File(fp)
            djangoFile.name = os.path.basename(djangoFile.name)
            attachment.file = djangoFile
            attachment.save()

            self.assertEqual(str(attachment), djangoFile.name)
Esempio n. 4
0
    def test_attachment_admin_default_name(self):
        from django_summernote.admin import AttachmentAdmin
        from django_summernote.models import Attachment
        from django.core.files import File
        import os

        aa = AttachmentAdmin(Attachment, self.site)
        attachment = Attachment()
        with open(__file__, 'rb') as fp:
            djangoFile = File(fp)
            djangoFile.name = os.path.basename(djangoFile.name)
            attachment.file = djangoFile
            self.assertEqual(attachment.name, None)
            aa.save_model(None, attachment, None, None)
            self.assertEqual(attachment.name, os.path.basename(__file__))
Esempio n. 5
0
    def test_attachment_admin_default_name(self):
        from django_summernote.admin import AttachmentAdmin
        from django_summernote.models import Attachment
        from django.core.files import File
        import os

        aa = AttachmentAdmin(Attachment, self.site)
        attachment = Attachment()
        with open(__file__, 'rb') as fp:
            djangoFile = File(fp)
            djangoFile.name = os.path.basename(djangoFile.name)
            attachment.file = djangoFile
            self.assertEqual(attachment.name, None)
            aa.save_model(None, attachment, None, None)
            self.assertEqual(attachment.name, os.path.basename(__file__))
Esempio n. 6
0
def upload_attachment(request):
    if request.method != "POST":
        return HttpResponseBadRequest("Only POST method is allowed")

    if not request.FILES.getlist("files"):
        return HttpResponseBadRequest("No files were requested")

    try:
        attachments = []

        for file in request.FILES.getlist("files"):
            attachment = Attachment()
            attachment.file = file
            attachment.name = file.name

            if file.size > summernote_config["attachment_filesize_limit"]:
                return HttpResponseBadRequest("File size exceeds the limit allowed and cannot be saved")

            attachment.save()
            attachments.append(attachment)

        return render(request, "django_summernote/upload_attachment.json", {"attachments": attachments})
    except IOError:
        return HttpResponseServerError("Failed to save attachment")