Browse Source

Add serializers

pull/10/head
Hironsan 7 years ago
parent
commit
cbf879885f
7 changed files with 80 additions and 1 deletions
  1. 12
      doccano/app/app/settings.py
  2. 4
      doccano/app/app/urls.py
  3. BIN
      doccano/app/db.sqlite3
  4. 2
      doccano/app/server/models.py
  5. 34
      doccano/app/server/serializers.py
  6. 10
      doccano/app/server/urls.py
  7. 19
      doccano/app/server/views.py

12
doccano/app/app/settings.py

@ -39,6 +39,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'server.apps.ServerConfig', 'server.apps.ServerConfig',
'widget_tweaks', 'widget_tweaks',
'rest_framework',
'django_filters'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -105,6 +107,16 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 2,
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/ # https://docs.djangoproject.com/en/2.0/topics/i18n/

4
doccano/app/app/urls.py

@ -16,6 +16,7 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.contrib.auth.views import LoginView, PasswordResetView, LogoutView from django.contrib.auth.views import LoginView, PasswordResetView, LogoutView
from server.urls import router
urlpatterns = [ urlpatterns = [
@ -25,4 +26,7 @@ urlpatterns = [
redirect_authenticated_user=True), name='login'), redirect_authenticated_user=True), name='login'),
path('logout/', LogoutView.as_view(), name='logout'), path('logout/', LogoutView.as_view(), name='logout'),
path('password_reset/', PasswordResetView.as_view(), name='password_reset'), path('password_reset/', PasswordResetView.as_view(), name='password_reset'),
path('api-auth/', include('rest_framework.urls')),
path('api/', include(router.urls)),
] ]

BIN
doccano/app/db.sqlite3

2
doccano/app/server/models.py

@ -16,7 +16,7 @@ class Project(models.Model):
class Label(models.Model): class Label(models.Model):
text = models.CharField(max_length=100, unique=True) text = models.CharField(max_length=100, unique=True)
shortcut = models.CharField(max_length=10, unique=True) shortcut = models.CharField(max_length=10, unique=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE, null=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
def as_dict(self): def as_dict(self):
return {'id': self.id, return {'id': self.id,

34
doccano/app/server/serializers.py

@ -0,0 +1,34 @@
from rest_framework import serializers
from .models import Label, Project, Document, Annotation
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ('id', 'name', 'description', 'created_at', 'updated_at', 'users')
class LabelSerializer(serializers.ModelSerializer):
class Meta:
model = Label
fields = ('id', 'text', 'shortcut')
class AnnotationSerializer(serializers.ModelSerializer):
label = LabelSerializer()
class Meta:
model = Annotation
fields = ('id', 'prob', 'label')
class DocumentSerializer(serializers.ModelSerializer):
project = ProjectSerializer()
annotations = AnnotationSerializer(many=True)
class Meta:
model = Document
fields = ('id', 'text', 'project', 'annotations')

10
doccano/app/server/urls.py

@ -3,6 +3,16 @@ from django.urls import path
from .views import IndexView from .views import IndexView
from .views import AnnotationAPIView, ProgressAPI, SearchAPI, InboxView from .views import AnnotationAPIView, ProgressAPI, SearchAPI, InboxView
from .views import ProjectListView, ProjectAdminView, RawDataAPI, LabelAPI, DataDownloadAPI from .views import ProjectListView, ProjectAdminView, RawDataAPI, LabelAPI, DataDownloadAPI
from rest_framework import routers
from .views import LabelViewSet, ProjectViewSet, DocumentViewSet
router = routers.DefaultRouter()
router.register(r'labels', LabelViewSet)
router.register(r'projects', ProjectViewSet)
router.register(r'documents', DocumentViewSet)
#router.register(r'users', UserViewSet)
urlpatterns = [ urlpatterns = [
path('', IndexView.as_view(), name='index'), path('', IndexView.as_view(), name='index'),

19
doccano/app/server/views.py

@ -1,13 +1,16 @@
import json import json
import django_filters
from django.http import JsonResponse, HttpResponse from django.http import JsonResponse, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.core.paginator import Paginator from django.core.paginator import Paginator
from rest_framework import viewsets, filters
from .models import Annotation, Label, Document, Project from .models import Annotation, Label, Document, Project
from .serializers import LabelSerializer, ProjectSerializer, DocumentSerializer
class IndexView(View): class IndexView(View):
@ -195,3 +198,19 @@ class DataDownloadAPI(View):
response['Content-Disposition'] = 'attachment; filename=annotation_data.json' response['Content-Disposition'] = 'attachment; filename=annotation_data.json'
return response return response
class LabelViewSet(viewsets.ModelViewSet):
queryset = Label.objects.all()
serializer_class = LabelSerializer
filter_fields = ('text', 'project')
class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
class DocumentViewSet(viewsets.ModelViewSet):
queryset = Document.objects.all()
serializer_class = DocumentSerializer
Loading…
Cancel
Save