Browse Source

Update annotations api

pull/10/head
Hironsan 6 years ago
parent
commit
47435a285d
7 changed files with 21 additions and 38 deletions
  1. BIN
      app/db.sqlite3
  2. 40
      app/server/api.py
  3. 12
      app/server/serializers.py
  4. 2
      app/server/static/bundle/document_classification.js
  5. 2
      app/server/static/bundle/sequence_labeling.js
  6. 2
      app/server/static/js/document_classification.js
  7. 1
      app/server/static/js/sequence_labeling.js

BIN
app/db.sqlite3

40
app/server/api.py

@ -137,61 +137,35 @@ class AnnotationsAPI(generics.ListCreateAPIView):
permission_classes = (IsAuthenticated, IsProjectUser)
def get_serializer_class(self):
project_id = self.kwargs['project_id']
project = get_object_or_404(Project, pk=project_id)
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
self.serializer_class = project.get_annotation_serializer()
return self.serializer_class
def get_queryset(self):
project_id = self.kwargs['project_id']
project = get_object_or_404(Project, pk=project_id)
doc_id = self.kwargs['doc_id']
document = get_object_or_404(Document, pk=doc_id, project=project)
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
document = project.documents.get(id=self.kwargs['doc_id'])
self.queryset = document.get_annotations()
return self.queryset
def post(self, request, *args, **kwargs):
def perform_create(self, serializer):
doc = get_object_or_404(Document, pk=self.kwargs['doc_id'])
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
self.serializer_class = project.get_annotation_serializer()
if project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
label = get_object_or_404(Label, pk=request.data['label_id'])
annotation = DocumentAnnotation(document=doc, label=label, manual=True,
user=self.request.user)
elif project.is_type_of(Project.SEQUENCE_LABELING):
label = get_object_or_404(Label, pk=request.data['label_id'])
annotation = SequenceAnnotation(document=doc, label=label, manual=True,
user=self.request.user,
start_offset=request.data['start_offset'],
end_offset=request.data['end_offset'])
elif project.is_type_of(Project.Seq2seq):
text = request.data['text']
annotation = Seq2seqAnnotation(document=doc,
text=text,
manual=True,
user=self.request.user)
annotation.save()
serializer = self.serializer_class(annotation)
return Response(serializer.data)
serializer.save(document=doc, user=self.request.user)
class AnnotationAPI(generics.RetrieveUpdateDestroyAPIView):
permission_classes = (IsAuthenticated, IsProjectUser, IsOwnAnnotation)
def get_queryset(self):
doc_id = self.kwargs['doc_id']
document = get_object_or_404(Document, pk=doc_id)
document = get_object_or_404(Document, pk=self.kwargs['doc_id'])
self.queryset = document.get_annotations()
return self.queryset
def get_object(self):
annotation_id = self.kwargs['annotation_id']
queryset = self.filter_queryset(self.get_queryset())
obj = get_object_or_404(queryset, pk=annotation_id)
obj = get_object_or_404(queryset, pk=self.kwargs['annotation_id'])
self.check_object_permissions(self.request, obj)
return obj

12
app/server/serializers.py

@ -19,12 +19,16 @@ class TextSerializer(serializers.ModelSerializer):
class DocumentAnnotationSerializer(serializers.ModelSerializer):
label = LabelSerializer()
label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta:
model = DocumentAnnotation
fields = ('id', 'prob', 'label')
def create(self, validated_data):
annotation = DocumentAnnotation.objects.create(**validated_data)
return annotation
class DocumentSerializer(serializers.ModelSerializer):
labels = DocumentAnnotationSerializer(source='doc_annotations', many=True)
@ -35,12 +39,16 @@ class DocumentSerializer(serializers.ModelSerializer):
class SequenceAnnotationSerializer(serializers.ModelSerializer):
label = LabelSerializer()
label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta:
model = SequenceAnnotation
fields = ('id', 'prob', 'label', 'start_offset', 'end_offset')
def create(self, validated_data):
annotation = SequenceAnnotation.objects.create(**validated_data)
return annotation
class SequenceSerializer(serializers.ModelSerializer):
labels = SequenceAnnotationSerializer(source='seq_annotations', many=True)

