2021-05-31 09:20:38 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
2021-06-09 12:51:31 +00:00
|
|
|
from common.http import is_true
|
2021-05-31 09:20:38 +00:00
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from common.const.http import GET, PATCH, POST
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.drf.api import JMSGenericViewSet
|
2021-05-31 09:20:38 +00:00
|
|
|
from ..serializers import (
|
2021-06-08 03:11:27 +00:00
|
|
|
SiteMessageDetailSerializer, SiteMessageIdsSerializer,
|
2021-05-31 09:20:38 +00:00
|
|
|
SiteMessageSendSerializer,
|
|
|
|
)
|
2021-06-11 09:11:29 +00:00
|
|
|
from ..site_msg import SiteMessageUtil
|
2021-06-08 03:11:27 +00:00
|
|
|
from ..filters import SiteMsgFilter
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
__all__ = ('SiteMessageViewSet', )
|
|
|
|
|
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
class SiteMessageViewSet(ListModelMixin, RetrieveModelMixin, JMSGenericViewSet):
|
2021-05-31 09:20:38 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
serializer_classes = {
|
2021-06-08 03:11:27 +00:00
|
|
|
'default': SiteMessageDetailSerializer,
|
2021-05-31 09:20:38 +00:00
|
|
|
'mark_as_read': SiteMessageIdsSerializer,
|
|
|
|
'send': SiteMessageSendSerializer,
|
|
|
|
}
|
2021-06-08 03:11:27 +00:00
|
|
|
filterset_class = SiteMsgFilter
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
2021-06-09 12:51:31 +00:00
|
|
|
has_read = self.request.query_params.get('has_read')
|
|
|
|
|
|
|
|
if has_read is None:
|
2021-06-11 09:11:29 +00:00
|
|
|
msgs = SiteMessageUtil.get_user_all_msgs(user.id)
|
2021-06-09 12:51:31 +00:00
|
|
|
else:
|
2021-06-11 09:11:29 +00:00
|
|
|
msgs = SiteMessageUtil.filter_user_msgs(user.id, has_read=is_true(has_read))
|
2021-05-31 09:20:38 +00:00
|
|
|
return msgs
|
|
|
|
|
|
|
|
@action(methods=[GET], detail=False, url_path='unread-total')
|
|
|
|
def unread_total(self, request, **kwargs):
|
|
|
|
user = request.user
|
2021-06-11 09:11:29 +00:00
|
|
|
msgs = SiteMessageUtil.filter_user_msgs(user.id, has_read=False)
|
2021-05-31 09:20:38 +00:00
|
|
|
return Response(data={'total': msgs.count()})
|
|
|
|
|
2021-06-08 03:11:27 +00:00
|
|
|
@action(methods=[PATCH], detail=False, url_path='mark-as-read')
|
2021-05-31 09:20:38 +00:00
|
|
|
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']
|
2021-06-11 09:11:29 +00:00
|
|
|
SiteMessageUtil.mark_msgs_as_read(user.id, ids)
|
2021-05-31 09:20:38 +00:00
|
|
|
return Response({'detail': 'ok'})
|
|
|
|
|
2022-04-14 04:18:11 +00:00
|
|
|
@action(methods=[PATCH], detail=False, url_path='mark-as-read-all')
|
|
|
|
def mark_as_read_all(self, request, **kwargs):
|
|
|
|
user = request.user
|
|
|
|
SiteMessageUtil.mark_msgs_as_read(user.id)
|
|
|
|
return Response({'detail': 'ok'})
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
@action(methods=[POST], detail=False)
|
|
|
|
def send(self, request, **kwargs):
|
|
|
|
seri = self.get_serializer(data=request.data)
|
|
|
|
seri.is_valid(raise_exception=True)
|
2021-06-11 09:11:29 +00:00
|
|
|
SiteMessageUtil.send_msg(**seri.validated_data, sender=request.user)
|
2021-05-31 09:20:38 +00:00
|
|
|
return Response({'detail': 'ok'})
|