diff --git a/akarpov/files/admin.py b/akarpov/files/admin.py index 5ea49d1..f9100ad 100644 --- a/akarpov/files/admin.py +++ b/akarpov/files/admin.py @@ -1,6 +1,8 @@ from django.contrib import admin -from akarpov.files.models import File, Folder +from akarpov.files.models import File, FileInTrash, FileReport, Folder admin.site.register(File) admin.site.register(Folder) +admin.site.register(FileInTrash) +admin.site.register(FileReport) diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py index 3420969..9adc404 100644 --- a/akarpov/files/previews/__init__.py +++ b/akarpov/files/previews/__init__.py @@ -9,6 +9,7 @@ "docx": application.docx.view, "vnd.oasis.opendocument.text": application.odt.view, "x-httpd-php": text.common.view, + "json": application.json.view, }, "audio": { "aac": audio.basic.view, @@ -77,6 +78,7 @@ "oga": audio.oga.view, "pdf": application.pdf.view, "html": text.html.view, + "json": application.json.view, } | source_code | fonts_ext diff --git a/akarpov/files/previews/application/__init__.py b/akarpov/files/previews/application/__init__.py index ce34ad6..1fa0548 100644 --- a/akarpov/files/previews/application/__init__.py +++ b/akarpov/files/previews/application/__init__.py @@ -1 +1 @@ -from . import doc, docx, odt, pdf, zip # noqa +from . import doc, docx, json, odt, pdf, zip # noqa diff --git a/akarpov/files/previews/application/json.py b/akarpov/files/previews/application/json.py new file mode 100644 index 0000000..11a4fd1 --- /dev/null +++ b/akarpov/files/previews/application/json.py @@ -0,0 +1,84 @@ +from akarpov.files.models import File + + +def view(file: File): + static = """ + + """ + if file.file_size < 200 * 1024: + req = ( + f""" + getJSON('{file.file.url}', + """ + + """ + function(err, data) { + if (err !== null) { + console.log('Something went wrong: ' + err); + } else { + var str = JSON.stringify(data, undefined, 4); + output(syntaxHighlight(str)); + } + }); + """ + ) + else: + req = """ + output("file is too large, download to view") + """ + + content = ( + """ +
+ + """ + ) + return static, content diff --git a/akarpov/files/previews/text/common.py b/akarpov/files/previews/text/common.py index 8b98e23..2071bd5 100644 --- a/akarpov/files/previews/text/common.py +++ b/akarpov/files/previews/text/common.py @@ -30,6 +30,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" extension = file.file.path.split(".")[-1] if extension in language_previews: extension = language_previews[extension] diff --git a/akarpov/files/previews/text/html.py b/akarpov/files/previews/text/html.py index 7db7e21..c1f36ca 100644 --- a/akarpov/files/previews/text/html.py +++ b/akarpov/files/previews/text/html.py @@ -4,6 +4,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" static = f""" diff --git a/akarpov/files/previews/text/plain.py b/akarpov/files/previews/text/plain.py index 03437e9..b8ac6a0 100644 --- a/akarpov/files/previews/text/plain.py +++ b/akarpov/files/previews/text/plain.py @@ -6,6 +6,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" extension = file.file.path.split(".")[-1] if hasattr(text, extension): return getattr(text, extension).view(file) diff --git a/akarpov/files/views.py b/akarpov/files/views.py index ea85c5e..0cfb2b5 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -17,11 +17,14 @@ from django_tables2 import SingleTableView from django_tables2.export import ExportMixin +from akarpov.common.views import SuperUserRequiredMixin from akarpov.contrib.chunked_upload.exceptions import ChunkedUploadError from akarpov.contrib.chunked_upload.models import ChunkedUpload from akarpov.contrib.chunked_upload.views import ( - ChunkedUploadCompleteView, - ChunkedUploadView, + ChunkedUploadCompleteView as ChunkedUploadABSCompleteView, +) +from akarpov.contrib.chunked_upload.views import ( + ChunkedUploadView as ChunkedUploadABSView, ) from akarpov.files.filters import FileFilter from akarpov.files.forms import FileForm, FolderForm @@ -207,7 +210,7 @@ def get_redirect_url(self, *args, **kwargs): delete_file_view = DeleteFileView.as_view() -class ChunkedUploadView(ChunkedUploadView): +class ChunkedUploadView(ChunkedUploadABSView): model = ChunkedUpload field_name = "the_file" @@ -218,7 +221,7 @@ def check_permissions(self, request): ) -class ChunkedUploadCompleteView(ChunkedUploadCompleteView): +class ChunkedUploadCompleteView(ChunkedUploadABSCompleteView): def __init__(self, **kwargs): super().__init__(**kwargs) self.message = {} @@ -298,14 +301,10 @@ def get_redirect_url(self, *args, **kwargs): report_file = ReportFileView.as_view() -class ListFileReports(LoginRequiredMixin, ListView): +class ListFileReports(SuperUserRequiredMixin, ListView): model = FileReport + queryset = FileReport.objects.all() template_name = "files/reports.html" - def get_queryset(self): - if self.request.user.is_superuser: - return FileReport.objects.all() - return FileReport.objects.none() - file_report_list = ListFileReports.as_view() diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index 9cd3496..006ea62 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -36,7 +36,7 @@