Пример #1
0
 def test_if_modified_since(self):
     resp = FileResponse(
         "我的",
         filename="ddd.jpg",
         headers={"Last-Modified": "Fri, 08 Feb 2019 22:56:51 GMT"},
         req_headers={"If-Modified-Since": "Fri, 08 Feb 2019 22:56:53 GMT"})
     assert resp.status_code == 304
Пример #2
0
 def test_content_is_bytes(self):
     resp = FileResponse(b"abc", filename="ddd.png")
     assert resp.content == b"abc"
     assert resp.headers["content-length"] == "3"
     assert "last-modified" in resp.headers
     assert resp.filename == "ddd.png"
     assert resp.headers["content-type"] == "image/png"
     assert resp.headers["Content-Disposition"] == \
            'attachment; filename="ddd.png"'
     assert resp.status_code == 200
Пример #3
0
    async def export(self, article_list, code, url):
        """
        导出文章或文章列表,生成压缩包
        :param article_list:
        :param code:
        :param url:
        :return:
        """
        zip_file = BytesIO()
        zf = zipfile.ZipFile(zip_file, "w")
        for article in article_list:
            zf.writestr(*await ArticleExporter(article, code, url).export())

        zf.close()
        zip_file.seek(0)
        body = zip_file.read()
        zip_file.close()
        return FileResponse(
            body, filename=f"{datetime.datetime.now().timestamp()}.zip")
Пример #4
0
    async def export(self, article_list, code, url):
        zip_file = BytesIO()
        zf = zipfile.ZipFile(zip_file, "w")
        for article in article_list:
            ext = "md"
            if article.id == "me":
                if not self.check_code(code):
                    return {"error": True}
                from html.parser import unescape
                from weasyprint import HTML
                from urllib.parse import urlparse, urljoin
                html = markdown.markdown(
                    article.article, extensions=['markdown.extensions.extra'])
                html = unescape(html)

                html = '<div class="markdown-body">%s</div>' % re.sub(
                    r'(?<=src\=")(.+?)(?=")',
                    partial(self._repl, current_url=url)
                    , html)
                loop = asyncio.get_event_loop()
                buffer = await loop.run_in_executor(
                    None, partial(HTML(string=html).write_pdf, stylesheets=[
                        os.path.join(project_path, "static/css/pdf.css")]))
                ext = "pdf"
            else:
                buffer = "\n".join(
                    [article.article,
                     "[comment]: <tags> (%s)" % article.tags,
                     "[comment]: <description> (%s)" % article.description,
                     "[comment]: <title> (%s)" % article.title,
                     "[comment]: <author> (%s)" % article.author,
                ]).encode()
            zf.writestr("%s.%s" % (article.title, ext),  buffer)
        zf.close()
        zip_file.seek(0)
        body = zip_file.read()
        zip_file.close()
        return FileResponse(body, filename=f"{get_id()}.zip")
Пример #5
0
 def test_not_download(self, join_root_dir):
     resp = FileResponse(open(join_root_dir("test_data/settings.py")),
                         headers={"content-length": "10"},
                         download=False)
     assert "Content-Disposition" not in resp.headers
Пример #6
0
 def test_content_type(self, join_root_dir):
     resp = FileResponse(open(join_root_dir("test_data/settings.py")),
                         headers={"content-length": "10"})
     assert resp.headers["content-type"] == "text/x-python; charset=utf-8"
Пример #7
0
 def test_content_is_bytes_without_filename(self):
     with pytest.raises(AssertionError) as error_info:
         FileResponse(b"abc")
     assert error_info.value.args[0] == "filename must specify."
Пример #8
0
    def test_content_is_invalid_without_charset(self):
        with pytest.raises(AssertionError) as error_info:
            FileResponse({}, charset=None)

        assert error_info.value.args[0] == \
               "FileResponse content must be bytes. Got dict."
Пример #9
0
    def test_content_is_invalid(self):
        with pytest.raises(AssertionError) as error_info:
            FileResponse({})

        assert error_info.value.args[0] == \
               "FileResponse content must be string or bytes. Got dict."
Пример #10
0
    def test_content_is_fileobj_without_content_length(self, join_root_dir):
        with pytest.raises(AssertionError) as error_info:
            FileResponse(open(join_root_dir("test_data/settings.py")))

        assert error_info.value.args[0] == \
               "File like object need specify Content-Length."
Пример #11
0
 def test_content_is_fileobj(self, join_root_dir):
     resp = FileResponse(open(join_root_dir("test_data/test.xz")),
                         headers={"content-length": "10"})
     assert resp.filename == "test.xz"
     assert resp.headers["content-length"] == "10"
     assert resp.headers["content-encoding"] == "xz"