示例#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')
示例#2
0
    def save(self, *args, **kwargs):
        if self.content and not self.markdown:
            soup = BeautifulSoup(self.content, "html.parser")
            for tag in soup.select("*"):
                if tag.get("style"):
                    tag["style"] = re.sub(r"font-?\w*:\s*.*?;", "",
                                          tag["style"])
            for tag in soup.find_all("img"):
                url = tag.get("src")
                if url and not url.startswith("/"):
                    try:
                        res = requests.get(url)
                        if res.ok:
                            att = Attachment(name=os.path.basename(
                                urlparse(url).path)[:50].replace("/", ""))
                            att.file.save(att.name[:20],
                                          ContentFile(res.content))
                            att.save()
                            tag["src"] = att.file.url
                    except requests.exceptions.RequestException:
                        pass

                if tag.get("style"):
                    tag["style"] = re.sub(r"margin-?\w*:\s*.*?;", "",
                                          tag["style"])

            self.content = str(soup)
        self.slug = slugify(self.title)
        return super().save(*args, **kwargs)
示例#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)
示例#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__))
示例#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__))
示例#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')
示例#7
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")