From f6ce35beb46dc22646be46bcf62f60230d000a14 Mon Sep 17 00:00:00 2001 From: Tomas 'Jethro' Pokorny Date: Wed, 2 Dec 2020 01:42:15 +0100 Subject: [PATCH] TreeNodeEditor | funguje pridavani a editace vzoraku. --- seminar/views/views_rest.py | 51 +++++++++- seminar/viewsets.py | 6 +- vue_frontend/src/components/AddNewNode.vue | 3 +- vue_frontend/src/components/TreeNode.vue | 40 +++++--- .../src/components/UlohaVzorakNode.vue | 99 +++++++++++++++---- 5 files changed, 160 insertions(+), 39 deletions(-) diff --git a/seminar/views/views_rest.py b/seminar/views/views_rest.py index 4b748d26..9820c752 100644 --- a/seminar/views/views_rest.py +++ b/seminar/views/views_rest.py @@ -22,11 +22,6 @@ class UlohaSerializer(serializers.ModelSerializer): model = m.Uloha fields = '__all__' -class UlohaVzorakNodeSerializer(serializers.ModelSerializer): - class Meta: - model = m.UlohaVzorakNode - fields = '__all__' - depth = DEFAULT_NODE_DEPTH class RocnikNodeSerializer(serializers.ModelSerializer): class Meta: @@ -205,6 +200,52 @@ class UlohaZadaniNodeCreateSerializer(serializers.ModelSerializer): fields = ('uloha','where','refnode') depth = DEFAULT_NODE_DEPTH +class UlohaVzorakNodeSerializer(serializers.ModelSerializer): + class Meta: + model = m.UlohaVzorakNode + fields = '__all__' + depth = DEFAULT_NODE_DEPTH + +class UlohaVzorakNodeWriteSerializer(serializers.ModelSerializer): + uloha = serializers.PrimaryKeyRelatedField(queryset=m.Uloha.objects.all(), many=False, read_only=False) + + class Meta: + model = m.UlohaVzorakNode + fields = ('id','uloha') + depth = DEFAULT_NODE_DEPTH + + +class UlohaVzorakNodeCreateSerializer(serializers.ModelSerializer): + uloha_id = serializers.IntegerField() + refnode = serializers.IntegerField() + where = serializers.CharField() + + def create(self, validated_data): + uloha_id = validated_data.pop('uloha_id') + uloha = m.Uloha.objects.get(pk=uloha_id) + 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.UlohaVzorakNode,uloha = uloha) + elif where == 'za': + node = treelib.create_node_after(refnode,m.UlohaVzorakNode,uloha = uloha) + elif where == 'pred': + node = treelib.create_node_before(refnode,m.UlohaVzorakNode,uloha = uloha) + node.refnode = None + node.where = None + node.uloha_id = None + + return node + + class Meta: + model = m.UlohaVzorakNode + fields = ('refnode', 'uloha_id', 'where') + depth = DEFAULT_NODE_DEPTH + + + class ReseniNodeSerializer(serializers.ModelSerializer): class Meta: diff --git a/seminar/viewsets.py b/seminar/viewsets.py index 0f2eb472..a8779118 100644 --- a/seminar/viewsets.py +++ b/seminar/viewsets.py @@ -114,8 +114,10 @@ class CastNodeViewSet(PermissionMixin, ReadWriteSerializerMixin,viewsets.ModelVi raise PermissionDenied('Nelze smazat CastNode, který má děti!') -class UlohaVzorakNodeViewSet(PermissionMixin, viewsets.ModelViewSet): - serializer_class = views.UlohaVzorakNodeSerializer +class UlohaVzorakNodeViewSet(PermissionMixin, ReadWriteSerializerMixin, viewsets.ModelViewSet): + read_serializer_class = views.UlohaVzorakNodeSerializer + write_serializer_class = views.UlohaVzorakNodeWriteSerializer + create_serializer_class = views.UlohaVzorakNodeCreateSerializer def get_queryset(self): queryset = m.UlohaVzorakNode.objects.all() diff --git a/vue_frontend/src/components/AddNewNode.vue b/vue_frontend/src/components/AddNewNode.vue index 0f4958ed..e2b4885a 100644 --- a/vue_frontend/src/components/AddNewNode.vue +++ b/vue_frontend/src/components/AddNewNode.vue @@ -6,7 +6,7 @@
- +
@@ -24,6 +24,7 @@ export default { types: Array, where: String, refnode: Object, + tema: Object, }, data: () => ({ selected: null, diff --git a/vue_frontend/src/components/TreeNode.vue b/vue_frontend/src/components/TreeNode.vue index 0d82b1a6..4c8e6b2a 100644 --- a/vue_frontend/src/components/TreeNode.vue +++ b/vue_frontend/src/components/TreeNode.vue @@ -1,16 +1,19 @@