#219 Refactored list views

This commit is contained in:
Alvaro Leonel 2024-04-23 21:23:55 -03:00
parent b1827ca531
commit ec236d59bc
5 changed files with 57 additions and 50 deletions

View file

@ -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 @@ class NotificationQuerySet(models.QuerySet):
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:

View file

@ -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()};

View file

@ -79,7 +79,7 @@ class NotificationTestPages(TestCase):
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 @@ class NotificationTestPages(TestCase):
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 @@ class NotificationTestPages(TestCase):
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 @@ class NotificationTestPages(TestCase):
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 @@ class NotificationTestPages(TestCase):
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 @@ class NotificationTestPages(TestCase):
"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 @@ class NotificationTestPages(TestCase):
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())

View file

@ -6,8 +6,7 @@ from . import views
app_name = "notifications"
urlpatterns = [
path("all/", views.AllNotificationsList.as_view(), name="all"),
path("unread/", views.UnreadNotificationsList.as_view(), name="unread"),
path("/list/<str:filter_by>/", 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/<int:slug>)/", views.mark_as_read, name="mark_as_read"),
path("mark-as-unread/<int:slug>/", views.mark_as_unread, name="mark_as_unread"),

View file

@ -17,34 +17,36 @@ from notifications.settings import notification_settings
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()
@login_required
def mark_all_as_read(request):
request.user.notifications_notification_related.mark_all_as_read()
@ -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