From a96ebbb011bf2123d49ff804ae730fff35c615be Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 4 Jun 2018 11:39:08 +0900 Subject: [PATCH] Implement label api --- doccano/app/server/tests.py | 50 ++++++++++++++++++++++++++++++++++++- doccano/app/server/urls.py | 3 ++- doccano/app/server/views.py | 23 ++++++++++++++--- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/doccano/app/server/tests.py b/doccano/app/server/tests.py index 04e964ae..dd58d89d 100644 --- a/doccano/app/server/tests.py +++ b/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) diff --git a/doccano/app/server/urls.py b/doccano/app/server/urls.py index 26c816af..f8347b21 100644 --- a/doccano/app/server/urls.py +++ b/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('/docs', AnnotationView.as_view()), path('/apis/data', AnnotationAPIView.as_view()), path('/apis/raw_data', RawDataAPI.as_view(), name='data_api'), + path('/apis/labels', LabelAPI.as_view(), name='label_api'), path('/apis/label', MetaInfoAPI.as_view()), path('/apis/search', SearchAPI.as_view()), ] diff --git a/doccano/app/server/views.py b/doccano/app/server/views.py index cdfc3f87..ed723731 100644 --- a/doccano/app/server/views.py +++ b/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):