Initial commit - clean project structure
This commit is contained in:
37
notifications/views.py
Normal file
37
notifications/views.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from .models import Notification
|
||||
from .serializers import MarkReadResponseSerializer, NotificationListResponseSerializer
|
||||
|
||||
|
||||
class NotificationListView(APIView):
|
||||
"""GET /api/notifications — فهرست اعلانهای کاربر (جدیدترین اول)"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@extend_schema(responses=NotificationListResponseSerializer, tags=['Notifications (notifications)'])
|
||||
def get(self, request):
|
||||
qs = Notification.objects.filter(user_id=request.user.id)
|
||||
unread_count = qs.filter(is_read=False).count()
|
||||
|
||||
data = {'items': list(qs), 'unreadCount': unread_count}
|
||||
return Response(NotificationListResponseSerializer(data).data)
|
||||
|
||||
|
||||
class NotificationMarkReadView(APIView):
|
||||
"""PATCH /api/notifications/{id}/read — علامتگذاری یک اعلان بهعنوان خواندهشده"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@extend_schema(responses=MarkReadResponseSerializer, tags=['Notifications (notifications)'])
|
||||
def patch(self, request, id):
|
||||
notification = get_object_or_404(Notification, pk=id, user_id=request.user.id)
|
||||
if not notification.is_read:
|
||||
notification.is_read = True
|
||||
notification.save(update_fields=['is_read'])
|
||||
|
||||
return Response(MarkReadResponseSerializer({'success': True}).data)
|
||||
Reference in New Issue
Block a user