From 2ca89d9e76621eed25dbc400f2d61c1e2f169a6b Mon Sep 17 00:00:00 2001 From: retroinspect Date: Thu, 14 Sep 2023 02:25:49 +0000 Subject: [PATCH] refactor(notification): add type hint for notification --- apps/core/filters/notification.py | 7 ++++++- apps/core/models/notification.py | 16 +++++++++++++--- apps/core/models/notification_log.py | 9 ++++++++- apps/core/serializers/notification.py | 2 +- apps/core/views/viewsets/notification.py | 12 +++++++++--- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/apps/core/filters/notification.py b/apps/core/filters/notification.py index 9042dbe6..b4abee61 100644 --- a/apps/core/filters/notification.py +++ b/apps/core/filters/notification.py @@ -1,7 +1,12 @@ +from typing import TYPE_CHECKING + from django_filters.rest_framework import BooleanFilter, FilterSet from apps.core.models import Notification +if TYPE_CHECKING: + from django.db.models import QuerySet + class NotificationFilter(FilterSet): class Meta: @@ -34,7 +39,7 @@ class Meta: ) @staticmethod - def get_is_read(queryset, field_name, value): + def get_is_read(queryset: QuerySet, field_name: str, value: str) -> QuerySet: return queryset.filter( notification_read_log_set__is_read=value, ) diff --git a/apps/core/models/notification.py b/apps/core/models/notification.py index 22b91c92..f17e9957 100644 --- a/apps/core/models/notification.py +++ b/apps/core/models/notification.py @@ -1,9 +1,15 @@ +from typing import TYPE_CHECKING + from django.db import models from django.utils.functional import cached_property from ara.db.models import MetaDataModel from ara.firebase import fcm_notify_comment +if TYPE_CHECKING: + from apps.core.models.article import Article + from apps.core.models.comment import Comment + TYPE_CHOICES = ( ("default", "default"), ("article_commented", "article_commented"), @@ -57,10 +63,12 @@ def data(self) -> dict: } @classmethod - def notify_commented(cls, comment): + def notify_commented(cls, comment: Comment): from apps.core.models import NotificationReadLog - def notify_article_commented(_parent_article, _comment): + def notify_article_commented( + _parent_article: Article, _comment: Comment + ) -> None: title = f"{_comment.created_by.profile.nickname} 님이 새로운 댓글을 작성했습니다." NotificationReadLog.objects.create( read_by=_parent_article.created_by, @@ -79,7 +87,9 @@ def notify_article_commented(_parent_article, _comment): f"post/{_parent_article.id}", ) - def notify_comment_commented(_parent_article, _comment): + def notify_comment_commented( + _parent_article: Article, _comment: Comment + ) -> None: title = f"{_comment.created_by.profile.nickname} 님이 새로운 대댓글을 작성했습니다." NotificationReadLog.objects.create( read_by=_comment.parent_comment.created_by, diff --git a/apps/core/models/notification_log.py b/apps/core/models/notification_log.py index 97b8fc10..5e22ac59 100644 --- a/apps/core/models/notification_log.py +++ b/apps/core/models/notification_log.py @@ -1,8 +1,13 @@ +from typing import TYPE_CHECKING + from django.conf import settings from django.db import models from ara.db.models import MetaDataModel +if TYPE_CHECKING: + from apps.user.models import user + class NotificationReadLog(MetaDataModel): class Meta(MetaDataModel.Meta): @@ -31,7 +36,9 @@ class Meta(MetaDataModel.Meta): ) @classmethod - def prefetch_my_notification_read_log(cls, user, prefix="") -> models.Prefetch: + def prefetch_my_notification_read_log( + cls, user: user, prefix="" + ) -> models.Prefetch: return models.Prefetch( "{}notification_read_log_set".format(prefix), queryset=NotificationReadLog.objects.filter( diff --git a/apps/core/serializers/notification.py b/apps/core/serializers/notification.py index 4bb1df89..ccfd5803 100644 --- a/apps/core/serializers/notification.py +++ b/apps/core/serializers/notification.py @@ -9,7 +9,7 @@ class Meta: model = Notification fields = "__all__" - def get_is_read(self, obj) -> bool | None: + def get_is_read(self, obj: Notification) -> bool | None: if not obj.notification_read_log_set.exists(): return None diff --git a/apps/core/views/viewsets/notification.py b/apps/core/views/viewsets/notification.py index 3425dcf4..713e0e2e 100644 --- a/apps/core/views/viewsets/notification.py +++ b/apps/core/views/viewsets/notification.py @@ -1,3 +1,5 @@ +from typing import TYPE_CHECKING + from rest_framework import decorators, response, serializers, status, viewsets from apps.core.filters.notification import NotificationFilter @@ -5,6 +7,10 @@ from apps.core.serializers.notification import NotificationSerializer from ara.classes.viewset import ActionAPIViewSet +if TYPE_CHECKING: + from django.db.models import QuerySet + from rest_framework.request import HttpRequest + class NotificationViewSet(viewsets.ReadOnlyModelViewSet, ActionAPIViewSet): queryset = Notification.objects.all() @@ -15,7 +21,7 @@ class NotificationViewSet(viewsets.ReadOnlyModelViewSet, ActionAPIViewSet): "read_all": serializers.Serializer, } - def get_queryset(self): + def get_queryset(self) -> QuerySet: if not self.request.user.is_authenticated: return Notification.objects.none() @@ -40,7 +46,7 @@ def get_queryset(self): return queryset @decorators.action(detail=False, methods=["post"]) - def read_all(self, request, *args, **kwargs): + def read_all(self, request: HttpRequest, *args, **kwargs) -> response.Response: notification_read_logs = NotificationReadLog.objects.filter( read_by=request.user, notification__in=[notification.id for notification in self.get_queryset()], @@ -53,7 +59,7 @@ def read_all(self, request, *args, **kwargs): ) @decorators.action(detail=True, methods=["post"]) - def read(self, request, *args, **kwargs): + def read(self, request: HttpRequest, *args, **kwargs) -> response.Response: try: notification_read_log = self.get_object().notification_read_log_set.get( read_by=request.user,