Browse Source

Implement file uploader

pull/10/head
Hironsan 6 years ago
parent
commit
1861d1e49b
5 changed files with 53 additions and 5 deletions
  1. BIN
      doccano/app/db.sqlite3
  2. 18
      doccano/app/server/data/test.jsonl
  3. 29
      doccano/app/server/tests.py
  4. 3
      doccano/app/server/urls.py
  5. 8
      doccano/app/server/views.py

BIN
doccano/app/db.sqlite3

18
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}

29
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)

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
from .views import ProjectListView, ProjectDetailView, ProjectAdminView, RawDataAPI
urlpatterns = [
path('', ProjectListView.as_view(), name='project-list'),
@ -9,6 +9,7 @@ urlpatterns = [
path('<int:pk>/admin', ProjectAdminView.as_view()),
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:project_id>/apis/label', MetaInfoAPI.as_view()),
path('<int:project_id>/apis/search', SearchAPI.as_view()),
]

8
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'})

Loading…
Cancel
Save