2015-02-18 01:45:39 +01:00
|
|
|
from openslides.utils.rest_api import Field, ModelSerializer, ValidationError
|
2017-01-20 11:34:05 +01:00
|
|
|
from openslides.utils.validate import validate_html
|
2015-01-06 00:04:36 +01:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
from .models import (
|
|
|
|
ChatMessage,
|
2017-08-22 14:17:20 +02:00
|
|
|
ConfigStore,
|
2016-10-21 11:05:24 +02:00
|
|
|
Countdown,
|
2018-11-04 14:02:30 +01:00
|
|
|
History,
|
2016-10-21 11:05:24 +02:00
|
|
|
ProjectionDefault,
|
|
|
|
Projector,
|
|
|
|
ProjectorMessage,
|
|
|
|
Tag,
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class JSONSerializerField(Field):
|
|
|
|
"""
|
2017-08-22 14:17:20 +02:00
|
|
|
Serializer for projector's and config JSONField.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
"""
|
2015-09-08 14:14:11 +02:00
|
|
|
Checks that data is a dictionary. The key is a hex UUID and the
|
|
|
|
value is a dictionary with must have a key 'name'.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-09-08 14:14:11 +02:00
|
|
|
if type(data) is not dict:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
2015-09-08 14:14:11 +02:00
|
|
|
for element in data.values():
|
2015-02-18 01:45:39 +01:00
|
|
|
if type(element) is not dict:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
|
|
|
elif element.get("name") is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Every dictionary must have a key 'name'."}
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
return data
|
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
def to_representation(self, value):
|
|
|
|
"""
|
|
|
|
Returns the value. It is decoded from the Django JSONField.
|
|
|
|
"""
|
|
|
|
return value
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
class ProjectionDefaultSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.ProjectionDefault objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
class Meta:
|
|
|
|
model = ProjectionDefault
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "name", "display_name", "projector")
|
2016-09-12 11:05:34 +02:00
|
|
|
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
class ProjectorSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.Projector objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
config = JSONSerializerField()
|
2016-09-12 11:05:34 +02:00
|
|
|
projectiondefaults = ProjectionDefaultSerializer(many=True, read_only=True)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Projector
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"config",
|
|
|
|
"scale",
|
|
|
|
"scroll",
|
|
|
|
"name",
|
|
|
|
"blank",
|
|
|
|
"width",
|
|
|
|
"height",
|
|
|
|
"projectiondefaults",
|
|
|
|
)
|
|
|
|
read_only_fields = ("scale", "scroll", "blank", "width", "height")
|
2015-01-06 00:04:36 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class TagSerializer(ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for core.models.Tag objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
model = Tag
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "name")
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
class ConfigSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.Tag objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
value = JSONSerializerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ConfigStore
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "key", "value")
|
2017-08-22 14:17:20 +02:00
|
|
|
|
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
class ChatMessageSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.ChatMessage objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
class Meta:
|
|
|
|
model = ChatMessage
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "message", "timestamp", "user")
|
|
|
|
read_only_fields = ("user",)
|
2016-10-21 11:05:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectorMessageSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.ProjectorMessage objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
class Meta:
|
|
|
|
model = ProjectorMessage
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "message")
|
2016-10-21 11:05:24 +02:00
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
def validate(self, data):
|
2019-01-06 16:22:33 +01:00
|
|
|
if "message" in data:
|
|
|
|
data["message"] = validate_html(data["message"])
|
2017-01-20 11:34:05 +01:00
|
|
|
return data
|
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
|
|
|
|
class CountdownSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.Countdown objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
class Meta:
|
|
|
|
model = Countdown
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "description", "default_time", "countdown_time", "running")
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HistorySerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.Countdown objects.
|
|
|
|
|
|
|
|
Does not contain full data of history object.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
class Meta:
|
|
|
|
model = History
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "element_id", "now", "information", "user")
|