Browse Source

Implement label api

pull/10/head
Hironsan 6 years ago
parent
commit
a96ebbb011
3 changed files with 70 additions and 6 deletions
  1. 50
      doccano/app/server/tests.py
  2. 3
      doccano/app/server/urls.py
  3. 23
      doccano/app/server/views.py

50
doccano/app/server/tests.py

@ -3,7 +3,7 @@ import os
from django.test import TestCase, Client
from django.urls import reverse
from .models import Project, RawData
from .models import Project, RawData, Label
class ProjectModelTest(TestCase):
@ -33,3 +33,51 @@ class TestRawDataAPI(TestCase):
{'name': 'fred', 'attachment': f})
self.assertGreater(RawData.objects.count(), 0)
class TestLabelAPI(TestCase):
def test_get(self):
client = Client()
res = client.get(reverse('data_api', args=[1]))
self.assertEqual(res.status_code, 200)
self.assertIsInstance(res.json(), dict)
self.assertIn('data', res.json())
self.assertEqual(res.json()['data'], [])
def test_post(self):
self.assertEqual(Label.objects.count(), 0)
client = Client()
res = client.post(reverse('label_api', args=[1]),
data={'text': 'label1', 'shortcut': 'k'})
self.assertGreater(Label.objects.count(), 0)
self.assertEqual(Label.objects.count(), 1)
def test_put(self):
self.assertEqual(Label.objects.count(), 0)
client = Client()
res = client.post(reverse('label_api', args=[1]),
data={'text': 'label1', 'shortcut': 'k'})
self.assertEqual(Label.objects.count(), 1)
prev_label = Label.objects.all()[0]
text = 'neko'
shortcut = 'P'
res = client.put(reverse('label_api', args=[1]),
data={"id": prev_label.id, 'text': text, 'shortcut': shortcut})
self.assertEqual(Label.objects.count(), 1)
post_label = Label.objects.all()[0]
self.assertEqual(prev_label.id, post_label.id)
self.assertEqual(post_label.text, text)
self.assertEqual(post_label.shortcut, shortcut)
def test_delete(self):
self.assertEqual(Label.objects.count(), 0)
client = Client()
res = client.post(reverse('label_api', args=[1]),
data={'text': 'label1', 'shortcut': 'k'})
self.assertEqual(Label.objects.count(), 1)
label_id = Label.objects.all()[0].id
res = client.delete(reverse('label_api', args=[1]),
data={"id": label_id})
self.assertEqual(Label.objects.count(), 0)

3
doccano/app/server/urls.py

@ -1,7 +1,7 @@
from django.urls import path
from .views import AnnotationView, AnnotationAPIView, MetaInfoAPI, SearchAPI
from .views import ProjectListView, ProjectDetailView, ProjectAdminView, RawDataAPI
from .views import ProjectListView, ProjectDetailView, ProjectAdminView, RawDataAPI, LabelAPI
urlpatterns = [
path('', ProjectListView.as_view(), name='project-list'),
@ -10,6 +10,7 @@ urlpatterns = [
path('<int:project_id>/docs', AnnotationView.as_view()),
path('<int:project_id>/apis/data', AnnotationAPIView.as_view()),
path('<int:pk>/apis/raw_data', RawDataAPI.as_view(), name='data_api'),
path('<int:pk>/apis/labels', LabelAPI.as_view(), name='label_api'),
path('<int:project_id>/apis/label', MetaInfoAPI.as_view()),
path('<int:project_id>/apis/search', SearchAPI.as_view()),
]

23
doccano/app/server/views.py

@ -87,19 +87,34 @@ class LabelAPI(View):
def post(self, request, *args, **kwargs):
"""Create labels."""
Label().save()
text = request.POST.get('text')
shortcut = request.POST.get('shortcut')
Label(text=text, shortcut=shortcut).save()
return JsonResponse({'status': 'ok'})
def put(self, request, *args, **kwargs):
"""Update labels."""
label = Label.objects.get(id=1)
label.text = ''
label.shortcut = ''
body = request.body.decode('utf-8').replace("'", '"')
body = json.loads(body)
label_id = body.get('id')
text = body.get('text')
shortcut = body.get('shortcut')
label = Label.objects.get(id=label_id)
label.text = text
label.shortcut = shortcut
label.save()
return JsonResponse({'status': 'ok'})
def delete(self, request, *args, **kwargs):
body = request.body.decode('utf-8').replace("'", '"')
body = json.loads(body)
label_id = body.get('id')
Label.objects.get(id=label_id).delete()
return JsonResponse({'status': 'ok'})
class RawDataAPI(View):

Loading…
Cancel
Save