|
|
|
from rest_framework import serializers
|
|
|
|
from rest_polymorphic.serializers import PolymorphicSerializer
|
|
|
|
|
|
|
|
import seminar.models as m
|
|
|
|
from seminar import treelib
|
|
|
|
|
|
|
|
DEFAULT_NODE_DEPTH = 2
|
|
|
|
|
|
|
|
class TextSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.Text
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UlohaVzorakNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.UlohaVzorakNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class UlohaZadaniNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.UlohaZadaniNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class RocnikNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.RocnikNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class CisloNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.CisloNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class MezicisloNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.MezicisloNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class TemaVCisleNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.TemaVCisleNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class OrgTextNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.OrgTextNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class PohadkaNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.PohadkaNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class TextNodeSerializer(serializers.ModelSerializer):
|
|
|
|
text = TextSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = m.TextNode
|
|
|
|
fields = ('id','text','polymorphic_ctype')
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class TextNodeWriteSerializer(serializers.ModelSerializer):
|
|
|
|
text = TextSerializer()
|
|
|
|
|
|
|
|
def update(self,node,validated_data):
|
|
|
|
node.text.na_web = validated_data.get('text').get('na_web')
|
|
|
|
node.text.save()
|
|
|
|
return node
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = m.TextNode
|
|
|
|
fields = ('id','text')
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class TextNodeCreateSerializer(serializers.ModelSerializer):
|
|
|
|
text = TextSerializer()
|
|
|
|
refnode = serializers.CharField()
|
|
|
|
where = serializers.CharField()
|
|
|
|
|
|
|
|
def create(self,validated_data):
|
|
|
|
temp_text = validated_data.pop('text')
|
|
|
|
where = validated_data.pop('where')
|
|
|
|
refnode_id = validated_data.pop('refnode')
|
|
|
|
refnode = m.TreeNode.objects.get(pk=refnode_id)
|
|
|
|
text = m.Text.objects.create(**temp_text)
|
|
|
|
if where == 'syn':
|
|
|
|
node = treelib.create_child(refnode,m.TextNode,text=text)
|
|
|
|
elif where == 'za':
|
|
|
|
node = treelib.create_node_after(refnode,m.TextNode,text=text)
|
|
|
|
elif where == 'pred':
|
|
|
|
node = treelib.create_node_before(refnode,m.TextNode,text=text)
|
|
|
|
node.where = None
|
|
|
|
node.refnode = None
|
|
|
|
return node
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = m.TextNode
|
|
|
|
fields = ('text','where','refnode')
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class CastNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.CastNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class CastNodeCreateSerializer(serializers.ModelSerializer):
|
|
|
|
refnode = serializers.CharField()
|
|
|
|
where = serializers.CharField()
|
|
|
|
|
|
|
|
def create(self,validated_data):
|
|
|
|
temp_nadpis = validated_data.pop('nadpis')
|
|
|
|
where = validated_data.pop('where')
|
|
|
|
refnode_id = validated_data.pop('refnode')
|
|
|
|
refnode = m.TreeNode.objects.get(pk=refnode_id)
|
|
|
|
if where == 'syn':
|
|
|
|
node = treelib.create_child(refnode,m.CastNode,nadpis=temp_nadpis)
|
|
|
|
elif where == 'za':
|
|
|
|
node = treelib.create_node_after(refnode,m.CastNode,nadpis=temp_nadpis)
|
|
|
|
elif where == 'pred':
|
|
|
|
node = treelib.create_node_before(refnode,m.CastNode,nadpis=temp_nadpis)
|
|
|
|
node.where = None
|
|
|
|
node.refnode = None
|
|
|
|
return node
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = m.CastNode
|
|
|
|
fields = ('nadpis','where','refnode')
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
class ReseniNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = m.ReseniNode
|
|
|
|
fields = '__all__'
|
|
|
|
depth = DEFAULT_NODE_DEPTH
|
|
|
|
|
|
|
|
|
|
|
|
class TreeNodeSerializer(PolymorphicSerializer):
|
|
|
|
model_serializer_mapping = {
|
|
|
|
m.RocnikNode: RocnikNodeSerializer,
|
|
|
|
m.CisloNode: CisloNodeSerializer,
|
|
|
|
m.MezicisloNode: MezicisloNodeSerializer,
|
|
|
|
m.TemaVCisleNode: TemaVCisleNodeSerializer,
|
|
|
|
m.OrgTextNode: OrgTextNodeSerializer,
|
|
|
|
m.UlohaZadaniNode: UlohaZadaniNodeSerializer,
|
|
|
|
m.UlohaVzorakNode: UlohaVzorakNodeSerializer,
|
|
|
|
m.PohadkaNode: PohadkaNodeSerializer,
|
|
|
|
m.TextNode: TextNodeSerializer,
|
|
|
|
m.CastNode: CastNodeSerializer,
|
|
|
|
m.ReseniNode: ReseniNodeSerializer,
|
|
|
|
}
|
|
|
|
|