From a7dcfb4540aa8f51944529f9956e9cfb85bba436 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Fri, 14 Apr 2023 21:07:56 +0300 Subject: [PATCH] made video adaptive, fixed csv preview --- ...16_alter_file_file_type_alter_file_name.py | 23 +++++++++++ akarpov/files/models.py | 4 +- akarpov/files/previews/application/zip.py | 5 ++- akarpov/files/previews/image/basic.py | 2 +- akarpov/files/previews/text/common.py | 4 +- akarpov/files/previews/text/csv.py | 38 ++++++++++++++++--- akarpov/files/previews/text/plain.py | 4 +- akarpov/files/previews/video/basic.py | 8 ++-- akarpov/files/previews/video/mp4.py | 9 +++-- akarpov/templates/files/view.html | 2 - 10 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 akarpov/files/migrations/0016_alter_file_file_type_alter_file_name.py diff --git a/akarpov/files/migrations/0016_alter_file_file_type_alter_file_name.py b/akarpov/files/migrations/0016_alter_file_file_type_alter_file_name.py new file mode 100644 index 0000000..8e062aa --- /dev/null +++ b/akarpov/files/migrations/0016_alter_file_file_type_alter_file_name.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2 on 2023-04-14 17:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("files", "0015_fileintrash_name"), + ] + + operations = [ + migrations.AlterField( + model_name="file", + name="file_type", + field=models.CharField(blank=True, max_length=255, null=True), + ), + migrations.AlterField( + model_name="file", + name="name", + field=models.CharField(blank=True, max_length=255, null=True), + ), + ] diff --git a/akarpov/files/models.py b/akarpov/files/models.py index 4618f7e..cd9349b 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -31,9 +31,9 @@ class File(TimeStampedModel, ShortLink): file = FileField(blank=False, upload_to=user_unique_file_upload) # meta - name = CharField(max_length=100, null=True, blank=True) + name = CharField(max_length=255, null=True, blank=True) description = TextField(blank=True, null=True) - file_type = CharField(max_length=50, null=True, blank=True) + file_type = CharField(max_length=255, null=True, blank=True) @property def file_image_url(self): diff --git a/akarpov/files/previews/application/zip.py b/akarpov/files/previews/application/zip.py index 95fcae1..3f269c4 100644 --- a/akarpov/files/previews/application/zip.py +++ b/akarpov/files/previews/application/zip.py @@ -96,6 +96,7 @@ def view(file: File) -> (str, str): """ content = ( """ +
    Picture
diff --git a/akarpov/files/previews/text/common.py b/akarpov/files/previews/text/common.py index 00db072..e14a50a 100644 --- a/akarpov/files/previews/text/common.py +++ b/akarpov/files/previews/text/common.py @@ -37,7 +37,7 @@ def view(file: File) -> (str, str): """ - content = "
"
+    content = "
"
     with file.file.open("r") as f:
         lines = f.readlines()
     for line in lines:
@@ -45,7 +45,7 @@ def view(file: File) -> (str, str):
             f"""
{html.escape(line)}
""" ) content += ( - """
+ """
""" + f""" diff --git a/akarpov/files/previews/text/csv.py b/akarpov/files/previews/text/csv.py index d86bfb7..1f216db 100644 --- a/akarpov/files/previews/text/csv.py +++ b/akarpov/files/previews/text/csv.py @@ -8,6 +8,37 @@ logger = structlog.get_logger(__name__) +def get_csv_table(reader): + content = "
" + header = next(reader) + content += """""" + for el in header: + content += f"""""" + content += """\n""" + if header: + content += """""" + i = 0 + for row in reader: + r = "" + if i >= 5000: + for _ in header: + r += """""" + r += "\n" + content += r + break + for ind, el in enumerate(row): + if ind == 0: + r += f"""""" + else: + r += f"""""" + r += "\n" + content += r + i += 1 + content += """""" + content += "
{el}
the remaining data was trunked
{el}{el}
" + return content + + def view(file: File): try: try: @@ -15,8 +46,7 @@ def view(file: File): dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) reader = csv.reader(csvfile, dialect) - for row in reader: - print(row) + content = get_csv_table(reader) except UnicodeDecodeError: rawdata = open("file.csv", "rb").read() enc = chardet.detect(rawdata) @@ -25,10 +55,8 @@ def view(file: File): dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) reader = csv.reader(csvfile, dialect) - for row in reader: - print(row) + content = get_csv_table(reader) except Exception as e: logger.error(e) static = "" - content = "" return static, content diff --git a/akarpov/files/previews/text/plain.py b/akarpov/files/previews/text/plain.py index b53a08b..03437e9 100644 --- a/akarpov/files/previews/text/plain.py +++ b/akarpov/files/previews/text/plain.py @@ -14,10 +14,10 @@ def view(file: File) -> (str, str): static = f""" """ - content = "
"
+    content = "
"
     with file.file.open("r") as f:
         lines = f.readlines()
     for line in lines:
         content += f"""
{html.escape(line)}
""" - content += "
" + content += "
" return static, content diff --git a/akarpov/files/previews/video/basic.py b/akarpov/files/previews/video/basic.py index 077c09d..a356635 100644 --- a/akarpov/files/previews/video/basic.py +++ b/akarpov/files/previews/video/basic.py @@ -11,15 +11,17 @@ def view(file: File) -> (str, str): """ - data = """{"playbackRates": [0.5, 1, 1.5, 2], "responsive": true, "fit": true, "fluid": true }""" + data = """{"playbackRates": [0.5, 1, 1.5, 2], fit: true }""" content = f""" -