Notification api lari chiqarildi
This commit is contained in:
@@ -2,3 +2,4 @@ from .category import * # noqa
|
||||
from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .user import * # noqa
|
||||
from .notification import * # noqa
|
||||
|
||||
1
core/apps/api/serializers/notification/__init__.py
Normal file
1
core/apps/api/serializers/notification/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .natification import * # noqa
|
||||
49
core/apps/api/serializers/notification/natification.py
Normal file
49
core/apps/api/serializers/notification/natification.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from rest_framework import serializers
|
||||
from core.apps.accounts.models import UserNotification, Notification
|
||||
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = [
|
||||
"id",
|
||||
"title",
|
||||
"description",
|
||||
"long",
|
||||
"lat"
|
||||
|
||||
]
|
||||
|
||||
|
||||
class BaseUserNotificationSerializer(serializers.ModelSerializer):
|
||||
notification = NotificationSerializer(many=False, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = UserNotification
|
||||
fields = [
|
||||
"id",
|
||||
"is_read",
|
||||
"notification",
|
||||
"created_at",
|
||||
]
|
||||
|
||||
|
||||
class ListUserNotificationSerializer(BaseUserNotificationSerializer):
|
||||
class Meta(BaseUserNotificationSerializer.Meta): ...
|
||||
|
||||
|
||||
class RetrieveUserNotificationSerializer(BaseUserNotificationSerializer):
|
||||
class Meta(BaseUserNotificationSerializer.Meta): ...
|
||||
|
||||
|
||||
class CreateUserNotificationSerializer(BaseUserNotificationSerializer):
|
||||
class Meta(BaseUserNotificationSerializer.Meta): ...
|
||||
|
||||
|
||||
class UpdateUserNotificationSerializer(BaseUserNotificationSerializer):
|
||||
class Meta(BaseUserNotificationSerializer.Meta):
|
||||
fields = [
|
||||
"is_read"
|
||||
]
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
from .test_user_like import * # noqa
|
||||
from .test_user_notification import * # noqa
|
||||
|
||||
@@ -46,11 +46,10 @@ def test_list(data):
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_create(data,ad):
|
||||
def test_create(data, ad):
|
||||
urls, client, instance = data
|
||||
response = client.post(urls["list"], data={"ad": ad.pk})
|
||||
data_resp = response.json()
|
||||
print(data_resp)
|
||||
assert response.status_code == 201
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
78
core/apps/api/tests/user/test_user_notification.py
Normal file
78
core/apps/api/tests/user/test_user_notification.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from core.apps.accounts.models import UserNotification
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def instance(db):
|
||||
return UserNotification._baker()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_client(instance):
|
||||
client = APIClient()
|
||||
client.force_authenticate(user=instance.user)
|
||||
return client, instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data(api_client):
|
||||
client, instance = api_client
|
||||
return (
|
||||
{
|
||||
"list": reverse("notification-list"),
|
||||
"retrieve": reverse("notification-detail", kwargs={"pk": instance.pk}),
|
||||
"retrieve-not-found": reverse("notification-detail", kwargs={"pk": 1000}),
|
||||
"notification-read": reverse("notification-notification-read", kwargs={"pk": instance.pk}),
|
||||
"all-read": reverse("notification-all-read"),
|
||||
},
|
||||
client,
|
||||
instance,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_list(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["list"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_retrieve(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["retrieve"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_retrieve_not_found(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["retrieve-not-found"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 404
|
||||
assert data_resp["status"] is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_notification_reads(data):
|
||||
urls, client, _ = data
|
||||
response = client.post(urls["notification-read"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_all_read(data):
|
||||
urls, client, _ = data
|
||||
response = client.post(urls["all-read"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
@@ -2,9 +2,10 @@ from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from core.apps.api.views import CategoryHomeApiViewSet, CategoryViewSet, HomeAdApiView, SearchHistoryViewSet, \
|
||||
UserLikeViewSet
|
||||
UserLikeViewSet, NotificationViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("notification", NotificationViewSet, basename="notification")
|
||||
router.register("user-like", UserLikeViewSet, basename="user-like")
|
||||
router.register("category", CategoryViewSet, basename="category")
|
||||
router.register("category-home", CategoryHomeApiViewSet, basename="category-home")
|
||||
|
||||
@@ -2,3 +2,4 @@ from .category import * # noqa
|
||||
from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .user import * # noqa
|
||||
from .notification import * # noqa
|
||||
|
||||
1
core/apps/api/views/notification/__init__.py
Normal file
1
core/apps/api/views/notification/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .notification import * # noqa
|
||||
54
core/apps/api/views/notification/notification.py
Normal file
54
core/apps/api/views/notification/notification.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from xmlrpc.client import Fault
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.viewsets import ReadOnlyModelViewSet
|
||||
from django_core.mixins.base import BaseViewSetMixin
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from core.apps.accounts.models import UserNotification
|
||||
from rest_framework.decorators import action
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from core.apps.api.serializers.notification import (
|
||||
ListUserNotificationSerializer,
|
||||
CreateUserNotificationSerializer,
|
||||
RetrieveUserNotificationSerializer,
|
||||
UpdateUserNotificationSerializer
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=["Notification"])
|
||||
class NotificationViewSet(BaseViewSetMixin, ReadOnlyModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = ListUserNotificationSerializer
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
|
||||
action_permission_classes = {}
|
||||
action_serializer_class = {
|
||||
"list": ListUserNotificationSerializer,
|
||||
"retrieve": RetrieveUserNotificationSerializer,
|
||||
"create": CreateUserNotificationSerializer,
|
||||
}
|
||||
|
||||
def get_queryset(self):
|
||||
qs = UserNotification.objects.filter(user=self.request.user).order_by("-created_at")
|
||||
return qs
|
||||
|
||||
@action(detail=True, methods=["post"])
|
||||
def notification_read(self, request, pk=None):
|
||||
notification = self.get_object()
|
||||
serializer = UpdateUserNotificationSerializer(
|
||||
notification,
|
||||
data={"is_read": True},
|
||||
partial=True
|
||||
)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=False, methods=["post"])
|
||||
def all_read(self, request):
|
||||
user = request.user
|
||||
UserNotification.objects.filter(user=user, is_read=False).update(is_read=True)
|
||||
return Response({"detail": _("Barcha xabarlar o'qilgan qilindi!")}, status=status.HTTP_200_OK)
|
||||
Reference in New Issue
Block a user