From 92f7c5a75c07fee6c5368160e9a98966a1670cb0 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 11 Apr 2019 15:18:56 +0900 Subject: [PATCH] Implement me API --- app/server/api.py | 10 +++++++++- app/server/api_urls.py | 2 ++ app/server/serializers.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/server/api.py b/app/server/api.py index 9fadb468..bc853c4d 100644 --- a/app/server/api.py +++ b/app/server/api.py @@ -14,13 +14,21 @@ from rest_framework_csv.renderers import CSVRenderer from .filters import DocumentFilter from .models import Project, Label, Document from .permissions import IsAdminUserAndWriteOnly, IsProjectUser, IsOwnAnnotation -from .serializers import ProjectSerializer, LabelSerializer, DocumentSerializer +from .serializers import ProjectSerializer, LabelSerializer, DocumentSerializer, UserSerializer from .serializers import ProjectPolymorphicSerializer from .utils import CSVParser, JSONParser, PlainTextParser, CoNLLParser from .utils import JSONLRenderer from .utils import JSONPainter, CSVPainter +class Me(APIView): + permission_classes = (IsAuthenticated,) + + def get(self, request, *args, **kwargs): + serializer = UserSerializer(request.user, context={'request': request}) + return Response(serializer.data) + + class ProjectList(generics.ListCreateAPIView): queryset = Project.objects.all() serializer_class = ProjectPolymorphicSerializer diff --git a/app/server/api_urls.py b/app/server/api_urls.py index 8e173541..15f894d9 100644 --- a/app/server/api_urls.py +++ b/app/server/api_urls.py @@ -1,6 +1,7 @@ from django.urls import path from rest_framework.urlpatterns import format_suffix_patterns +from .api import Me from .api import ProjectList, ProjectDetail from .api import LabelList, LabelDetail from .api import DocumentList, DocumentDetail @@ -10,6 +11,7 @@ from .api import StatisticsAPI urlpatterns = [ + path('me', Me.as_view(), name='me'), path('projects', ProjectList.as_view(), name='project_list'), path('projects/', ProjectDetail.as_view(), name='project_detail'), path('projects//statistics', diff --git a/app/server/serializers.py b/app/server/serializers.py index 889286da..760f1543 100644 --- a/app/server/serializers.py +++ b/app/server/serializers.py @@ -1,3 +1,4 @@ +from django.contrib.auth import get_user_model from rest_framework import serializers from rest_polymorphic.serializers import PolymorphicSerializer @@ -6,6 +7,13 @@ from .models import TextClassificationProject, SequenceLabelingProject, Seq2seqP from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation +class UserSerializer(serializers.ModelSerializer): + + class Meta: + model = get_user_model() + fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser') + + class LabelSerializer(serializers.ModelSerializer): class Meta: