Skip to content

Commit

Permalink
refactor(notification): add type hint for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
retroinspect committed Sep 14, 2023
1 parent 344c1b1 commit 2ca89d9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
7 changes: 6 additions & 1 deletion apps/core/filters/notification.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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,
)
16 changes: 13 additions & 3 deletions apps/core/models/notification.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion apps/core/models/notification_log.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion apps/core/serializers/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions apps/core/views/viewsets/notification.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from typing import TYPE_CHECKING

from rest_framework import decorators, response, serializers, status, viewsets

from apps.core.filters.notification import NotificationFilter
from apps.core.models import Notification, NotificationReadLog
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()
Expand 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()

Expand All @@ -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()],
Expand All @@ -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,
Expand Down

0 comments on commit 2ca89d9

Please sign in to comment.