From 08198e0535107baa39ff04c723e638cbfd283b16 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sun, 24 Sep 2023 20:29:02 +0300 Subject: [PATCH] added file notification on view, minor fixes --- akarpov/files/forms.py | 2 +- ..._user_on_view_alter_basefileitem_parent.py | 32 ++++++++++++++++ akarpov/files/models.py | 6 +++ .../files/previews/application/__init__.py | 2 + akarpov/files/views.py | 11 ++++++ akarpov/templates/base.html | 37 +++++++++++++++++++ akarpov/templates/files/list.html | 2 +- akarpov/templates/files/upload.html | 10 ++--- 8 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 akarpov/files/migrations/0025_file_notify_user_on_view_alter_basefileitem_parent.py diff --git a/akarpov/files/forms.py b/akarpov/files/forms.py index 89d4fb4..e99aba7 100644 --- a/akarpov/files/forms.py +++ b/akarpov/files/forms.py @@ -6,7 +6,7 @@ class FileForm(forms.ModelForm): class Meta: model = File - fields = ["name", "private", "description"] + fields = ["name", "private", "notify_user_on_view", "description"] class FolderForm(forms.ModelForm): diff --git a/akarpov/files/migrations/0025_file_notify_user_on_view_alter_basefileitem_parent.py b/akarpov/files/migrations/0025_file_notify_user_on_view_alter_basefileitem_parent.py new file mode 100644 index 0000000..16fc653 --- /dev/null +++ b/akarpov/files/migrations/0025_file_notify_user_on_view_alter_basefileitem_parent.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.5 on 2023-09-24 16:47 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("files", "0024_alter_file_options_alter_filereport_options_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="file", + name="notify_user_on_view", + field=models.BooleanField( + default=False, verbose_name="Receive notifications on file view" + ), + ), + migrations.AlterField( + model_name="basefileitem", + name="parent", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="children", + to="files.folder", + verbose_name="Folder", + ), + ), + ] diff --git a/akarpov/files/models.py b/akarpov/files/models.py index 6ca27d7..affa15f 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -26,6 +26,7 @@ class BaseFileItem(PolymorphicModel): parent = ForeignKey( + verbose_name="Folder", to="files.Folder", null=True, blank=True, @@ -74,6 +75,11 @@ class File(BaseFileItem, TimeStampedModel, ShortLinkModel, UserHistoryModel): description = TextField(blank=True, null=True) file_type = CharField(max_length=255, null=True, blank=True) + # extra settings + notify_user_on_view = BooleanField( + "Receive notifications on file view", default=False + ) + @property def file_name(self): return self.file.path.split("/")[-1] diff --git a/akarpov/files/previews/application/__init__.py b/akarpov/files/previews/application/__init__.py index 1fa0548..b4b2bdc 100644 --- a/akarpov/files/previews/application/__init__.py +++ b/akarpov/files/previews/application/__init__.py @@ -1 +1,3 @@ from . import doc, docx, json, odt, pdf, zip # noqa + +# TODO: add gzip, xlsx diff --git a/akarpov/files/views.py b/akarpov/files/views.py index fd70e14..d4a6a76 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -34,6 +34,7 @@ from akarpov.files.services.folders import delete_folder from akarpov.files.services.preview import get_base_meta from akarpov.files.tables import FileTable +from akarpov.notifications.services import send_notification logger = structlog.get_logger(__name__) @@ -148,6 +149,16 @@ def dispatch(self, request, *args, **kwargs): if "bot" in useragent: if file.file_type and file.file_type.split("/")[0] == "image": return HttpResponseRedirect(file.file.url) + else: + if file.notify_user_on_view: + if self.request.user != file.user: + send_notification( + "File view", + f"File {file.name} was opened", + "site", + user_id=file.user.id, + conformation=True, + ) return super().dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): diff --git a/akarpov/templates/base.html b/akarpov/templates/base.html index 4d0f8da..e352fe2 100644 --- a/akarpov/templates/base.html +++ b/akarpov/templates/base.html @@ -140,5 +140,42 @@ {% block inline_javascript %} {% endblock inline_javascript %} + {% if request.user.is_authenticated %} + + {% endif %} diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index 6a0d7bf..ffc4dc4 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -56,7 +56,7 @@ {% endif %} {% endif %} - {% if request.user.is_authenticated and is_folder_owner %} + {% if request.user.is_authenticated and is_folder_owner and not folder_slug %}
table view
diff --git a/akarpov/templates/files/upload.html b/akarpov/templates/files/upload.html index 8c5bed0..3d7b3c4 100644 --- a/akarpov/templates/files/upload.html +++ b/akarpov/templates/files/upload.html @@ -26,11 +26,11 @@ {% block inline_javascript %}