From 244539dc4733107afe79d141222cd604125d7c4e Mon Sep 17 00:00:00 2001 From: Alvaro Mariano Date: Tue, 27 Jun 2023 00:06:44 +0000 Subject: [PATCH] Remove slug2id and id2slug --- CHANGELOG.md | 1 + notifications/base/models.py | 3 +-- notifications/helpers.py | 3 +-- notifications/tests/tests.py | 15 +++++++-------- notifications/utils.py | 8 -------- notifications/views.py | 7 +++---- 6 files changed, 13 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 845f72b..2d1e6fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Extracted and improved the sample code - Fixed variable types on JS code - Added `/all/` for the path to all notifications + - Remove slug2id and id2slug ## 1.8.0 diff --git a/notifications/base/models.py b/notifications/base/models.py index 05fdbec..70a9aed 100644 --- a/notifications/base/models.py +++ b/notifications/base/models.py @@ -15,7 +15,6 @@ from swapper import load_model from notifications import settings as notifications_settings from notifications.signals import notify -from notifications.utils import id2slug EXTRA_DATA = notifications_settings.get_config()["USE_JSONFIELD"] @@ -256,7 +255,7 @@ class AbstractNotification(models.Model): @property def slug(self): - return id2slug(self.id) + return self.id def mark_as_read(self): if self.unread: diff --git a/notifications/helpers.py b/notifications/helpers.py index a6feb6d..7973982 100644 --- a/notifications/helpers.py +++ b/notifications/helpers.py @@ -1,7 +1,6 @@ from django.forms import model_to_dict from notifications.settings import get_config -from notifications.utils import id2slug def get_num_to_fetch(request): @@ -22,7 +21,7 @@ def get_notification_list(request, method_name="all"): notification_list = [] for notification in getattr(request.user.notifications, method_name)()[0:num_to_fetch]: struct = model_to_dict(notification) - struct["slug"] = id2slug(notification.id) + struct["slug"] = notification.id if notification.actor: struct["actor"] = str(notification.actor) if notification.target: diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index dd24763..25ed566 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -23,7 +23,6 @@ from swapper import load_model from notifications.base.models import notify_handler from notifications.signals import notify -from notifications.utils import id2slug Notification = load_model("notifications", "Notification") User = get_user_model() @@ -222,7 +221,7 @@ class NotificationTestPages(TestCase): for index, notification in enumerate(self.to_user.notifications.all()): if index % 3 == 0: - response = self.client.get(reverse("notifications:mark_as_read", args=[id2slug(notification.id)])) + 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")) @@ -248,7 +247,7 @@ class NotificationTestPages(TestCase): ) self.assertRedirects(response, reverse("notifications:unread") + query_parameters) - slug = id2slug(self.to_user.notifications.first().id) + slug = self.to_user.notifications.first().id response = self.client.get( reverse("notifications:mark_as_read", args=[slug]), data={ @@ -257,7 +256,7 @@ class NotificationTestPages(TestCase): ) self.assertRedirects(response, reverse("notifications:unread") + query_parameters) - slug = id2slug(self.to_user.notifications.first().id) + slug = self.to_user.notifications.first().id response = self.client.get( reverse("notifications:mark_as_unread", args=[slug]), { @@ -283,7 +282,7 @@ class NotificationTestPages(TestCase): def test_delete_messages_pages(self): self.login("to", "pwd") - slug = id2slug(self.to_user.notifications.first().id) + slug = self.to_user.notifications.first().id response = self.client.get(reverse("notifications:delete", args=[slug])) self.assertRedirects(response, reverse("notifications:all")) @@ -301,7 +300,7 @@ class NotificationTestPages(TestCase): def test_soft_delete_messages_manager(self): self.login("to", "pwd") - slug = id2slug(self.to_user.notifications.first().id) + slug = self.to_user.notifications.first().id response = self.client.get(reverse("notifications:delete", args=[slug])) self.assertRedirects(response, reverse("notifications:all")) @@ -396,7 +395,7 @@ class NotificationTestPages(TestCase): self.assertEqual(data["unread_count"], 1) self.assertEqual(len(data["unread_list"]), 1) self.assertEqual(data["unread_list"][0]["verb"], "commented") - self.assertEqual(data["unread_list"][0]["slug"], id2slug(data["unread_list"][0]["id"])) + self.assertEqual(data["unread_list"][0]["slug"], data["unread_list"][0]["id"]) def test_all_list_api(self): self.login("to", "pwd") @@ -439,7 +438,7 @@ class NotificationTestPages(TestCase): self.assertEqual(data["all_count"], self.message_count + 1) self.assertEqual(len(data["all_list"]), self.message_count) self.assertEqual(data["all_list"][0]["verb"], "commented") - self.assertEqual(data["all_list"][0]["slug"], id2slug(data["all_list"][0]["id"])) + self.assertEqual(data["all_list"][0]["slug"], data["all_list"][0]["id"]) def test_unread_list_api_mark_as_read(self): # pylint: disable=invalid-name self.login("to", "pwd") diff --git a/notifications/utils.py b/notifications/utils.py index a2abcad..038355b 100644 --- a/notifications/utils.py +++ b/notifications/utils.py @@ -1,10 +1,2 @@ """' Django notifications utils file """ # -*- coding: utf-8 -*- - - -def slug2id(slug): - return int(slug) - 110909 - - -def id2slug(notification_id): - return notification_id + 110909 diff --git a/notifications/views.py b/notifications/views.py index 89a55c1..c3c077f 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -13,7 +13,6 @@ from swapper import load_model from notifications import settings as notification_settings from notifications.helpers import get_notification_list -from notifications.utils import slug2id Notification = load_model("notifications", "Notification") @@ -59,7 +58,7 @@ def mark_all_as_read(request): @login_required def mark_as_read(request, slug=None): - notification_id = slug2id(slug) + notification_id = slug notification = get_object_or_404(Notification, recipient=request.user, id=notification_id) notification.mark_as_read() @@ -74,7 +73,7 @@ def mark_as_read(request, slug=None): @login_required def mark_as_unread(request, slug=None): - notification_id = slug2id(slug) + notification_id = slug notification = get_object_or_404(Notification, recipient=request.user, id=notification_id) notification.mark_as_unread() @@ -89,7 +88,7 @@ def mark_as_unread(request, slug=None): @login_required def delete(request, slug=None): - notification_id = slug2id(slug) + notification_id = slug notification = get_object_or_404(Notification, recipient=request.user, id=notification_id)