2019-01-18 19:11:22 +01:00
|
|
|
from typing import Any
|
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,
|
|
|
|
)
|
2019-03-04 18:40:08 +01:00
|
|
|
from ..utils.projector import projector_slides
|
|
|
|
from ..utils.rest_api import Field, IntegerField, ModelSerializer, ValidationError
|
|
|
|
from ..utils.validate import validate_html
|
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):
|
|
|
|
"""
|
2019-01-18 19:11:22 +01:00
|
|
|
Returns the value. It is encoded from the Django JSONField.
|
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
|
|
|
|
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
def elements_validator(value: Any) -> None:
|
|
|
|
"""
|
|
|
|
Checks the format of the elements field.
|
|
|
|
"""
|
|
|
|
if not isinstance(value, list):
|
|
|
|
raise ValidationError({"detail": "Data must be a list."})
|
|
|
|
for element in value:
|
|
|
|
if not isinstance(element, dict):
|
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
|
|
|
if element.get("name") is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Every dictionary must have a key 'name'."}
|
|
|
|
)
|
2019-01-27 13:17:17 +01:00
|
|
|
if element["name"] not in projector_slides:
|
2019-01-18 19:11:22 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": f"Unknown projector element {element['name']},"}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def elements_array_validator(value: Any) -> None:
|
|
|
|
"""
|
|
|
|
Validates the value of the element field of the projector model.
|
|
|
|
"""
|
|
|
|
if not isinstance(value, list):
|
|
|
|
raise ValidationError({"detail": "Data must be a list."})
|
|
|
|
for element in value:
|
|
|
|
elements_validator(element)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
elements = JSONSerializerField(validators=[elements_validator])
|
2019-01-24 16:25:50 +01:00
|
|
|
elements_preview = JSONSerializerField(validators=[elements_validator])
|
2019-01-18 19:11:22 +01:00
|
|
|
elements_history = JSONSerializerField(validators=[elements_array_validator])
|
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
projectiondefaults = ProjectionDefaultSerializer(many=True, read_only=True)
|
2019-01-18 19:11:22 +01:00
|
|
|
width = IntegerField(min_value=800, max_value=3840, required=False)
|
|
|
|
height = IntegerField(min_value=340, max_value=2880, required=False)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Projector
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = (
|
|
|
|
"id",
|
2019-01-18 19:11:22 +01:00
|
|
|
"elements",
|
|
|
|
"elements_preview",
|
|
|
|
"elements_history",
|
2019-01-06 16:22:33 +01:00
|
|
|
"scale",
|
|
|
|
"scroll",
|
|
|
|
"name",
|
|
|
|
"width",
|
|
|
|
"height",
|
2019-01-31 12:31:53 +01:00
|
|
|
"reference_projector",
|
2019-01-06 16:22:33 +01:00
|
|
|
"projectiondefaults",
|
2019-02-21 17:04:59 +01:00
|
|
|
"background_color",
|
|
|
|
"header_background_color",
|
|
|
|
"header_font_color",
|
|
|
|
"header_h1_color",
|
|
|
|
"show_header_footer",
|
|
|
|
"show_title",
|
|
|
|
"show_logo",
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2019-01-18 19:11:22 +01:00
|
|
|
read_only_fields = ("scale", "scroll")
|
|
|
|
|
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-02-27 16:08:22 +01:00
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"default_time",
|
|
|
|
"countdown_time",
|
|
|
|
"running",
|
|
|
|
)
|
|
|
|
unique_together = ("title",)
|
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
|
|
|
|
2019-01-19 18:50:05 +01:00
|
|
|
information = JSONSerializerField()
|
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
class Meta:
|
|
|
|
model = History
|
2019-01-19 15:49:46 +01:00
|
|
|
fields = ("id", "element_id", "now", "information", "restricted", "user")
|
2019-01-12 20:49:28 +01:00
|
|
|
read_only_fields = ("now",)
|