Browse Source

Add API

pull/10/head
Hironsan 6 years ago
parent
commit
538917340d
6 changed files with 60 additions and 71 deletions
  1. 2
      doccano/app/app/settings.py
  2. BIN
      doccano/app/db.sqlite3
  3. 2
      doccano/app/server/templates/base.html
  4. 0
      doccano/app/server/templates/projects.html
  5. 13
      doccano/app/server/urls.py
  6. 114
      doccano/app/server/views.py

2
doccano/app/app/settings.py

@ -114,7 +114,7 @@ REST_FRAMEWORK = {
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 2,
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'SEARCH_PARAM': 'q',
}

BIN
doccano/app/db.sqlite3

2
doccano/app/server/templates/base.html

@ -25,7 +25,7 @@
<div class="container">
<div class="navbar-brand">
<a class="navbar-item" href="{% url 'index' %}">
<img src="{% static 'images/logo.png' %}" width="40" height="48">doccano
<img src="{% static 'images/logo.png' %}" width="40" height="48">
</a>
</div>
<div id="topNav" class="navbar-menu">

doccano/app/server/templates/project_list.html → doccano/app/server/templates/projects.html

13
doccano/app/server/urls.py

@ -2,21 +2,22 @@ from django.urls import path
from .views import IndexView
from .views import AnnotationAPIView, ProgressAPI, SearchAPI, InboxView
from .views import ProjectListView, ProjectAdminView, RawDataAPI, LabelAPI, DataDownloadAPI
from .views import ProjectsView, ProjectAdminView, RawDataAPI, LabelAPI, DataDownloadAPI
from rest_framework import routers
from .views import LabelViewSet, ProjectViewSet, DocumentViewSet
from .views import ProjectViewSet
from .views import ProjectLabelsAPI, ProjectLabelAPI, ProjectDocsAPI
router = routers.DefaultRouter()
router.register(r'labels', LabelViewSet)
router.register(r'projects', ProjectViewSet)
router.register(r'documents', DocumentViewSet)
#router.register(r'users', UserViewSet)
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('projects/', ProjectListView.as_view(), name='project-list'),
path('api/projects/<int:project_id>/labels/', ProjectLabelsAPI.as_view(), name='labels'),
path('api/projects/<int:project_id>/labels/<int:label_id>', ProjectLabelAPI.as_view(), name='label'),
path('api/projects/<int:project_id>/docs/', ProjectDocsAPI.as_view(), name='docs'),
path('projects/', ProjectsView.as_view(), name='project-list'),
path('projects/<int:pk>/admin', ProjectAdminView.as_view(), name='project-admin'),
path('projects/<int:project_id>/download', DataDownloadAPI.as_view(), name='download'),
path('projects/<int:project_id>/', InboxView.as_view(), name='annotation'),

114
doccano/app/server/views.py

@ -2,32 +2,39 @@ import json
from django_filters.rest_framework import DjangoFilterBackend
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.views import View
from django.views.generic import TemplateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.paginator import Paginator
from rest_framework import viewsets, filters
from rest_framework import viewsets, filters, generics
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from .models import Annotation, Label, Document, Project
from .serializers import LabelSerializer, ProjectSerializer, DocumentSerializer
class IndexView(View):
class IndexView(TemplateView):
template_name = 'index.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
class InboxView(View):
class InboxView(LoginRequiredMixin, TemplateView):
template_name = 'annotation.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
class ProjectsView(LoginRequiredMixin, ListView):
model = Project
paginate_by = 100
template_name = 'projects.html'
class ProjectAdminView(LoginRequiredMixin, DetailView):
model = Project
template_name = 'project_admin.html'
class AnnotationAPIView(View):
@ -171,27 +178,6 @@ class RawDataAPI(View):
return JsonResponse({'status': 'ok'})
class ProjectListView(ListView):
model = Project
paginate_by = 100 # if pagination is desired
template_name = 'project_list.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
class ProjectAdminView(DetailView):
model = Project
template_name = 'project_admin.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
class DataDownloadAPI(View):
def get(self, request, *args, **kwargs):
@ -207,18 +193,6 @@ class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
@action(methods=['get'], detail=True)
def labels(self, request, pk=None):
project = self.get_object()
res = {label.id: label.text for label in project.labels.all()}
return Response(res)
@action(methods=['get'], detail=True)
def docs(self, request, pk=None):
project = self.get_object()
res = [doc.as_dict() for doc in project.documents.all()]
return Response(res)
@action(methods=['get'], detail=True)
def progress(self, request, pk=None):
project = self.get_object()
@ -226,30 +200,44 @@ class ProjectViewSet(viewsets.ModelViewSet):
remaining = docs.filter(labels__isnull=True).count()
return Response({'total': docs.count(), 'remaining': remaining})
@action(methods=['post'], detail=True)
def upload_doc(self, request, pk=None):
project = self.get_object()
f = request.FILES['file']
content = ''.join(chunk.decode('utf-8') for chunk in f.chunks())
for line in content.split('\n'):
j = json.loads(line)
Document(project=project, text=j['text']).save()
@action(methods=['get'], detail=True)
def download_doc(self, request, pk=None):
project = self.get_object()
res = []
return res
class ProjectLabelsAPI(generics.ListCreateAPIView):
queryset = Label.objects.all()
serializer_class = LabelSerializer
pagination_class = None
def get_queryset(self):
project_id = self.kwargs['project_id']
queryset = self.queryset.filter(project=project_id)
class DocumentViewSet(viewsets.ModelViewSet):
queryset = Document.objects.all()
serializer_class = DocumentSerializer
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
search_fields = ('text', )
return queryset
class LabelViewSet(viewsets.ModelViewSet):
class ProjectLabelAPI(generics.RetrieveUpdateDestroyAPIView):
queryset = Label.objects.all()
serializer_class = LabelSerializer
filter_fields = ('project',)
def get_queryset(self):
project_id = self.kwargs['project_id']
queryset = self.queryset.filter(project=project_id)
return queryset
def get_object(self):
label_id = self.kwargs['label_id']
queryset = self.filter_queryset(self.get_queryset())
obj = get_object_or_404(queryset, pk=label_id)
self.check_object_permissions(self.request, obj)
return obj
class ProjectDocsAPI(generics.ListCreateAPIView):
queryset = Document.objects.all()
serializer_class = DocumentSerializer
def get_queryset(self):
project_id = self.kwargs['project_id']
queryset = self.queryset.filter(project=project_id)
return queryset
Loading…
Cancel
Save