2
app/server/static/bundle/document_classification.js

@ -161,7 +161,7 @@ eval("var g;\n\n// This works in non-strict mode\ng = (function() {\n\treturn th
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! vue */ \"./node_modules/vue/dist/vue.esm.js\");\n/* harmony import */ var _mixin_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./mixin.js */ \"./static/js/mixin.js\");\n/* harmony import */ var _http_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./http.js */ \"./static/js/http.js\");\n\nvue__WEBPACK_IMPORTED_MODULE_0__[\"default\"].use(__webpack_require__(/*! vue-shortkey */ \"./node_modules/vue-shortkey/dist/index.js\"), { prevent: ['input', 'textarea'] });\n\n\n\nvar vm = new vue__WEBPACK_IMPORTED_MODULE_0__[\"default\"]({\n el: '#mail-app',\n delimiters: ['[[', ']]'],\n mixins: [_mixin_js__WEBPACK_IMPORTED_MODULE_1__[\"default\"]],\n\n methods: {\n addLabel: async function (label_id) {\n for (var i = 0; i < this.items[this.cur]['labels'].length; i++) {\n var item = this.items[this.cur]['labels'][i];\n if (label_id == item.label.id) {\n this.deleteLabel(i);\n return;\n }\n }\n\n var payload = {\n 'label_id': label_id\n };\n\n var doc_id = this.items[this.cur].id;\n await _http_js__WEBPACK_IMPORTED_MODULE_2__[\"default\"].post(`docs/${doc_id}/annotations/`, payload).then(response => {\n this.items[this.cur]['labels'].push(response.data);\n })\n }\n }\n});\n\n//# sourceURL=webpack:///./static/js/document_classification.js?");
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! vue */ \"./node_modules/vue/dist/vue.esm.js\");\n/* harmony import */ var _mixin_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./mixin.js */ \"./static/js/mixin.js\");\n/* harmony import */ var _http_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./http.js */ \"./static/js/http.js\");\n\nvue__WEBPACK_IMPORTED_MODULE_0__[\"default\"].use(__webpack_require__(/*! vue-shortkey */ \"./node_modules/vue-shortkey/dist/index.js\"), { prevent: ['input', 'textarea'] });\n\n\n\nvar vm = new vue__WEBPACK_IMPORTED_MODULE_0__[\"default\"]({\n el: '#mail-app',\n delimiters: ['[[', ']]'],\n mixins: [_mixin_js__WEBPACK_IMPORTED_MODULE_1__[\"default\"]],\n\n methods: {\n addLabel: async function (label_id) {\n for (var i = 0; i < this.items[this.cur]['labels'].length; i++) {\n var item = this.items[this.cur]['labels'][i];\n if (label_id == item.label.id) {\n this.deleteLabel(i);\n return;\n }\n }\n\n var payload = {\n 'label': label_id\n };\n\n var doc_id = this.items[this.cur].id;\n await _http_js__WEBPACK_IMPORTED_MODULE_2__[\"default\"].post(`docs/${doc_id}/annotations/`, payload).then(response => {\n this.items[this.cur]['labels'].push(response.data);\n })\n }\n }\n});\n\n//# sourceURL=webpack:///./static/js/document_classification.js?");
/***/ }),

2
app/server/static/bundle/sequence_labeling.js
File diff suppressed because it is too large
View File

2
app/server/static/js/document_classification.js

@ -19,7 +19,7 @@ var vm = new Vue({
}
var payload = {
'label_id': label_id
'label': label_id
};
var doc_id = this.items[this.cur].id;

1
app/server/static/js/sequence_labeling.js

@ -127,6 +127,7 @@ var vm = new Vue({
addLabel: function (label) {
var payload = label;
var doc_id = this.items[this.cur].id;
payload['label'] = label.label_id;
HTTP.post(`docs/${doc_id}/annotations/`, payload).then(response => {
this.items[this.cur]['labels'].push(response.data);
})

Loading…
Cancel
Save