feat: 调整站内信接口 (#6228)

* feat: 调整站内信接口

* 添加 websockt

* 添加信息类型字段

* 添加 has_read 过滤参数

* feat: 调整站内信接口

* 添加 websockt

* 添加信息类型字段

* 添加 has_read 过滤参数

* 去掉type websocket

* perf: 去掉type

Co-authored-by: xinwen <coderWen@126.com>
Co-authored-by: ibuler <ibuler@qq.com>
pull/6247/head^2
fit2bot 4 years ago committed by GitHub
parent 391a5cb7d0
commit af92271a52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,7 +23,7 @@ api_v1 = [
path('applications/', include('applications.urls.api_urls', namespace='api-applications')),
path('tickets/', include('tickets.urls.api_urls', namespace='api-tickets')),
path('acls/', include('acls.urls.api_urls', namespace='api-acls')),
path('notifications/', include('notifications.urls', namespace='api-notifications')),
path('notifications/', include('notifications.urls.notifications', namespace='api-notifications')),
path('prometheus/metrics/', api.PrometheusMetricsApi.as_view()),
]

@ -6,10 +6,11 @@ from common.permissions import IsValidUser
from common.const.http import GET, PATCH, POST
from common.drf.api import JmsGenericViewSet
from ..serializers import (
SiteMessageListSerializer, SiteMessageRetrieveSerializer, SiteMessageIdsSerializer,
SiteMessageDetailSerializer, SiteMessageIdsSerializer,
SiteMessageSendSerializer,
)
from ..site_msg import SiteMessage
from ..filters import SiteMsgFilter
__all__ = ('SiteMessageViewSet', )
@ -17,32 +18,24 @@ __all__ = ('SiteMessageViewSet', )
class SiteMessageViewSet(ListModelMixin, RetrieveModelMixin, JmsGenericViewSet):
permission_classes = (IsValidUser,)
serializer_classes = {
'retrieve': SiteMessageRetrieveSerializer,
'unread': SiteMessageListSerializer,
'list': SiteMessageListSerializer,
'default': SiteMessageDetailSerializer,
'mark_as_read': SiteMessageIdsSerializer,
'send': SiteMessageSendSerializer,
}
filterset_class = SiteMsgFilter
def get_queryset(self):
user = self.request.user
msgs = SiteMessage.get_user_all_msgs(user.id)
return msgs
@action(methods=[GET], detail=False)
def unread(self, request, **kwargs):
user = request.user
msgs = SiteMessage.get_user_unread_msgs(user.id)
msgs = self.filter_queryset(msgs)
return self.get_paginated_response_with_query_set(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)
return Response(data={'total': msgs.count()})
@action(methods=[PATCH], detail=False)
@action(methods=[PATCH], detail=False, url_path='mark-as-read')
def mark_as_read(self, request, **kwargs):
user = request.user
seri = self.get_serializer(data=request.data)

@ -0,0 +1,11 @@
import django_filters
from .models import SiteMessage
class SiteMsgFilter(django_filters.FilterSet):
has_read = django_filters.BooleanFilter(field_name='m2m_sitemessageusers__has_read')
class Meta:
model = SiteMessage
fields = ('has_read',)

@ -1,4 +1,5 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from common.db.models import JMSModel

@ -4,16 +4,24 @@ from rest_framework import serializers
from ..models import SiteMessage
class SiteMessageListSerializer(ModelSerializer):
class Meta:
model = SiteMessage
fields = ['id', 'subject', 'has_read', 'read_at']
class SenderMixin(ModelSerializer):
sender = serializers.SerializerMethodField()
def get_sender(self, site_msg):
sender = site_msg.sender
if sender:
return str(sender)
else:
return ''
class SiteMessageRetrieveSerializer(ModelSerializer):
class SiteMessageDetailSerializer(SenderMixin, ModelSerializer):
class Meta:
model = SiteMessage
fields = ['id', 'subject', 'message', 'has_read', 'read_at']
fields = [
'id', 'subject', 'message', 'has_read', 'read_at',
'date_created', 'date_updated', 'sender',
]
class SiteMessageIdsSerializer(serializers.Serializer):

@ -8,13 +8,14 @@ from .models import SiteMessage as SiteMessageModel, SiteMessageUsers
class SiteMessage:
@classmethod
def send_msg(cls, subject, message, user_ids=(), group_ids=(), sender=None, is_broadcast=False):
def send_msg(cls, subject, message, user_ids=(), group_ids=(),
sender=None, is_broadcast=False):
if not any((user_ids, group_ids, is_broadcast)):
raise ValueError('No recipient is specified')
site_msg = SiteMessageModel.objects.create(
subject=subject, message=message,
is_broadcast=is_broadcast, sender=sender
is_broadcast=is_broadcast, sender=sender,
)
if is_broadcast:

@ -2,7 +2,7 @@
from rest_framework_bulk.routes import BulkRouter
from django.urls import path
from . import api
from notifications import api
app_name = 'notifications'
Loading…
Cancel
Save