From 7dddf0c3c281897002e070104b261d4172b139a0 Mon Sep 17 00:00:00 2001 From: xinwen Date: Wed, 9 Jun 2021 20:51:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AB=99=E5=86=85=E4=BF=A1=E6=9C=AA?= =?UTF-8?q?=E8=AF=BB=E4=BF=A1=E6=81=AF=E8=AE=A1=E6=95=B0=E4=B8=8D=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/notifications/api/site_msgs.py | 10 ++++++++-- apps/notifications/filters.py | 11 +++++++++-- apps/notifications/site_msg.py | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/apps/notifications/api/site_msgs.py b/apps/notifications/api/site_msgs.py index 5618a70b4..6ee856922 100644 --- a/apps/notifications/api/site_msgs.py +++ b/apps/notifications/api/site_msgs.py @@ -2,6 +2,7 @@ from rest_framework.response import Response from rest_framework.mixins import ListModelMixin, RetrieveModelMixin from rest_framework.decorators import action +from common.http import is_true from common.permissions import IsValidUser from common.const.http import GET, PATCH, POST from common.drf.api import JmsGenericViewSet @@ -26,13 +27,18 @@ class SiteMessageViewSet(ListModelMixin, RetrieveModelMixin, JmsGenericViewSet): def get_queryset(self): user = self.request.user - msgs = SiteMessage.get_user_all_msgs(user.id) + has_read = self.request.query_params.get('has_read') + + if has_read is None: + msgs = SiteMessage.get_user_all_msgs(user.id) + else: + msgs = SiteMessage.filter_user_msgs(user.id, has_read=is_true(has_read)) return msgs @action(methods=[GET], detail=False, url_path='unread-total') def unread_total(self, request, **kwargs): user = request.user - msgs = SiteMessage.get_user_unread_msgs(user.id) + msgs = SiteMessage.filter_user_msgs(user.id, has_read=False) return Response(data={'total': msgs.count()}) @action(methods=[PATCH], detail=False, url_path='mark-as-read') diff --git a/apps/notifications/filters.py b/apps/notifications/filters.py index b14d47943..c28b8a3f2 100644 --- a/apps/notifications/filters.py +++ b/apps/notifications/filters.py @@ -1,10 +1,17 @@ import django_filters +from common.drf.filters import BaseFilterSet from .models import SiteMessage -class SiteMsgFilter(django_filters.FilterSet): - has_read = django_filters.BooleanFilter(field_name='m2m_sitemessageusers__has_read') +class SiteMsgFilter(BaseFilterSet): + # 不用 Django 的关联表过滤,有个小bug,会重复关联相同表 + # SELECT DISTINCT * FROM `notifications_sitemessage` + # INNER JOIN `notifications_sitemessageusers` ON (`notifications_sitemessage`.`id` = `notifications_sitemessageusers`.`sitemessage_id`) + # INNER JOIN `notifications_sitemessageusers` T4 ON (`notifications_sitemessage`.`id` = T4.`sitemessage_id`) + # WHERE (`notifications_sitemessageusers`.`user_id` = '40c8f140dfa246d4861b80f63cf4f6e3' AND NOT T4.`has_read`) + # ORDER BY `notifications_sitemessage`.`date_created` DESC LIMIT 15; + has_read = django_filters.BooleanFilter(method='do_nothing') class Meta: model = SiteMessage diff --git a/apps/notifications/site_msg.py b/apps/notifications/site_msg.py index 3767cc322..b78d3c7f4 100644 --- a/apps/notifications/site_msg.py +++ b/apps/notifications/site_msg.py @@ -51,10 +51,10 @@ class SiteMessage: return site_msgs_count @classmethod - def get_user_unread_msgs(cls, user_id): + def filter_user_msgs(cls, user_id, has_read=False): site_msgs = SiteMessageModel.objects.filter( m2m_sitemessageusers__user_id=user_id, - m2m_sitemessageusers__has_read=False + m2m_sitemessageusers__has_read=has_read ).distinct().annotate( has_read=F('m2m_sitemessageusers__has_read'), read_at=F('m2m_sitemessageusers__read_at')