Skip to content

Commit

Permalink
#219 Refactored list views
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroLQueiroz committed Apr 24, 2024
1 parent b1827ca commit ec236d5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
22 changes: 14 additions & 8 deletions notifications/querysets.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion notifications/templatetags/notifications_tags.py
Expand Up @@ -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()};
Expand Down
36 changes: 18 additions & 18 deletions notifications/tests/tests.py
Expand Up @@ -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(
Expand All @@ -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())
Expand All @@ -100,16 +100,16 @@ 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())
)
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())
)
Expand All @@ -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):
Expand All @@ -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())
Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions notifications/urls.py
Expand Up @@ -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/<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"),
Expand Down
44 changes: 23 additions & 21 deletions notifications/views.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ec236d5

Please sign in to comment.