Browse Source

Add foreignkey to label and data

pull/10/head
Hironsan 6 years ago
parent
commit
75674126b6
4 changed files with 31 additions and 26 deletions
  1. BIN
      doccano/app/db.sqlite3
  2. 24
      doccano/app/server/models.py
  3. 15
      doccano/app/server/static/annotation.js
  4. 18
      doccano/app/server/views.py

BIN
doccano/app/db.sqlite3

24
doccano/app/server/models.py

@ -2,9 +2,21 @@ from django.db import models
from django.contrib.auth.models import User
class Project(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
users = models.ManyToManyField(User)
def __str__(self):
return self.name
class Label(models.Model):
text = models.CharField(max_length=100)
shortcut = models.CharField(max_length=10)
project = models.ForeignKey(Project, on_delete=models.CASCADE, null=True)
def as_dict(self):
return {'id': self.id,
@ -14,6 +26,7 @@ class Label(models.Model):
class RawData(models.Model):
text = models.TextField()
project = models.ForeignKey(Project, on_delete=models.CASCADE, null=True)
def as_dict(self):
return {'id': self.id,
@ -32,14 +45,3 @@ class Annotation(models.Model):
'label_id': self.label.id,
'prob': self.prob,
'manual': self.manual}
class Project(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
users = models.ManyToManyField(User)
def __str__(self):
return self.name

15
doccano/app/server/static/annotation.js

@ -87,12 +87,9 @@ var vm = new Vue({
data: {
cur: 0,
items: [{
"id": 10,
"labels": [{
"text": "Prefecture",
"prob": 0.98
}],
"text": 'document'
"id": null,
"labels": [],
"text": ''
}],
labels: [],
guideline: 'Here is the Annotation Guideline Text',
@ -169,11 +166,11 @@ var vm = new Vue({
},
created: function () {
var self = this;
axios.get('/' + base_url + '/apis/label')
axios.get('/' + base_url + '/apis/labels')
.then(function (response) {
self.labels = response.data['labels'];
self.total = response.data['total'];
self.remaining = response.data['remaining'];
//self.total = response.data['total'];
//self.remaining = response.data['remaining'];
})
.catch(function (error) {
console.log('ERROR!! happend by Backend.')

18
doccano/app/server/views.py

@ -19,6 +19,8 @@ class AnnotationView(View):
class AnnotationAPIView(View):
def get(self, request, *args, **kwargs):
project_id = kwargs.get('project_id')
project = Project.objects.get(id=project_id)
once_active_learned = len(Annotation.objects.all().exclude(prob=None)) > 0
if once_active_learned:
# Use Annotation model & RawData model.
@ -28,9 +30,12 @@ class AnnotationAPIView(View):
docs = Annotation.objects.all()
else:
# Left outer join data and annotation.
docs = RawData.objects.filter(annotation__isnull=True)
docs = RawData.objects.filter(annotation__isnull=True, project=project)
docs = [{**d.as_dict(), **{'labels': []}} for d in docs]
if not docs:
docs = [{'id': None, 'labels': [], 'text': ''}]
return JsonResponse({'data': docs})
def post(self, request, *args, **kwargs):
@ -54,12 +59,10 @@ class AnnotationAPIView(View):
class MetaInfoAPI(View):
def get(self, request, *args, **kwargs):
labels = Label.objects.all()
labels = [l.as_dict() for l in labels]
total = RawData.objects.count()
remaining = RawData.objects.filter(annotation__isnull=True).count()
return JsonResponse({'labels': labels, 'total': total, 'remaining': remaining})
return JsonResponse({'total': total, 'remaining': remaining})
class SearchAPI(View):
@ -80,7 +83,8 @@ class LabelAPI(View):
def get(self, request, *args, **kwargs):
"""Get labels."""
labels = Label.objects.all()
project_id = kwargs.get('pk')
labels = Label.objects.filter(project=project_id)
labels = [label.as_dict() for label in labels]
return JsonResponse({'labels': labels})
@ -89,11 +93,13 @@ class LabelAPI(View):
"""Create labels."""
#text = request.POST.get('text')
#shortcut = request.POST.get('shortcut')
project_id = kwargs.get('pk')
project = Project.objects.get(id=project_id)
body = request.body.decode('utf-8').replace("'", '"')
body = json.loads(body)
text = body.get('text')
shortcut = body.get('shortcut')
label = Label(text=text, shortcut=shortcut)
label = Label(text=text, shortcut=shortcut, project=project)
label.save()
return JsonResponse(label.as_dict())

Loading…
Cancel
Save