From 1861d1e49b1a601ee26119c4515708f041ad1a80 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 4 Jun 2018 10:59:23 +0900 Subject: [PATCH] Implement file uploader --- doccano/app/db.sqlite3 | Bin 180224 -> 180224 bytes doccano/app/server/data/test.jsonl | 18 ++++++++++++++++++ doccano/app/server/tests.py | 29 +++++++++++++++++++++++++++-- doccano/app/server/urls.py | 3 ++- doccano/app/server/views.py | 8 ++++++-- 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 doccano/app/server/data/test.jsonl diff --git a/doccano/app/db.sqlite3 b/doccano/app/db.sqlite3 index f4c2b159a91174158592330f20d238dc65a9972c..a48584b1b6b7bf304072f49cf59e7c9372a27941 100644 GIT binary patch delta 273 zcmZo@;BIK(o*>P5cA|_k%k%6Iwu7Q~@rsXUk R>FHU%Oj6qqP5YNCuYtjMM#mm=vaO mi)Zo!N-tsHf5d;7e+f`}8b6C1vm@j5EMF$6?T7N2>;eF(vL}TA diff --git a/doccano/app/server/data/test.jsonl b/doccano/app/server/data/test.jsonl new file mode 100644 index 00000000..c386876b --- /dev/null +++ b/doccano/app/server/data/test.jsonl @@ -0,0 +1,18 @@ +{"id": 5, "label": "2", "text": "\u30a2\u30f3\u30d1\u30b5\u30f3\u30c9", "manual": true, "prob": 0.5, "manual": true, "prob": 0.5} +{"id": 10, "label": "2", "text": "\u8a00\u8a9e", "manual": true, "prob": 0.5} +{"id": 11, "label": "1", "text": "\u65e5\u672c\u8a9e", "manual": true, "prob": 0.5} +{"id": 12, "label": "1", "text": "\u5730\u7406\u5b66", "manual": true, "prob": 0.5} +{"id": 23, "label": "2", "text": "\u56fd\u306e\u4e00\u89a7", "manual": false, "prob": 0.5} +{"id": 31, "label": "2", "text": "\u30d1\u30ea", "manual": false, "prob": 0.5} +{"id": 32, "label": "3", "text": "\u30e8\u30fc\u30ed\u30c3\u30d1", "manual": false, "prob": 0.5} +{"id": 42, "label": "2", "text": "\u751f\u7269", "manual": true, "prob": 0.5} +{"id": 43, "label": "3", "text": "\u30b3\u30b1\u690d\u7269", "manual": false, "prob": 0.5} +{"id": 47, "label": "3", "text": "\u793e\u4f1a\u5b66", "manual": true, "prob": 0.5} +{"id": 111, "label": "1", "text": "\u65e5\u672c\u8a9e", "manual": true, "prob": 0.5} +{"id": 112, "label": "1", "text": "\u5730\u7406\u5b66", "manual": true, "prob": 0.5} +{"id": 123, "label": "2", "text": "\u56fd\u306e\u4e00\u89a7", "manual": false, "prob": 0.5} +{"id": 131, "label": "2", "text": "\u30d1\u30ea", "manual": false, "prob": 0.5} +{"id": 132, "label": "3", "text": "\u30e8\u30fc\u30ed\u30c3\u30d1", "manual": true, "prob": 0.5} +{"id": 142, "label": "2", "text": "\u751f\u7269", "manual": true, "prob": 0.5} +{"id": 143, "label": "1", "text": "\u30b3\u30b1\u690d\u7269", "manual": false, "prob": 0.5} +{"id": 147, "label": "3", "text": "\u793e\u4f1a\u5b66", "manual": true, "prob": 0.5} \ No newline at end of file diff --git a/doccano/app/server/tests.py b/doccano/app/server/tests.py index daee335f..04e964ae 100644 --- a/doccano/app/server/tests.py +++ b/doccano/app/server/tests.py @@ -1,6 +1,9 @@ -from django.test import TestCase +import os -from .models import Project +from django.test import TestCase, Client +from django.urls import reverse + +from .models import Project, RawData class ProjectModelTest(TestCase): @@ -8,3 +11,25 @@ class ProjectModelTest(TestCase): def test_string_representation(self): project = Project(name='my project', description='my description') self.assertEqual(str(project), project.name) + + +class TestRawDataAPI(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(RawData.objects.count(), 0) + + filename = os.path.join(os.path.dirname(__file__), 'data/test.jsonl') + client = Client() + with open(filename) as f: + client.post(reverse('data_api', args=[1]), + {'name': 'fred', 'attachment': f}) + + self.assertGreater(RawData.objects.count(), 0) diff --git a/doccano/app/server/urls.py b/doccano/app/server/urls.py index adecfe82..26c816af 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 +from .views import ProjectListView, ProjectDetailView, ProjectAdminView, RawDataAPI urlpatterns = [ path('', ProjectListView.as_view(), name='project-list'), @@ -9,6 +9,7 @@ urlpatterns = [ path('/admin', ProjectAdminView.as_view()), path('/docs', AnnotationView.as_view()), path('/apis/data', AnnotationAPIView.as_view()), + path('/apis/raw_data', RawDataAPI.as_view(), name='data_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 0d6f5e12..cdfc3f87 100644 --- a/doccano/app/server/views.py +++ b/doccano/app/server/views.py @@ -110,8 +110,12 @@ class RawDataAPI(View): def post(self, request, *args, **kwargs): """Upload data.""" - print(request.FILES['file']) - # RawData().save() + f = request.FILES['attachment'] + content = ''.join(chunk.decode('utf-8') for chunk in f.chunks()) + for line in content.split('\n'): + j = json.loads(line) + RawData(text=j['text']).save() + return JsonResponse({'status': 'ok'})