diff --git a/mamweb/routers.py b/mamweb/routers.py index 5a75d39d..dfb87d90 100644 --- a/mamweb/routers.py +++ b/mamweb/routers.py @@ -8,4 +8,6 @@ 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'ulohazadaninode', vs.UlohaZadaniNodeViewSet) diff --git a/seminar/views/views_rest.py b/seminar/views/views_rest.py index 739c57c9..4b748d26 100644 --- a/seminar/views/views_rest.py +++ b/seminar/views/views_rest.py @@ -6,6 +6,7 @@ from seminar import treelib DEFAULT_NODE_DEPTH = 2 + class TextSerializer(serializers.ModelSerializer): class Meta: model = m.Text @@ -16,18 +17,17 @@ class ProblemSerializer(serializers.ModelSerializer): model = m.Problem fields = '__all__' +class UlohaSerializer(serializers.ModelSerializer): + class Meta: + model = m.Uloha + 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 @@ -141,6 +141,71 @@ class CastNodeCreateSerializer(serializers.ModelSerializer): fields = ('nadpis','where','refnode') depth = DEFAULT_NODE_DEPTH +class UlohaZadaniNodeSerializer(serializers.ModelSerializer): + class Meta: + model = m.UlohaZadaniNode + fields = '__all__' + depth = DEFAULT_NODE_DEPTH + +class UlohaZadaniNodeWriteSerializer(serializers.ModelSerializer): + uloha = UlohaSerializer() + + def update(self,node,validated_data): + node.uloha.max_body = validated_data.get('uloha').get('max_body') + node.uloha.kod = validated_data.get('uloha').get('kod') + node.uloha.nazev = validated_data.get('uloha').get('nazev') + node.uloha.save() + return node + + class Meta: + model = m.TextNode + fields = ('id','uloha') + depth = DEFAULT_NODE_DEPTH + +class UlohaZadaniNodeCreateSerializer(serializers.ModelSerializer): + uloha = UlohaSerializer() + refnode = serializers.IntegerField() + where = serializers.CharField() + + def create(self,validated_data): + # text_zadani = validated_data.pop('text_zadani') + temp_uloha = validated_data.pop('uloha') + where = validated_data.pop('where') + refnode_id = validated_data.pop('refnode') + refnode = m.TreeNode.objects.get(pk=refnode_id) + + # Z cesty ke koreni stromu zjistime, v jakem jsme tematu a v jakem cisle + cislo = None + tema = None + travelnode = refnode + while travelnode is not None: + if isinstance(travelnode, m.TemaVCisleNode): + tema = travelnode.tema + if isinstance(travelnode, m.CisloNode): + cislo = travelnode.cislo + travelnode = treelib.get_parent(travelnode) + # Vyrobime ulohu + uloha = m.Uloha.objects.create(cislo_zadani=cislo, nadproblem = tema, **temp_uloha) + + # A vyrobime UlohaZadaniNode + if where == 'syn': + node = treelib.create_child(refnode,m.UlohaZadaniNode,uloha = uloha) + elif where == 'za': + node = treelib.create_node_after(refnode,m.UlohaZadaniNode,uloha = uloha) + elif where == 'pred': + node = treelib.create_node_before(refnode,m.UlohaZadaniNode,uloha = uloha) + node.where = None + node.refnode = None + node.max_body = None + node.kod = None + return node + + class Meta: + model = m.UlohaZadaniNode + fields = ('uloha','where','refnode') + depth = DEFAULT_NODE_DEPTH + + class ReseniNodeSerializer(serializers.ModelSerializer): class Meta: model = m.ReseniNode diff --git a/seminar/viewsets.py b/seminar/viewsets.py index 9d600375..7cbe57a3 100644 --- a/seminar/viewsets.py +++ b/seminar/viewsets.py @@ -50,10 +50,6 @@ class ReadWriteSerializerMixin(object): ) return self.create_serializer_class -class UlohaVzorakNodeViewSet(viewsets.ModelViewSet): - queryset = m.UlohaVzorakNode.objects.all() - serializer_class = views.UlohaVzorakNodeSerializer - class TextViewSet(viewsets.ModelViewSet): queryset = m.Text.objects.all() serializer_class = views.TextSerializer @@ -86,8 +82,31 @@ class UlohaVzorakNodeViewSet(viewsets.ModelViewSet): 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 + + def get_queryset(self): + queryset = m.Uloha.objects.all() + 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 UlohaZadaniNodeViewSet(ReadWriteSerializerMixin, viewsets.ModelViewSet): + queryset = m.UlohaZadaniNode.objects.all() + read_serializer_class = views.UlohaZadaniNodeSerializer + write_serializer_class = views.UlohaZadaniNodeWriteSerializer + create_serializer_class = views.UlohaZadaniNodeCreateSerializer + + class ProblemViewSet(viewsets.ModelViewSet): serializer_class = views.ProblemSerializer @@ -105,3 +124,4 @@ class ProblemViewSet(viewsets.ModelViewSet): return queryset + diff --git a/vue_frontend/src/components/UlohaZadaniNode.vue b/vue_frontend/src/components/UlohaZadaniNode.vue index 7c5f6346..b41fc872 100644 --- a/vue_frontend/src/components/UlohaZadaniNode.vue +++ b/vue_frontend/src/components/UlohaZadaniNode.vue @@ -1,23 +1,111 @@