From 9f046073c56434a7def2559b6bcc55ab640fcb3b Mon Sep 17 00:00:00 2001 From: sm4640 Date: Fri, 2 May 2025 22:07:09 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[#41]=20notification=20?= =?UTF-8?q?=EC=95=B1=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EC=95=8C=EB=A6=BC?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notifications/__init__.py | 0 notifications/admin.py | 3 +++ notifications/apps.py | 6 ++++++ notifications/models.py | 11 +++++++++++ notifications/serializers.py | 32 ++++++++++++++++++++++++++++++++ notifications/services.py | 19 +++++++++++++++++++ notifications/tests.py | 3 +++ notifications/urls.py | 14 ++++++++++++++ notifications/views.py | 34 ++++++++++++++++++++++++++++++++++ 9 files changed, 122 insertions(+) create mode 100644 notifications/__init__.py create mode 100644 notifications/admin.py create mode 100644 notifications/apps.py create mode 100644 notifications/models.py create mode 100644 notifications/serializers.py create mode 100644 notifications/services.py create mode 100644 notifications/tests.py create mode 100644 notifications/urls.py create mode 100644 notifications/views.py diff --git a/notifications/__init__.py b/notifications/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notifications/admin.py b/notifications/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/notifications/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/notifications/apps.py b/notifications/apps.py new file mode 100644 index 0000000..001b4f9 --- /dev/null +++ b/notifications/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class NotificationsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'notifications' diff --git a/notifications/models.py b/notifications/models.py new file mode 100644 index 0000000..e426773 --- /dev/null +++ b/notifications/models.py @@ -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) \ No newline at end of file diff --git a/notifications/serializers.py b/notifications/serializers.py new file mode 100644 index 0000000..4bbd23c --- /dev/null +++ b/notifications/serializers.py @@ -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'] + \ No newline at end of file diff --git a/notifications/services.py b/notifications/services.py new file mode 100644 index 0000000..ff353af --- /dev/null +++ b/notifications/services.py @@ -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 + ) \ No newline at end of file diff --git a/notifications/tests.py b/notifications/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/notifications/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notifications/urls.py b/notifications/urls.py new file mode 100644 index 0000000..c2d898b --- /dev/null +++ b/notifications/urls.py @@ -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)), +] \ No newline at end of file diff --git a/notifications/views.py b/notifications/views.py new file mode 100644 index 0000000..aae2dfb --- /dev/null +++ b/notifications/views.py @@ -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) \ No newline at end of file