2021-05-31 09:20:38 +00:00
|
|
|
from rest_framework import serializers
|
2023-02-01 08:43:43 +00:00
|
|
|
from rest_framework.serializers import ModelSerializer
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2024-08-27 08:46:41 +00:00
|
|
|
from common.utils import convert_html_to_markdown
|
2023-02-01 08:43:43 +00:00
|
|
|
from ..models import MessageContent
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
2021-06-08 03:11:27 +00:00
|
|
|
class SenderMixin(ModelSerializer):
|
|
|
|
sender = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
def get_sender(self, site_msg):
|
|
|
|
sender = site_msg.sender
|
|
|
|
if sender:
|
|
|
|
return str(sender)
|
|
|
|
else:
|
|
|
|
return ''
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
2023-02-01 08:43:43 +00:00
|
|
|
class MessageContentSerializer(SenderMixin, ModelSerializer):
|
2024-08-27 08:46:41 +00:00
|
|
|
message = serializers.SerializerMethodField()
|
|
|
|
|
2023-02-01 08:43:43 +00:00
|
|
|
class Meta:
|
|
|
|
model = MessageContent
|
|
|
|
fields = [
|
|
|
|
'id', 'subject', 'message',
|
|
|
|
'date_created', 'date_updated',
|
|
|
|
'sender',
|
|
|
|
]
|
|
|
|
|
2024-08-27 08:46:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_message(site_msg):
|
|
|
|
markdown = convert_html_to_markdown(site_msg.message)
|
|
|
|
return markdown
|
|
|
|
|
2023-02-01 08:43:43 +00:00
|
|
|
|
|
|
|
class SiteMessageSerializer(SenderMixin, ModelSerializer):
|
|
|
|
content = MessageContentSerializer(read_only=True)
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
class Meta:
|
2023-02-01 08:43:43 +00:00
|
|
|
model = MessageContent
|
2021-06-08 03:11:27 +00:00
|
|
|
fields = [
|
2023-02-01 08:43:43 +00:00
|
|
|
'id', 'has_read', 'read_at', 'content', 'date_created'
|
2021-06-08 03:11:27 +00:00
|
|
|
]
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SiteMessageIdsSerializer(serializers.Serializer):
|
|
|
|
ids = serializers.ListField(child=serializers.UUIDField())
|
|
|
|
|
|
|
|
|
|
|
|
class SiteMessageSendSerializer(serializers.Serializer):
|
|
|
|
subject = serializers.CharField()
|
|
|
|
message = serializers.CharField()
|
|
|
|
user_ids = serializers.ListField(child=serializers.UUIDField(), required=False)
|
|
|
|
group_ids = serializers.ListField(child=serializers.UUIDField(), required=False)
|
|
|
|
is_broadcast = serializers.BooleanField(default=False)
|