TreeNodeEditor | funkcni pridavani uloh a jejich zadani.
This commit is contained in:
parent
35ff615ac0
commit
80e3e6d863
4 changed files with 190 additions and 15 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,23 +1,111 @@
|
|||
<template>
|
||||
<div class="ulohazadaninode">
|
||||
<!--pre>UlohaZadaniNode {{item.node.uloha}} {{typeof(item)}}</pre-->
|
||||
<h5>Zadání {{item.node.uloha.cislo_zadani.poradi}}.{{ item.node.uloha.kod }}: {{ item.node.uloha.nazev }}</h5>
|
||||
<template v-if="editorShow">
|
||||
Název: <input tpye="text" v-model="nazev"><br>
|
||||
Počet bodů: <input type="number" min="0" max="20" v-model="max_body"><br>
|
||||
Kód: <input type="text" v-model="kod"><br>
|
||||
<!--Autor: FIXME!<br-->
|
||||
<button v-if="create" v-on:click="createNode">Vytvořit úlohu</button>
|
||||
<button v-if="!create" v-on:click="saveNode">Uložit</button>
|
||||
</template>
|
||||
<template v-else>
|
||||
<h5>Zadání {{item.node.uloha.cislo_zadani.poradi}}.{{ item.node.uloha.kod }}: {{ item.node.uloha.nazev }} ({{item.node.uloha.max_body}} b)</h5>
|
||||
<button v-if="editorMode" v-on:click="editorShow = !editorShow">Upravit</button>
|
||||
<!--button v-if="editorMode" v-on:click="deleteText" class="delete">Smazat</button-->
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios'
|
||||
|
||||
export default {
|
||||
name: 'UlohaZadaniNode',
|
||||
data: () => ({
|
||||
max_body: 0,
|
||||
kod: "",
|
||||
editorShow: false,
|
||||
editorMode: false,
|
||||
}),
|
||||
props: {
|
||||
item: Object,
|
||||
created: Boolean
|
||||
,
|
||||
editorShow: Boolean,
|
||||
editorMode: Boolean,
|
||||
create: Boolean,
|
||||
where: String,
|
||||
refnode: Object
|
||||
},
|
||||
methods: {
|
||||
getCookie: function (name){
|
||||
var cookieValue = null;
|
||||
if (document.cookie && document.cookie != '') {
|
||||
var cookies = document.cookie.split(';');
|
||||
for (var i = 0; i < cookies.length; i++) {
|
||||
var cookie = cookies[i].trim();
|
||||
// Does this cookie string begin with the name we want?
|
||||
|
||||
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
},
|
||||
createNode: function(){
|
||||
console.log('Creating UlohaZadaniNode');
|
||||
this.loading = true
|
||||
axios.post('/api/ulohazadaninode/',{
|
||||
refnode: this.refnode.id,
|
||||
where: this.where,
|
||||
max_body: this.max_body,
|
||||
kod: this.kod,
|
||||
nazev: this.nazev,
|
||||
}).then(response => {
|
||||
console.log(response.data);
|
||||
this.loading=false;
|
||||
this.$root.$emit('updateData',"ulohaZadaniNode create update");
|
||||
}).catch( e => {
|
||||
console.log(e);
|
||||
});},
|
||||
saveNode: function(){
|
||||
console.log('Saving UlohaZadaniNode');
|
||||
this.loading = true
|
||||
axios.put('/api/ulohazadaninode/'+this.item.node.id+'/',{
|
||||
uloha: {
|
||||
max_body: this.max_body,
|
||||
kod: this.kod,
|
||||
nazev: this.nazev,
|
||||
}
|
||||
}).then(response => {
|
||||
console.log(response.data);
|
||||
this.loading=false;
|
||||
this.item.node = response.data;
|
||||
this.$root.$emit('updateData',"ulohaZadaniNode save update");
|
||||
}).catch( e => {
|
||||
console.log(e);
|
||||
});
|
||||
this.editorShow = false;
|
||||
|
||||
},
|
||||
|
||||
},
|
||||
mounted: function(){
|
||||
axios.defaults.headers.common['X-CSRFToken'] = this.getCookie('csrftoken');
|
||||
if (this.create){
|
||||
this.editorShow = true;
|
||||
this.max_body = 0;
|
||||
this.nazev = "";
|
||||
this.kod = "";
|
||||
} else {
|
||||
this.max_body = this.item.node.uloha.max_body;
|
||||
this.nazev = this.item.node.uloha.nazev;
|
||||
this.kod = this.item.node.uloha.kod;
|
||||
}
|
||||
if (this.item.node.uloha === null){
|
||||
console.log("Uloha je null!");
|
||||
console.log(this.item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
Loading…
Reference in a new issue