Remove slug2id and id2slug

This commit is contained in:
Alvaro Mariano 2023-06-27 00:06:44 +00:00
parent 645611c1e1
commit 244539dc47
6 changed files with 13 additions and 24 deletions

View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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")

View file

@ -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

View file

@ -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)