2015-02-18 01:45:39 +01:00
|
|
|
from openslides.utils.rest_api import Field, ModelSerializer, ValidationError
|
2015-01-06 00:04:36 +01:00
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
from .models import ChatMessage, CustomSlide, Projector, Tag
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class JSONSerializerField(Field):
|
|
|
|
"""
|
|
|
|
Serializer for projector's JSONField.
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
raise ValidationError('Data must be a dictionary.')
|
|
|
|
for element in data.values():
|
2015-02-18 01:45:39 +01:00
|
|
|
if type(element) is not dict:
|
2015-09-08 14:14:11 +02:00
|
|
|
raise ValidationError('Data must be a dictionary.')
|
2015-02-18 01:45:39 +01:00
|
|
|
elif element.get('name') is None:
|
|
|
|
raise ValidationError("Every dictionary must have a key 'name'.")
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectorSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.Projector objects.
|
|
|
|
"""
|
2015-09-08 14:14:11 +02:00
|
|
|
config = JSONSerializerField(write_only=True)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Projector
|
2015-09-14 23:16:31 +02:00
|
|
|
fields = ('id', 'config', 'elements', 'scale', 'scroll', )
|
2015-01-06 00:04:36 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class CustomSlideSerializer(ModelSerializer):
|
2015-01-06 00:04:36 +01:00
|
|
|
"""
|
2015-01-17 14:25:05 +01:00
|
|
|
Serializer for core.models.CustomSlide objects.
|
2015-01-06 00:04:36 +01:00
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = CustomSlide
|
2015-02-18 01:45:39 +01:00
|
|
|
fields = ('id', 'title', 'text', 'weight', )
|
2015-01-17 14:25:05 +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.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = Tag
|
2015-02-18 01:45:39 +01:00
|
|
|
fields = ('id', 'name', )
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ChatMessageSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for core.models.ChatMessage objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = ChatMessage
|
|
|
|
fields = ('id', 'message', 'timestamp', 'user', )
|
|
|
|
read_only_fields = ('user', )
|