Browse Source

TreeNode editor | working adding and editing TextNodes.

export_seznamu_prednasek
parent
commit
c9e8e737da
  1. 2
      mamweb/routers.py
  2. 47
      seminar/views/views_rest.py
  3. 53
      seminar/viewsets.py
  4. 5
      vue_frontend/src/components/AddNewNode.vue
  5. 34
      vue_frontend/src/components/TextNode.vue
  6. 6
      vue_frontend/src/components/TreeNode.vue

2
mamweb/routers.py

@ -4,4 +4,6 @@ from seminar import viewsets as vs
router = routers.DefaultRouter()
router.register(r'ulohavzoraknode', vs.UlohaVzorakNodeViewSet)
router.register(r'text', vs.TextViewSet)
router.register(r'textnode', vs.TextNodeViewSet)

47
seminar/views/views_rest.py

@ -2,9 +2,17 @@ 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
@ -54,9 +62,44 @@ class PohadkaNodeSerializer(serializers.ModelSerializer):
depth = DEFAULT_NODE_DEPTH
class TextNodeSerializer(serializers.ModelSerializer):
text = TextSerializer()
class Meta:
model = m.TextNode
fields = '__all__'
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')
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)
node = treelib.create_child(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):
@ -85,5 +128,5 @@ class TreeNodeSerializer(PolymorphicSerializer):
m.TextNode: TextNodeSerializer,
m.CastNode: CastNodeSerializer,
m.ReseniNode: ReseniNodeSerializer,
}

53
seminar/viewsets.py

@ -2,6 +2,59 @@ from rest_framework import viewsets,filters
from . import models as m
from . import views
class ReadWriteSerializerMixin(object):
"""
Overrides get_serializer_class to choose the read serializer
for GET requests and the write serializer for POST requests.
Set read_serializer_class and write_serializer_class attributes on a
viewset.
"""
read_serializer_class = None
create_serializer_class = None
write_serializer_class = None
def get_serializer_class(self):
if self.action == "create":
return self.get_create_serializer_class()
if self.action in ["update", "partial_update", "destroy"]:
return self.get_write_serializer_class()
return self.get_read_serializer_class()
def get_read_serializer_class(self):
assert self.read_serializer_class is not None, (
"'%s' should either include a `read_serializer_class` attribute,"
"or override the `get_read_serializer_class()` method."
% self.__class__.__name__
)
return self.read_serializer_class
def get_write_serializer_class(self):
assert self.write_serializer_class is not None, (
"'%s' should either include a `write_serializer_class` attribute,"
"or override the `get_write_serializer_class()` method."
% self.__class__.__name__
)
return self.write_serializer_class
def get_create_serializer_class(self):
assert self.create_serializer_class is not None, (
"'%s' should either include a `create_serializer_class` attribute,"
"or override the `get_create_serializer_class()` method."
% self.__class__.__name__
)
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
class TextNodeViewSet(ReadWriteSerializerMixin,viewsets.ModelViewSet):
queryset = m.TextNode.objects.all()
read_serializer_class = views.TextNodeSerializer
write_serializer_class = views.TextNodeWriteSerializer

5
vue_frontend/src/components/AddNewNode.vue

@ -6,7 +6,7 @@
<button v-if="types.includes('ulohaZadaniNode')" v-on:click="selected='ulohaZadaniNode'" :disabled="selected && selected !== 'ulohaZadaniNode'">Zadání úlohy</button>
<button v-if="types.includes('ulohaVzorakNode')" v-on:click="selected='ulohaVzorakNode'" :disabled="selected && selected !== 'ulohaVzorakNode'">Vzorák</button>
<div v-if="selected">
<component :is='selected' :item='null' create></component>
<component :is='selected' :item='null' :where="where" :refnode="refnode" create></component>
</div>
</div>
</template>
@ -22,7 +22,8 @@ export default {
name: 'AddNewNode',
props: {
types: Array,
where: String
where: String,
refnode: Object,
},
data: () => ({
selected: null,

34
vue_frontend/src/components/TextNode.vue

@ -48,7 +48,9 @@ export default {
props: {
item: Object,
editorShow: Boolean,
create: Boolean
create: Boolean,
where: String,
refnode: Object
},
mounted: function() {
//console.log("mounted");
@ -82,8 +84,38 @@ export default {
updateText: function() {
console.log("Saving text");
console.log(this.currentText);
if (this.create){
console.log(this.refnode);
console.log(this.where);
axios.post('/api/textnode/',{
'text': { 'na_web': this.currentText},
'refnode': this.refnode.id,
'where': this.where
}).then(response => {this.originalText = response.data.text.na_web})
.catch(e => {
this.errors.push(e)
});
} else {
axios.put('/api/textnode/'+this.item.node.id+'/',{
'text': { 'na_web': this.currentText},
'id': this.item.node.id
}).then(response => {this.originalText = response.data.text.na_web})
.catch(e => {
this.errors.push(e)
});
}
// FIXME really save!
this.editorShow = false;
},
save: function() {
console.log(this.item);
if (this.create){
console.log(this.refnode);
console.log(this.where);
}
}
}
}

6
vue_frontend/src/components/TreeNode.vue

@ -9,12 +9,12 @@
<div v-if="item.children.length === 0">
<div v-if="item.appendable_children.length > 0">
<b>Vložit jako syna</b>
<addnewnode :types="item.appendable_siblings" where="syn" />
<addnewnode :types="item.appendable_siblings" :refnode="item.node" where="syn" />
</div>
</div>
<div v-if="item.children.length > 0 && item.children[0].appendable_siblings.length > 0">
<b>Vložit před</b>
<addnewnode :types="item.children[0].appendable_siblings" where="pred" />
<addnewnode :types="item.children[0].appendable_siblings" :refnode="item.children[0].node" where="pred" />
</div>
<ul>
@ -24,7 +24,7 @@
<div v-if="chld.appendable_siblings.length > 0">
<b v-if="index < (item.children.length - 1)">Vložit mezi</b>
<b v-else>Vložit za</b>
<addnewnode :types="chld.appendable_siblings" where="za" />
<addnewnode :types="chld.appendable_siblings" :refnode="chld.node" where="za" />
</div>
</li>
</ul>

Loading…
Cancel
Save