2020-12-04 06:51:01 +01:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2021-02-18 11:22:41 +01:00
|
|
|
read_groups = IdPrimaryKeyRelatedField(
|
|
|
|
many=True, required=False, queryset=get_group_model().objects.all()
|
|
|
|
)
|
|
|
|
write_groups = IdPrimaryKeyRelatedField(
|
2020-12-04 06:51:01 +01:00
|
|
|
many=True, required=False, queryset=get_group_model().objects.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ChatGroup
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"name",
|
2021-02-18 11:22:41 +01:00
|
|
|
"read_groups",
|
|
|
|
"write_groups",
|
2020-12-04 06:51:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ChatMessageSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for chat.models.ChatMessage objects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
chatgroup = IdPrimaryKeyRelatedField(
|
|
|
|
required=False, queryset=ChatGroup.objects.all()
|
|
|
|
)
|
2021-02-18 11:22:41 +01:00
|
|
|
read_groups_id = SerializerMethodField()
|
|
|
|
write_groups_id = SerializerMethodField()
|
2020-12-04 06:51:01 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ChatMessage
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"text",
|
|
|
|
"chatgroup",
|
|
|
|
"timestamp",
|
|
|
|
"username",
|
2021-02-09 16:06:44 +01:00
|
|
|
"user_id",
|
2021-02-18 11:22:41 +01:00
|
|
|
"read_groups_id",
|
|
|
|
"write_groups_id",
|
2020-12-04 06:51:01 +01:00
|
|
|
)
|
2021-02-09 16:06:44 +01:00
|
|
|
read_only_fields = (
|
|
|
|
"username",
|
|
|
|
"user_id",
|
|
|
|
)
|
2020-12-04 06:51:01 +01:00
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
if "text" in data:
|
|
|
|
data["text"] = validate_html_strict(data["text"])
|
|
|
|
return data
|
|
|
|
|
2021-02-18 11:22:41 +01:00
|
|
|
def get_read_groups_id(self, chatmessage):
|
|
|
|
return [group.id for group in chatmessage.chatgroup.read_groups.all()]
|
|
|
|
|
|
|
|
def get_write_groups_id(self, chatmessage):
|
|
|
|
return [group.id for group in chatmessage.chatgroup.write_groups.all()]
|