2021-08-24 06:20:54 +00:00
|
|
|
from rest_framework.mixins import ListModelMixin, UpdateModelMixin, RetrieveModelMixin
|
2021-05-31 09:20:38 +00:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.drf.api import JMSGenericViewSet
|
2021-08-24 06:20:54 +00:00
|
|
|
from common.permissions import IsObjectOwner, IsSuperUser, OnlySuperUserCanList
|
2021-05-31 09:20:38 +00:00
|
|
|
from notifications.notifications import system_msgs
|
2021-08-24 06:20:54 +00:00
|
|
|
from notifications.models import SystemMsgSubscription, UserMsgSubscription
|
2021-05-31 09:20:38 +00:00
|
|
|
from notifications.backends import BACKEND
|
|
|
|
from notifications.serializers import (
|
2021-08-24 06:20:54 +00:00
|
|
|
SystemMsgSubscriptionSerializer, SystemMsgSubscriptionByCategorySerializer,
|
|
|
|
UserMsgSubscriptionSerializer,
|
2021-05-31 09:20:38 +00:00
|
|
|
)
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
__all__ = (
|
|
|
|
'BackendListView', 'SystemMsgSubscriptionViewSet',
|
|
|
|
'UserMsgSubscriptionViewSet', 'get_all_test_messages'
|
|
|
|
)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
2021-07-27 08:06:00 +00:00
|
|
|
JMSGenericViewSet):
|
2021-05-31 09:20:38 +00:00
|
|
|
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)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserMsgSubscriptionViewSet(ListModelMixin,
|
|
|
|
RetrieveModelMixin,
|
|
|
|
UpdateModelMixin,
|
|
|
|
JMSGenericViewSet):
|
|
|
|
lookup_field = 'user_id'
|
|
|
|
queryset = UserMsgSubscription.objects.all()
|
|
|
|
serializer_class = UserMsgSubscriptionSerializer
|
|
|
|
permission_classes = (IsObjectOwner | IsSuperUser, OnlySuperUserCanList)
|
2021-10-22 12:06:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_all_test_messages(request):
|
|
|
|
import textwrap
|
|
|
|
from ..notifications import Message
|
|
|
|
from django.shortcuts import HttpResponse
|
|
|
|
|
|
|
|
msgs_cls = Message.get_all_sub_messages()
|
|
|
|
html_data = '<h3>HTML 格式 </h3>'
|
|
|
|
text_data = '<h3>Text 格式</h3>'
|
|
|
|
|
|
|
|
for msg_cls in msgs_cls:
|
|
|
|
try:
|
|
|
|
msg = msg_cls.gen_test_msg()
|
|
|
|
if not msg:
|
|
|
|
continue
|
|
|
|
msg_html = msg.html_msg_with_sign['message']
|
|
|
|
msg_text = msg.text_msg_with_sign['message']
|
|
|
|
except NotImplementedError:
|
|
|
|
msg_html = msg_text = '没有实现方法'
|
|
|
|
except Exception as e:
|
|
|
|
msg_html = msg_text = 'Error: ' + str(e)
|
|
|
|
|
|
|
|
html_data += """
|
|
|
|
<h3>{}</h3>
|
|
|
|
{}
|
|
|
|
<hr />
|
|
|
|
""".format(msg_cls.__name__, msg_html)
|
|
|
|
|
|
|
|
text_data += textwrap.dedent("""
|
|
|
|
<h3>{}</h3>
|
|
|
|
<pre>
|
|
|
|
{}
|
|
|
|
</pre>
|
|
|
|
<br/>
|
|
|
|
<hr />
|
|
|
|
""").format(msg_cls.__name__, msg_text)
|
|
|
|
return HttpResponse(html_data + text_data)
|
|
|
|
|
|
|
|
|