diff --git a/apps/core/models/notification.py b/apps/core/models/notification.py index 22b91c92..8fa37b0d 100644 --- a/apps/core/models/notification.py +++ b/apps/core/models/notification.py @@ -1,9 +1,18 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + from django.db import models from django.utils.functional import cached_property +from apps.core.models import Article, Comment +from apps.core.models.board import NameType from ara.db.models import MetaDataModel from ara.firebase import fcm_notify_comment +if TYPE_CHECKING: + from apps.user.models import UserProfile + TYPE_CHOICES = ( ("default", "default"), ("article_commented", "article_commented"), @@ -12,41 +21,41 @@ class Notification(MetaDataModel): - class Meta(MetaDataModel.Meta): - verbose_name = "알림" - verbose_name_plural = "알림 목록" - type = models.CharField( + verbose_name="알림 종류", choices=TYPE_CHOICES, default="default", max_length=32, - verbose_name="알림 종류", ) title = models.CharField( - max_length=256, verbose_name="제목", + max_length=256, ) content = models.TextField( verbose_name="내용", ) related_article = models.ForeignKey( - on_delete=models.CASCADE, + verbose_name="알림 관련 제보", to="core.Article", + on_delete=models.CASCADE, + related_name="notification_set", null=True, db_index=True, - related_name="notification_set", - verbose_name="알림 관련 제보", ) related_comment = models.ForeignKey( - on_delete=models.CASCADE, + verbose_name="알림 관련 댓글", to="core.Comment", + on_delete=models.CASCADE, + related_name="notification_set", null=True, db_index=True, - related_name="notification_set", - verbose_name="알림 관련 댓글", ) + class Meta(MetaDataModel.Meta): + verbose_name = "알림" + verbose_name_plural = "알림 목록" + @cached_property def data(self) -> dict: return { @@ -56,12 +65,23 @@ def data(self) -> dict: "click_action": "", } + @staticmethod + def get_display_name(article: Article, profile: UserProfile): + if article.name_type == NameType.REALNAME: + return profile.realname + elif article.name_type == NameType.REGULAR: + return profile.nickname + else: + return "익명" + @classmethod def notify_commented(cls, comment): from apps.core.models import NotificationReadLog - def notify_article_commented(_parent_article, _comment): - title = f"{_comment.created_by.profile.nickname} 님이 새로운 댓글을 작성했습니다." + def notify_article_commented(_parent_article: Article, _comment: Comment): + name = cls.get_display_name(_parent_article, _comment.created_by.profile) + title = f"{name} 님이 새로운 댓글을 작성했습니다." + NotificationReadLog.objects.create( read_by=_parent_article.created_by, notification=cls.objects.create( @@ -79,8 +99,10 @@ def notify_article_commented(_parent_article, _comment): f"post/{_parent_article.id}", ) - def notify_comment_commented(_parent_article, _comment): - title = f"{_comment.created_by.profile.nickname} 님이 새로운 대댓글을 작성했습니다." + def notify_comment_commented(_parent_article: Article, _comment: Comment): + name = cls.get_display_name(_parent_article, _comment.created_by.profile) + title = f"{name} 님이 새로운 대댓글을 작성했습니다." + NotificationReadLog.objects.create( read_by=_comment.parent_comment.created_by, notification=cls.objects.create(