✨ Feat: [#41] notification 앱 생성 및 알림 조회 구현
This commit is contained in:
0
notifications/__init__.py
Normal file
0
notifications/__init__.py
Normal file
3
notifications/admin.py
Normal file
3
notifications/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
notifications/apps.py
Normal file
6
notifications/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'notifications'
|
||||
11
notifications/models.py
Normal file
11
notifications/models.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.db import models
|
||||
|
||||
from users.models import *
|
||||
|
||||
from common.models.baseModels import *
|
||||
|
||||
class Notification(BaseModel):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
|
||||
content = models.TextField(blank=True)
|
||||
is_read = models.BooleanField(default=False)
|
||||
note_type = models.CharField(max_length=10, choices=NotificationType.choices)
|
||||
32
notifications/serializers.py
Normal file
32
notifications/serializers.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from .models import *
|
||||
from projects.models import *
|
||||
from rest_framework import serializers
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
meta = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = ['id', 'content', 'note_type', 'is_read', 'meta']
|
||||
|
||||
def get_meta(self, obj):
|
||||
|
||||
REL_SERIALIZER_MAP = {
|
||||
'project_invitation' : ProjectInvitationMetaSerializer,
|
||||
}
|
||||
|
||||
for rel_name, serializer_cls in REL_SERIALIZER_MAP.items():
|
||||
rel_obj = getattr(obj, rel_name, None)
|
||||
if rel_obj is not None:
|
||||
return serializer_cls(rel_obj).data
|
||||
return None
|
||||
|
||||
class ProjectInvitationMetaSerializer(serializers.ModelSerializer):
|
||||
project_invitation_id = serializers.CharField(source='id')
|
||||
project_title = serializers.CharField(source='project.title')
|
||||
from_user_nickname = serializers.CharField(source='from_user.nickname')
|
||||
|
||||
class Meta:
|
||||
model = ProjectInvitation
|
||||
fields = ['project_invitation_id', 'project_title', 'from_user_nickname', 'status']
|
||||
|
||||
19
notifications/services.py
Normal file
19
notifications/services.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from users.models import *
|
||||
from .models import *
|
||||
|
||||
from common.models.choiceModels import *
|
||||
|
||||
|
||||
|
||||
# 알림 관련 서비스 로직
|
||||
class NotifiationService:
|
||||
@staticmethod
|
||||
def set_content():
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def create_notification(user: User, note_type: NotificationType):
|
||||
return Notification.objects.create(
|
||||
user = user,
|
||||
note_type=note_type
|
||||
)
|
||||
3
notifications/tests.py
Normal file
3
notifications/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
14
notifications/urls.py
Normal file
14
notifications/urls.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from .views import *
|
||||
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
app_name = 'notifications'
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'', NotificationReadViewSet, basename='notification')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
34
notifications/views.py
Normal file
34
notifications/views.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.db import transaction
|
||||
|
||||
from rest_framework.viewsets import ReadOnlyModelViewSet
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
from .serializers import *
|
||||
from .models import *
|
||||
from .services import *
|
||||
|
||||
from users.services import *
|
||||
|
||||
|
||||
# Create your views here.
|
||||
class NotificationReadViewSet(ReadOnlyModelViewSet):
|
||||
serializer_class = NotificationSerializer
|
||||
|
||||
# 30일 이전 알림만 가져옴
|
||||
def get_queryset(self):
|
||||
qs = UserToNotificationService.get_all_notification(self.request.user)
|
||||
print(qs)
|
||||
return qs.select_related(
|
||||
'project_invitation__project',
|
||||
'project_invitation__from_user',
|
||||
)
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
instance.is_read = True
|
||||
instance.save()
|
||||
serializer = self.get_serializer(instance)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
Reference in New Issue
Block a user