Skip to content

Commit 44f9370

Browse files
committed
feat: add notification type,message and suggestion id in api response
Signed-off-by: Viena <169875752+devnchill@users.noreply.github.com>
1 parent 1ecf651 commit 44f9370

2 files changed

Lines changed: 67 additions & 9 deletions

File tree

src/api/notifications/views.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
1+
from enum import StrEnum
2+
13
from django.db.models import QuerySet
24
from rest_framework import serializers, viewsets
35
from rest_framework.pagination import PageNumberPagination
46
from rest_framework.permissions import IsAuthenticated
57

6-
from webview.models import Notification
8+
from webview.models import Notification, SuggestionNotification, TextNotification
9+
10+
11+
class NotificationType(StrEnum):
12+
TEXT = "text"
13+
SUGGESTION = "suggestion"
714

815

916
class NotificationSerializer(serializers.ModelSerializer):
17+
type = serializers.SerializerMethodField()
1018
title = serializers.SerializerMethodField()
19+
message = serializers.SerializerMethodField()
20+
suggestion_id = serializers.SerializerMethodField()
1121

1222
class Meta:
1323
model = Notification
14-
fields = ["id", "title", "is_read", "created_at"]
24+
fields = [
25+
"id",
26+
"type",
27+
"title",
28+
"message",
29+
"suggestion_id",
30+
"is_read",
31+
"created_at",
32+
]
33+
34+
def get_type(self, obj: Notification) -> NotificationType:
35+
if isinstance(obj, TextNotification):
36+
return NotificationType.TEXT
37+
return NotificationType.SUGGESTION
1538

1639
def get_title(self, obj: Notification) -> str:
1740
return obj.title
1841

42+
def get_message(self, obj: Notification) -> str | None:
43+
if isinstance(obj, TextNotification):
44+
return obj.message
45+
return None
46+
47+
def get_suggestion_id(self, obj: Notification) -> int | None:
48+
if isinstance(obj, SuggestionNotification):
49+
return obj.suggestion_id
50+
return None
51+
1952

2053
class NotificationPagination(PageNumberPagination):
2154
page_size = 20

src/api/tests/test_notifications.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from rest_framework.reverse import reverse
66
from rest_framework.test import APIClient
77

8+
from api.notifications.views import NotificationType
89
from webview.models import Notification
910

1011

@@ -13,21 +14,24 @@ def test_list_notifications_authenticated(
1314
user: User,
1415
make_maintainer_notification: Callable[..., list[Notification]],
1516
) -> None:
16-
make_maintainer_notification(user)
17+
db_notifications = make_maintainer_notification(user)
1718

1819
url = reverse("notification-list")
1920
response = client.get(url)
2021

2122
assert response.status_code == status.HTTP_200_OK
2223
assert response.data["count"] == 1
2324

24-
api_data = response.data["results"]
25-
assert api_data[0]["title"] == (
25+
results = response.data["results"]
26+
assert results[0]["title"] == (
2627
"CVE-2025-0001 was automatically matched to packages you subscribed to"
2728
)
28-
assert api_data[0]["is_read"] is False
29-
assert "id" in api_data[0]
30-
assert "created_at" in api_data[0]
29+
assert results[0]["is_read"] is False
30+
assert "id" in results[0]
31+
assert "created_at" in results[0]
32+
assert results[0]["type"] == NotificationType.SUGGESTION
33+
assert results[0]["message"] is None
34+
assert results[0]["suggestion_id"] == db_notifications[0].suggestion_id
3135

3236

3337
def test_list_notifications_are_paginated(
@@ -94,8 +98,11 @@ def test_list_notifications_includes_text_notifications(
9498
assert response.data["count"] == 1
9599

96100
api_data = response.data["results"]
101+
assert api_data[0]["type"] == NotificationType.TEXT
97102
assert api_data[0]["title"] == "Hello"
98103
assert api_data[0]["is_read"] is False
104+
assert api_data[0]["message"] == "World"
105+
assert api_data[0]["suggestion_id"] is None
99106

100107

101108
def test_list_notifications_shows_both_types(
@@ -104,10 +111,28 @@ def test_list_notifications_shows_both_types(
104111
make_maintainer_notification: Callable[..., list[Notification]],
105112
) -> None:
106113
user.profile.create_text_notification("Hello", "World")
107-
make_maintainer_notification(user)
114+
db_suggestion_notifications = make_maintainer_notification(user)
108115

109116
url = reverse("notification-list")
110117
response = client.get(url)
111118

112119
assert response.status_code == status.HTTP_200_OK
113120
assert response.data["count"] == 2
121+
122+
results = response.data["results"]
123+
text_result = next(r for r in results if r["type"] == NotificationType.TEXT)
124+
suggestion_result = next(
125+
r for r in results if r["type"] == NotificationType.SUGGESTION
126+
)
127+
128+
assert text_result["type"] == NotificationType.TEXT
129+
assert text_result["title"] == "Hello"
130+
assert text_result["message"] == "World"
131+
assert text_result["suggestion_id"] is None
132+
133+
assert suggestion_result["type"] == NotificationType.SUGGESTION
134+
assert (
135+
suggestion_result["suggestion_id"]
136+
== db_suggestion_notifications[0].suggestion_id
137+
)
138+
assert suggestion_result["message"] is None

0 commit comments

Comments
 (0)