From ec236d59bc572912321659c490e272424c243ce8 Mon Sep 17 00:00:00 2001 From: Alvaro Leonel Date: Tue, 23 Apr 2024 21:23:55 -0300 Subject: [PATCH] #219 Refactored list views --- notifications/querysets.py | 22 ++++++---- .../templatetags/notifications_tags.py | 2 +- notifications/tests/tests.py | 36 +++++++-------- notifications/urls.py | 3 +- notifications/views.py | 44 ++++++++++--------- 5 files changed, 57 insertions(+), 50 deletions(-) diff --git a/notifications/querysets.py b/notifications/querysets.py index b7c2a90e..21204b32 100644 --- a/notifications/querysets.py +++ b/notifications/querysets.py @@ -20,14 +20,6 @@ def assert_soft_delete() -> None: class NotificationQuerySet(models.QuerySet): """Notification QuerySet""" - def unsent(self) -> "NotificationQuerySet": - """Return only unsent items in the current queryset""" - return self.filter(emailed=False) - - def sent(self) -> "NotificationQuerySet": - """Return only sent items in the current queryset""" - return self.filter(emailed=True) - def unread(self, include_deleted: bool = False) -> "NotificationQuerySet": """Return only unread items in the current queryset""" if notification_settings.SOFT_DELETE and not include_deleted: @@ -103,6 +95,20 @@ def mark_all_as_active(self, recipient: Union[None, Type[AbstractUser]] = None) return qset.update(deleted=False) + def unsent(self, include_deleted: bool = False) -> "NotificationQuerySet": + """Return only unsent items in the current queryset""" + if notification_settings.SOFT_DELETE and not include_deleted: + return self.filter(emailed=False, deleted=False) + + return self.filter(emailed=False) + + def sent(self, include_deleted: bool = False) -> "NotificationQuerySet": + """Return only sent items in the current queryset""" + if notification_settings.SOFT_DELETE and not include_deleted: + return self.filter(emailed=True, deleted=False) + + return self.filter(emailed=True) + def mark_as_unsent(self, recipient: Union[None, Type[AbstractUser]] = None) -> int: qset = self.sent() if recipient: diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 7c91ab89..157b41da 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -58,7 +58,7 @@ def register_notify_callbacks( notify_menu_class='{menu_class}'; notify_api_url='{api_url}'; notify_fetch_count='{fetch}'; - notify_unread_url='{reverse("notifications:unread")}'; + notify_unread_url='{reverse("notifications:list", args=("unread",))}'; notify_mark_all_unread_url='{reverse("notifications:mark_all_as_read")}'; notify_refresh_period={refresh_period}; notify_mark_as_read={str(mark_as_read).lower()}; diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index a4f021f4..4e5fdd62 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -79,7 +79,7 @@ def login(self, username, password): def test_all_messages_page(self): self.login("to", "pwd") - response = self.client.get(reverse("notifications:all")) + response = self.client.get(reverse("notifications:list", args=("all",))) self.assertEqual(response.status_code, 200) self.assertEqual( @@ -88,7 +88,7 @@ def test_all_messages_page(self): def test_unread_messages_pages(self): self.login("to", "pwd") - response = self.client.get(reverse("notifications:unread")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread()) @@ -100,7 +100,7 @@ def test_unread_messages_pages(self): response = self.client.get(reverse("notifications:mark_as_read", args=[notification.id])) self.assertEqual(response.status_code, 302) - response = self.client.get(reverse("notifications:unread")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread()) @@ -108,8 +108,8 @@ def test_unread_messages_pages(self): self.assertTrue(len(response.context["notifications"]) < self.message_count) response = self.client.get(reverse("notifications:mark_all_as_read")) - self.assertRedirects(response, reverse("notifications:unread")) - response = self.client.get(reverse("notifications:unread")) + self.assertRedirects(response, reverse("notifications:list", args=("unread",))) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread()) ) @@ -122,28 +122,28 @@ def test_next_pages(self): response = self.client.get( reverse("notifications:mark_all_as_read"), data={ - "next": reverse("notifications:unread") + query_parameters, + "next": reverse("notifications:list", args=("unread",)) + query_parameters, }, ) - self.assertRedirects(response, reverse("notifications:unread") + query_parameters) + self.assertRedirects(response, reverse("notifications:list", args=("unread",)) + query_parameters) slug = self.to_user.notifications_notification_related.first().id response = self.client.get( reverse("notifications:mark_as_read", args=[slug]), data={ - "next": reverse("notifications:unread") + query_parameters, + "next": reverse("notifications:list", args=("unread",)) + query_parameters, }, ) - self.assertRedirects(response, reverse("notifications:unread") + query_parameters) + self.assertRedirects(response, reverse("notifications:list", args=("unread",)) + query_parameters) slug = self.to_user.notifications_notification_related.first().id response = self.client.get( reverse("notifications:mark_as_unread", args=[slug]), { - "next": reverse("notifications:unread") + query_parameters, + "next": reverse("notifications:list", args=("unread",)) + query_parameters, }, ) - self.assertRedirects(response, reverse("notifications:unread") + query_parameters) + self.assertRedirects(response, reverse("notifications:list", args=("unread",)) + query_parameters) @override_settings(ALLOWED_HOSTS=["www.notifications_notification_related.com"]) def test_malicious_next_pages(self): @@ -157,23 +157,23 @@ def test_malicious_next_pages(self): "next": next_url + query_parameters, }, ) - self.assertRedirects(response, reverse("notifications:unread")) + self.assertRedirects(response, reverse("notifications:list", args=("unread",))) def test_delete_messages_pages(self): self.login("to", "pwd") slug = self.to_user.notifications_notification_related.first().id response = self.client.get(reverse("notifications:delete", args=[slug])) - self.assertRedirects(response, reverse("notifications:all")) + self.assertRedirects(response, reverse("notifications:list", args=("unread",))) - response = self.client.get(reverse("notifications:all")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.all()) ) self.assertEqual(len(response.context["notifications"]), self.message_count - 1) - response = self.client.get(reverse("notifications:unread")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread()) @@ -186,16 +186,16 @@ def test_soft_delete_messages_manager(self): slug = self.to_user.notifications_notification_related.first().id response = self.client.get(reverse("notifications:delete", args=[slug])) - self.assertRedirects(response, reverse("notifications:all")) + self.assertRedirects(response, reverse("notifications:list", args=("unread",))) - response = self.client.get(reverse("notifications:all")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.active()) ) self.assertEqual(len(response.context["notifications"]), self.message_count - 1) - response = self.client.get(reverse("notifications:unread")) + response = self.client.get(reverse("notifications:list", args=("unread",))) self.assertEqual(response.status_code, 200) self.assertEqual( len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread()) diff --git a/notifications/urls.py b/notifications/urls.py index 362e5f4e..7e5e14f3 100644 --- a/notifications/urls.py +++ b/notifications/urls.py @@ -6,8 +6,7 @@ app_name = "notifications" urlpatterns = [ - path("all/", views.AllNotificationsList.as_view(), name="all"), - path("unread/", views.UnreadNotificationsList.as_view(), name="unread"), + path("/list//", views.NotificationsList.as_view(), name="list"), path("mark-all-as-read/", views.mark_all_as_read, name="mark_all_as_read"), path("mark-as-read/)/", views.mark_as_read, name="mark_as_read"), path("mark-as-unread//", views.mark_as_unread, name="mark_as_unread"), diff --git a/notifications/views.py b/notifications/views.py index 062b7a7b..2904ba3b 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -17,32 +17,34 @@ Notification = load_model("notifications", "Notification") -class NotificationViewList(ListView): - template_name = "notifications/list.html" - context_object_name = "notifications" - paginate_by = notification_settings.PAGINATE_BY - - @method_decorator(login_required) - def dispatch(self, request, *args, **kwargs): - return super().dispatch(request, *args, **kwargs) - - -class AllNotificationsList(NotificationViewList): +@method_decorator(login_required, name="dispatch") +class NotificationsList(ListView): """ Index page for authenticated user """ + template_name = "notifications/list.html" + context_object_name = "notifications" + paginate_by = notification_settings.PAGINATE_BY + def get_queryset(self): - if notification_settings.SOFT_DELETE: + filter_by = self.kwargs["filter_by"] + if filter_by == "read": + qset = self.request.user.notifications_notification_related.read() + elif filter_by == "unread": + qset = self.request.user.notifications_notification_related.unread() + elif filter_by == "active": qset = self.request.user.notifications_notification_related.active() + elif filter_by == "deleted": + qset = self.request.user.notifications_notification_related.deleted() + elif filter_by == "sent": + qset = self.request.user.notifications_notification_related.sent() + elif filter_by == "unsent": + qset = self.request.user.notifications_notification_related.unsent() else: qset = self.request.user.notifications_notification_related.all() - return qset - -class UnreadNotificationsList(NotificationViewList): - def get_queryset(self): - return self.request.user.notifications_notification_related.unread() + return qset @login_required @@ -53,7 +55,7 @@ def mark_all_as_read(request): if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS): return redirect(iri_to_uri(_next)) - return redirect("notifications:unread") + return redirect("notifications:list", filter_by="unread") @login_required @@ -68,7 +70,7 @@ def mark_as_read(request, slug=None): if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS): return redirect(iri_to_uri(_next)) - return redirect("notifications:unread") + return redirect("notifications:list", filter_by="unread") @login_required @@ -83,7 +85,7 @@ def mark_as_unread(request, slug=None): if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS): return redirect(iri_to_uri(_next)) - return redirect("notifications:unread") + return redirect("notifications:list", filter_by="unread") @login_required @@ -103,7 +105,7 @@ def delete(request, slug=None): if _next and url_has_allowed_host_and_scheme(_next, settings.ALLOWED_HOSTS): return redirect(iri_to_uri(_next)) - return redirect("notifications:all") + return redirect("notifications:list", filter_by="unread") @never_cache