2021-05-31 09:20:38 +00:00
|
|
|
from rest_framework.decorators import action
|
2023-02-01 08:43:43 +00:00
|
|
|
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
|
|
|
from rest_framework.response import Response
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.api import JMSGenericViewSet
|
2023-02-01 08:43:43 +00:00
|
|
|
from common.const.http import GET, PATCH, POST
|
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from common.utils.http import is_true
|
2021-05-31 09:20:38 +00:00
|
|
|
from ..serializers import (
|
2023-02-01 08:43:43 +00:00
|
|
|
SiteMessageSerializer, SiteMessageIdsSerializer,
|
2021-05-31 09:20:38 +00:00
|
|
|
SiteMessageSendSerializer,
|
|
|
|
)
|
2021-06-11 09:11:29 +00:00
|
|
|
from ..site_msg import SiteMessageUtil
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
__all__ = ('SiteMessageViewSet',)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
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 = {
|
2023-02-01 08:43:43 +00:00
|
|
|
'default': SiteMessageSerializer,
|
2021-05-31 09:20:38 +00:00
|
|
|
'mark_as_read': SiteMessageIdsSerializer,
|
|
|
|
'send': SiteMessageSendSerializer,
|
|
|
|
}
|
2023-02-01 08:43:43 +00:00
|
|
|
filterset_fields = ('has_read',)
|
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
|
2023-02-01 08:43:43 +00:00
|
|
|
s = self.get_serializer(data=request.data)
|
|
|
|
s.is_valid(raise_exception=True)
|
|
|
|
ids = s.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):
|
2023-02-01 08:43:43 +00:00
|
|
|
s = self.get_serializer(data=request.data)
|
|
|
|
s.is_valid(raise_exception=True)
|
|
|
|
SiteMessageUtil.send_msg(**s.validated_data, sender=request.user)
|
2021-05-31 09:20:38 +00:00
|
|
|
return Response({'detail': 'ok'})
|