From 8b4811a4840c83798a172a20bf2817fec22fe33a Mon Sep 17 00:00:00 2001 From: DoyunShin Date: Sat, 24 Feb 2024 19:22:36 +0000 Subject: [PATCH 1/3] fix(notification): Fix notification shows nickname while other name_type on board --- apps/core/models/notification.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/core/models/notification.py b/apps/core/models/notification.py index 22b91c92..ce486027 100644 --- a/apps/core/models/notification.py +++ b/apps/core/models/notification.py @@ -1,6 +1,8 @@ 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 @@ -56,12 +58,26 @@ def data(self) -> dict: "click_action": "", } + @classmethod + def check_username(article: Article, comment: Comment, title: str) -> str: + if article.name_type == NameType.REALNAME: + title.format(user=comment.created_by.profile.realname) + elif article.name_type == NameType.REGULAR: + title.format(user=comment.created_by.profile.nickname) + else: + title.format(user="익명") + + return title + @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): + title = cls.check_username( + _parent_article, _comment, "{user} 님이 새로운 댓글을 작성했습니다." + ) + NotificationReadLog.objects.create( read_by=_parent_article.created_by, notification=cls.objects.create( @@ -79,8 +95,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): + title = cls.check_username( + _parent_article, _comment, "{user} 님이 새로운 대댓글을 작성했습니다." + ) NotificationReadLog.objects.create( read_by=_comment.parent_comment.created_by, notification=cls.objects.create( From d367801f02805fc3bb678359a7a87c989d7a4442 Mon Sep 17 00:00:00 2001 From: yuwol Date: Mon, 11 Mar 2024 21:34:55 +0900 Subject: [PATCH 2/3] chore(notification): follow Django coding style --- apps/core/models/notification.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/core/models/notification.py b/apps/core/models/notification.py index ce486027..0784f547 100644 --- a/apps/core/models/notification.py +++ b/apps/core/models/notification.py @@ -14,41 +14,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 { From 60f9c2be731271d09f9d8c99b73b7bfdbefa7496 Mon Sep 17 00:00:00 2001 From: yuwol Date: Tue, 12 Mar 2024 21:37:37 +0900 Subject: [PATCH 3/3] fix(notification): fix classmethod to staticmethod --- apps/core/models/notification.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/apps/core/models/notification.py b/apps/core/models/notification.py index 0784f547..8fa37b0d 100644 --- a/apps/core/models/notification.py +++ b/apps/core/models/notification.py @@ -1,3 +1,7 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + from django.db import models from django.utils.functional import cached_property @@ -6,6 +10,9 @@ 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"), @@ -58,25 +65,22 @@ def data(self) -> dict: "click_action": "", } - @classmethod - def check_username(article: Article, comment: Comment, title: str) -> str: + @staticmethod + def get_display_name(article: Article, profile: UserProfile): if article.name_type == NameType.REALNAME: - title.format(user=comment.created_by.profile.realname) + return profile.realname elif article.name_type == NameType.REGULAR: - title.format(user=comment.created_by.profile.nickname) + return profile.nickname else: - title.format(user="익명") - - return title + return "익명" @classmethod def notify_commented(cls, comment): from apps.core.models import NotificationReadLog def notify_article_commented(_parent_article: Article, _comment: Comment): - title = cls.check_username( - _parent_article, _comment, "{user} 님이 새로운 댓글을 작성했습니다." - ) + name = cls.get_display_name(_parent_article, _comment.created_by.profile) + title = f"{name} 님이 새로운 댓글을 작성했습니다." NotificationReadLog.objects.create( read_by=_parent_article.created_by, @@ -96,9 +100,9 @@ def notify_article_commented(_parent_article: Article, _comment: Comment): ) def notify_comment_commented(_parent_article: Article, _comment: Comment): - title = cls.check_username( - _parent_article, _comment, "{user} 님이 새로운 대댓글을 작성했습니다." - ) + 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(