mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-09 03:09:34 +00:00
feat: 站内信 (#6183)
* 添加站内信 * s * s * 添加接口 * fix * fix * 重构了一些 * 完成 * 完善 * s * s * s * s * s * s * 测试ok * 替换业务中发送消息的方式 * 修改 * s * 去掉 update 兼容 create * 添加 unread total 接口 * 调整json字段 Co-authored-by: xinwen <coderWen@126.com>
This commit is contained in:
2
apps/notifications/api/__init__.py
Normal file
2
apps/notifications/api/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .notifications import *
|
||||
from .site_msgs import *
|
72
apps/notifications/api/notifications.py
Normal file
72
apps/notifications/api/notifications.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from django.http import Http404
|
||||
from rest_framework.mixins import ListModelMixin, UpdateModelMixin
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
from common.drf.api import JmsGenericViewSet
|
||||
from notifications.notifications import system_msgs
|
||||
from notifications.models import SystemMsgSubscription
|
||||
from notifications.backends import BACKEND
|
||||
from notifications.serializers import (
|
||||
SystemMsgSubscriptionSerializer, SystemMsgSubscriptionByCategorySerializer
|
||||
)
|
||||
|
||||
__all__ = ('BackendListView', 'SystemMsgSubscriptionViewSet')
|
||||
|
||||
|
||||
class BackendListView(APIView):
|
||||
def get(self, request):
|
||||
data = [
|
||||
{
|
||||
'name': backend,
|
||||
'name_display': backend.label
|
||||
}
|
||||
for backend in BACKEND
|
||||
if backend.is_enable
|
||||
]
|
||||
return Response(data=data)
|
||||
|
||||
|
||||
class SystemMsgSubscriptionViewSet(ListModelMixin,
|
||||
UpdateModelMixin,
|
||||
JmsGenericViewSet):
|
||||
lookup_field = 'message_type'
|
||||
queryset = SystemMsgSubscription.objects.all()
|
||||
serializer_classes = {
|
||||
'list': SystemMsgSubscriptionByCategorySerializer,
|
||||
'update': SystemMsgSubscriptionSerializer,
|
||||
'partial_update': SystemMsgSubscriptionSerializer
|
||||
}
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
data = []
|
||||
category_children_mapper = {}
|
||||
|
||||
subscriptions = self.get_queryset()
|
||||
msgtype_sub_mapper = {}
|
||||
for sub in subscriptions:
|
||||
msgtype_sub_mapper[sub.message_type] = sub
|
||||
|
||||
for msg in system_msgs:
|
||||
message_type = msg['message_type']
|
||||
message_type_label = msg['message_type_label']
|
||||
category = msg['category']
|
||||
category_label = msg['category_label']
|
||||
|
||||
if category not in category_children_mapper:
|
||||
children = []
|
||||
|
||||
data.append({
|
||||
'category': category,
|
||||
'category_label': category_label,
|
||||
'children': children
|
||||
})
|
||||
category_children_mapper[category] = children
|
||||
|
||||
sub = msgtype_sub_mapper[message_type]
|
||||
sub.message_type_label = message_type_label
|
||||
category_children_mapper[category].append(sub)
|
||||
|
||||
serializer = self.get_serializer(data, many=True)
|
||||
return Response(data=serializer.data)
|
59
apps/notifications/api/site_msgs.py
Normal file
59
apps/notifications/api/site_msgs.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
||||
from rest_framework.decorators import action
|
||||
|
||||
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,
|
||||
SiteMessageSendSerializer,
|
||||
)
|
||||
from ..site_msg import SiteMessage
|
||||
|
||||
__all__ = ('SiteMessageViewSet', )
|
||||
|
||||
|
||||
class SiteMessageViewSet(ListModelMixin, RetrieveModelMixin, JmsGenericViewSet):
|
||||
permission_classes = (IsValidUser,)
|
||||
serializer_classes = {
|
||||
'retrieve': SiteMessageRetrieveSerializer,
|
||||
'unread': SiteMessageListSerializer,
|
||||
'list': SiteMessageListSerializer,
|
||||
'mark_as_read': SiteMessageIdsSerializer,
|
||||
'send': SiteMessageSendSerializer,
|
||||
}
|
||||
|
||||
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)
|
||||
def mark_as_read(self, request, **kwargs):
|
||||
user = request.user
|
||||
seri = self.get_serializer(data=request.data)
|
||||
seri.is_valid(raise_exception=True)
|
||||
ids = seri.validated_data['ids']
|
||||
SiteMessage.mark_msgs_as_read(user.id, ids)
|
||||
return Response({'detail': 'ok'})
|
||||
|
||||
@action(methods=[POST], detail=False)
|
||||
def send(self, request, **kwargs):
|
||||
seri = self.get_serializer(data=request.data)
|
||||
seri.is_valid(raise_exception=True)
|
||||
SiteMessage.send_msg(**seri.validated_data, sender=request.user)
|
||||
return Response({'detail': 'ok'})
|
Reference in New Issue
Block a user