OpenSlides/server/openslides/topics/serializers.py

63 lines
2.4 KiB
Python
Raw Normal View History

2019-02-06 16:14:34 +01:00
from openslides.utils.autoupdate import inform_changed_data
from openslides.utils.rest_api import CharField, IntegerField, ModelSerializer
2020-04-15 11:59:16 +02:00
from openslides.utils.validate import validate_html_permissive
from .models import Topic
class TopicSerializer(ModelSerializer):
"""
Serializer for core.models.Topic objects.
"""
2019-01-06 16:22:33 +01:00
agenda_type = IntegerField(
write_only=True, required=False, min_value=1, max_value=3, allow_null=True
2019-01-06 16:22:33 +01:00
)
agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1)
agenda_comment = CharField(write_only=True, required=False, allow_blank=True)
agenda_duration = IntegerField(write_only=True, required=False, min_value=1)
agenda_weight = IntegerField(write_only=True, required=False, min_value=1)
class Meta:
model = Topic
fields = (
2019-01-06 16:22:33 +01:00
"id",
"title",
"text",
"attachments",
"agenda_item_id",
"list_of_speakers_id",
2019-01-06 16:22:33 +01:00
"agenda_type",
"agenda_parent_id",
"agenda_comment",
"agenda_duration",
"agenda_weight",
)
2017-01-20 11:34:05 +01:00
def validate(self, data):
2019-01-06 16:22:33 +01:00
if "text" in data:
2020-04-15 11:59:16 +02:00
data["text"] = validate_html_permissive(data["text"])
2017-01-20 11:34:05 +01:00
return data
def create(self, validated_data):
"""
Customized create method. Set information about related agenda item
into agenda_item_update_information container.
"""
2019-01-06 16:22:33 +01:00
agenda_type = validated_data.pop("agenda_type", None)
agenda_parent_id = validated_data.pop("agenda_parent_id", None)
agenda_comment = validated_data.pop("agenda_comment", None)
agenda_duration = validated_data.pop("agenda_duration", None)
agenda_weight = validated_data.pop("agenda_weight", None)
attachments = validated_data.pop("attachments", [])
topic = Topic(**validated_data)
2019-01-06 16:22:33 +01:00
topic.agenda_item_update_information["type"] = agenda_type
topic.agenda_item_update_information["parent_id"] = agenda_parent_id
topic.agenda_item_update_information["comment"] = agenda_comment
topic.agenda_item_update_information["duration"] = agenda_duration
topic.agenda_item_update_information["weight"] = agenda_weight
2019-02-06 16:14:34 +01:00
topic.save(skip_autoupdate=True)
topic.attachments.add(*attachments)
2019-02-06 16:14:34 +01:00
inform_changed_data(topic)
return topic