diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py index 51ab277..ff771b1 100644 --- a/akarpov/files/previews/__init__.py +++ b/akarpov/files/previews/__init__.py @@ -19,7 +19,7 @@ "avif": image.basic.view, "bmp": image.basic.view, }, - "text": {"css": text.common.view, "plain": text.plain.view}, + "text": {"css": text.common.view, "plain": text.plain.view, "csv": text.csv.view}, } source_code = {} diff --git a/akarpov/files/previews/text/__init__.py b/akarpov/files/previews/text/__init__.py index a9a0aaf..4f27b08 100644 --- a/akarpov/files/previews/text/__init__.py +++ b/akarpov/files/previews/text/__init__.py @@ -1 +1 @@ -from . import common, plain # noqa +from . import common, csv, plain # noqa diff --git a/akarpov/files/previews/text/csv.py b/akarpov/files/previews/text/csv.py new file mode 100644 index 0000000..d86bfb7 --- /dev/null +++ b/akarpov/files/previews/text/csv.py @@ -0,0 +1,34 @@ +import csv + +import chardet +import structlog + +from akarpov.files.models import File + +logger = structlog.get_logger(__name__) + + +def view(file: File): + try: + try: + with open(file.file.path, newline="") as csvfile: + dialect = csv.Sniffer().sniff(csvfile.read(1024)) + csvfile.seek(0) + reader = csv.reader(csvfile, dialect) + for row in reader: + print(row) + except UnicodeDecodeError: + rawdata = open("file.csv", "rb").read() + enc = chardet.detect(rawdata) + print(enc) + with open(file.file.path, newline="", encoding=enc["encoding"]) as csvfile: + dialect = csv.Sniffer().sniff(csvfile.read(1024)) + csvfile.seek(0) + reader = csv.reader(csvfile, dialect) + for row in reader: + print(row) + except Exception as e: + logger.error(e) + static = "" + content = "" + return static, content diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 0e397d7..b9c39f2 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -18,7 +18,7 @@ class TopFolderView(LoginRequiredMixin, ListView): template_name = "files/list.html" - paginate_by = 20 + paginate_by = 19 model = File def get_queryset(self): diff --git a/poetry.lock b/poetry.lock index 7acb9db..fe58ef2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -689,6 +689,18 @@ Django = ">=3.2" daphne = ["daphne (>=4.0.0)"] tests = ["async-timeout", "coverage (>=4.5,<5.0)", "pytest", "pytest-asyncio", "pytest-django"] +[[package]] +name = "chardet" +version = "5.1.0" +description = "Universal encoding detector for Python 3" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "chardet-5.1.0-py3-none-any.whl", hash = "sha256:362777fb014af596ad31334fde1e8c327dfdb076e1960d1694662d46a6917ab9"}, + {file = "chardet-5.1.0.tar.gz", hash = "sha256:0d62712b956bc154f85fb0a266e2a3c5913c2967e00348701b32411d6def31e5"}, +] + [[package]] name = "charset-normalizer" version = "3.1.0" @@ -3163,7 +3175,6 @@ files = [ {file = "Pillow-9.5.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:c1170d6b195555644f0616fd6ed929dfcf6333b8675fcca044ae5ab110ded296"}, {file = "Pillow-9.5.0-cp311-cp311-win32.whl", hash = "sha256:54f7102ad31a3de5666827526e248c3530b3a33539dbda27c6843d19d72644ec"}, {file = "Pillow-9.5.0-cp311-cp311-win_amd64.whl", hash = "sha256:cfa4561277f677ecf651e2b22dc43e8f5368b74a25a8f7d1d4a3a243e573f2d4"}, - {file = "Pillow-9.5.0-cp311-cp311-win_arm64.whl", hash = "sha256:965e4a05ef364e7b973dd17fc765f42233415974d773e82144c9bbaaaea5d089"}, {file = "Pillow-9.5.0-cp312-cp312-win32.whl", hash = "sha256:22baf0c3cf0c7f26e82d6e1adf118027afb325e703922c8dfc1d5d0156bb2eeb"}, {file = "Pillow-9.5.0-cp312-cp312-win_amd64.whl", hash = "sha256:432b975c009cf649420615388561c0ce7cc31ce9b2e374db659ee4f7d57a1f8b"}, {file = "Pillow-9.5.0-cp37-cp37m-macosx_10_10_x86_64.whl", hash = "sha256:5d4ebf8e1db4441a55c509c4baa7a0587a0210f7cd25fcfe74dbbce7a4bd1906"}, @@ -5202,4 +5213,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "717219069cdeaa5037bc3693ddb6580db23e67d0e6ae42b47deed69a048622ea" +content-hash = "580bc9d748f65b25b9de3554c49e5c05a9cb3fccc60ce411b8f953ab70f2c14f" diff --git a/pyproject.toml b/pyproject.toml index c6cd624..00cddb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,6 +86,7 @@ xvfbwrapper = "^0.2.9" vtk = "^9.2.6" ffmpeg-python = "^0.2.0" cairosvg = "^2.7.0" +chardet = "^5.1.0" [build-system]