OpenSlides/server/openslides/chat/serializers.py
Sean 69adc1d41c Add Chat UI Components
Add Chat User Interface
Restructure some services
Virtual Scrolling
Manual change detection for message updates
Enhanced Date pipe
Message layout
Tabbed reusable chat window
Deleting messages
Further permission checks
Delete-prompts
Mobile friendly chat usage
automatically scroll to bottom
2021-02-11 17:10:25 +01:00

63 lines
1.5 KiB
Python

from openslides.utils.rest_api import (
IdPrimaryKeyRelatedField,
ModelSerializer,
SerializerMethodField,
)
from openslides.utils.validate import validate_html_strict
from ..utils.auth import get_group_model
from .models import ChatGroup, ChatMessage
class ChatGroupSerializer(ModelSerializer):
"""
Serializer for chat.models.ChatGroup objects.
"""
access_groups = IdPrimaryKeyRelatedField(
many=True, required=False, queryset=get_group_model().objects.all()
)
class Meta:
model = ChatGroup
fields = (
"id",
"name",
"access_groups",
)
class ChatMessageSerializer(ModelSerializer):
"""
Serializer for chat.models.ChatMessage objects.
"""
chatgroup = IdPrimaryKeyRelatedField(
required=False, queryset=ChatGroup.objects.all()
)
access_groups_id = SerializerMethodField()
class Meta:
model = ChatMessage
fields = (
"id",
"text",
"chatgroup",
"timestamp",
"username",
"user_id",
"access_groups_id",
)
read_only_fields = (
"username",
"user_id",
)
def validate(self, data):
if "text" in data:
data["text"] = validate_html_strict(data["text"])
return data
def get_access_groups_id(self, chatmessage):
return [group.id for group in chatmessage.chatgroup.access_groups.all()]