From e54f61e04d0c4449bfd398572006d4c6e3445024 Mon Sep 17 00:00:00 2001 From: Tomas 'Jethro' Pokorny Date: Tue, 22 Dec 2020 22:29:15 +0100 Subject: [PATCH] TreeNode editor | API pro reseni --- mamweb/routers.py | 2 ++ seminar/models.py | 2 +- seminar/views/views_rest.py | 42 +++++++++++++++++++++++++++++++++++++ seminar/viewsets.py | 21 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/mamweb/routers.py b/mamweb/routers.py index dfb87d90..003dd5a6 100644 --- a/mamweb/routers.py +++ b/mamweb/routers.py @@ -4,10 +4,12 @@ from seminar import viewsets as vs router = routers.DefaultRouter() router.register(r'ulohavzoraknode', vs.UlohaVzorakNodeViewSet,basename='ulohavzoraknode') +router.register(r'reseninode', vs.ReseniNodeViewSet,basename='reseninode') router.register(r'text', vs.TextViewSet) router.register(r'textnode', vs.TextNodeViewSet) router.register(r'castnode', vs.CastNodeViewSet) router.register(r'problem', vs.ProblemViewSet, basename='problem') router.register(r'uloha', vs.UlohaViewSet, basename='uloha') +router.register(r'reseni', vs.ReseniViewSet, basename='reseni') router.register(r'ulohazadaninode', vs.UlohaZadaniNodeViewSet) diff --git a/seminar/models.py b/seminar/models.py index 1bbf5514..d028a71b 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -1523,7 +1523,7 @@ class ReseniNode(TreeNode): verbose_name = 'reseni') def aktualizuj_nazev(self): - self.nazev = "OtisteneReseniNode: "+str(self.reseni) + self.nazev = "ReseniNode: "+str(self.reseni) def getOdkazStr(self): return str(self.reseni) diff --git a/seminar/views/views_rest.py b/seminar/views/views_rest.py index 9820c752..ef49b3cc 100644 --- a/seminar/views/views_rest.py +++ b/seminar/views/views_rest.py @@ -22,6 +22,10 @@ class UlohaSerializer(serializers.ModelSerializer): model = m.Uloha fields = '__all__' +class ReseniSerializer(serializers.ModelSerializer): + class Meta: + model = m.Reseni + fields = '__all__' class RocnikNodeSerializer(serializers.ModelSerializer): class Meta: @@ -253,6 +257,44 @@ class ReseniNodeSerializer(serializers.ModelSerializer): fields = '__all__' depth = DEFAULT_NODE_DEPTH +class ReseniNodeWriteSerializer(serializers.ModelSerializer): + reseni = serializers.PrimaryKeyRelatedField(queryset=m.Reseni.objects.all(), many=False, read_only=False) + + class Meta: + model = m.ReseniNode + fields = ('id','reseni') + depth = DEFAULT_NODE_DEPTH + +class ReseniNodeCreateSerializer(serializers.ModelSerializer): + reseni_id = serializers.IntegerField() + refnode = serializers.IntegerField() + where = serializers.CharField() + + def create(self,validated_data): + # text_zadani = validated_data.pop('text_zadani') + reseni_id = validated_data.pop('reseni_id') + reseni = m.Reseni.objects.get(pk=reseni_id) + where = validated_data.pop('where') + refnode_id = validated_data.pop('refnode') + refnode = m.TreeNode.objects.get(pk=refnode_id) + + # A vyrobime UlohaZadaniNode + if where == 'syn': + node = treelib.create_child(refnode,m.ReseniNode,reseni = reseni) + elif where == 'za': + node = treelib.create_node_after(refnode,m.ReseniNode,reseni = reseni) + elif where == 'pred': + node = treelib.create_node_before(refnode,m.ReseniNode,reseni = reseni) + node.where = None + node.refnode = None + node.reseni_id = None + return node + + class Meta: + model = m.ReseniNode + fields = ('reseni_id','where','refnode') + depth = DEFAULT_NODE_DEPTH + class TreeNodeSerializer(PolymorphicSerializer): model_serializer_mapping = { diff --git a/seminar/viewsets.py b/seminar/viewsets.py index 4291a160..7e2ea63a 100644 --- a/seminar/viewsets.py +++ b/seminar/viewsets.py @@ -110,6 +110,20 @@ class UlohaVzorakNodeViewSet(PermissionMixin, ReadWriteSerializerMixin, viewsets queryset = queryset.filter(nadproblem__pk = nadproblem) return queryset +class ReseniViewSet(viewsets.ModelViewSet): + serializer_class = views.ReseniSerializer + + def get_queryset(self): + queryset = m.Reseni.objects.all() + #FIXME upravit nazvy dle skutecnych polozek reseni + nazev = self.request.query_params.get('nazev',None) + if nazev is not None: + queryset = queryset.filter(nazev__contains=nazev) + nadproblem = self.request.query_params.get('nadproblem',None) + if nadproblem is not None: + queryset = queryset.filter(nadproblem__pk = nadproblem) + return queryset + class UlohaViewSet(viewsets.ModelViewSet): serializer_class = views.UlohaSerializer @@ -129,6 +143,13 @@ class UlohaZadaniNodeViewSet(ReadWriteSerializerMixin, viewsets.ModelViewSet): write_serializer_class = views.UlohaZadaniNodeWriteSerializer create_serializer_class = views.UlohaZadaniNodeCreateSerializer +class ReseniNodeViewSet(ReadWriteSerializerMixin, viewsets.ModelViewSet): + queryset = m.ReseniNode.objects.all() + read_serializer_class = views.ReseniNodeSerializer + write_serializer_class = views.ReseniNodeWriteSerializer + create_serializer_class = views.ReseniNodeCreateSerializer + + class ProblemViewSet(viewsets.ModelViewSet): serializer_class = views.ProblemSerializer