diff --git a/backend/api/urls.py b/backend/api/urls.py index 528007cc..8face475 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -2,8 +2,7 @@ from django.urls import include, path from .views import (annotation, auto_labeling, comment, example, example_state, export_dataset, health, import_dataset, import_export, - label, project, relation_types, statistics, tag, task, - user) + label, project, statistics, tag, task, user) from .views.tasks import category, relation, span, text urlpatterns_project = [ @@ -89,17 +88,17 @@ urlpatterns_project = [ ), path( route='relation_types', - view=relation_types.RelationTypesList.as_view(), + view=label.RelationTypeList.as_view(), name='relation_types_list' ), path( route='relation_type-upload', - view=relation_types.RelationTypesUploadAPI.as_view(), + view=label.RelationTypeUploadAPI.as_view(), name='relation_type-upload' ), path( route='relation_types/', - view=relation_types.RelationTypesDetail.as_view(), + view=label.RelationTypeDetail.as_view(), name='relation_type_detail' ), path( diff --git a/backend/api/views/label.py b/backend/api/views/label.py index 87fc5ff1..f0b0626a 100644 --- a/backend/api/views/label.py +++ b/backend/api/views/label.py @@ -14,9 +14,9 @@ from rest_framework.views import APIView from members.permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin from ..exceptions import LabelValidationError -from ..models import CategoryType, Label, Project, SpanType +from ..models import CategoryType, Label, Project, RelationTypes, SpanType from ..serializers import (CategoryTypeSerializer, LabelSerializer, - SpanTypeSerializer) + RelationTypesSerializer, SpanTypeSerializer) def camel_to_snake(name): @@ -49,13 +49,6 @@ class LabelList(generics.ListCreateAPIView): return Response(status=status.HTTP_204_NO_CONTENT) -# class LabelDetail(generics.RetrieveUpdateDestroyAPIView): -# queryset = Label.objects.all() -# serializer_class = LabelSerializer -# lookup_url_kwarg = 'label_id' -# permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - - class CategoryTypeList(LabelList): model = CategoryType serializer_class = CategoryTypeSerializer @@ -80,6 +73,18 @@ class SpanTypeDetail(generics.RetrieveUpdateDestroyAPIView): permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] +class RelationTypeList(LabelList): + model = RelationTypes + serializer_class = RelationTypesSerializer + + +class RelationTypeDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = RelationTypes.objects.all() + serializer_class = RelationTypesSerializer + lookup_url_kwarg = 'relation_type_id' + permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] + + class LabelUploadAPI(APIView): parser_classes = (MultiPartParser,) permission_classes = [IsAuthenticated & IsProjectAdmin] @@ -109,3 +114,7 @@ class CategoryTypeUploadAPI(LabelUploadAPI): class SpanTypeUploadAPI(LabelUploadAPI): serializer_class = SpanTypeSerializer + + +class RelationTypeUploadAPI(LabelUploadAPI): + serializer_class = RelationTypesSerializer diff --git a/backend/api/views/relation_types.py b/backend/api/views/relation_types.py deleted file mode 100644 index fe5186b4..00000000 --- a/backend/api/views/relation_types.py +++ /dev/null @@ -1,97 +0,0 @@ -import json - -from django.db import IntegrityError, transaction -from django.shortcuts import get_object_or_404 -from rest_framework import generics, status -from rest_framework.exceptions import ParseError -from rest_framework.parsers import MultiPartParser -from rest_framework.permissions import IsAuthenticated -from rest_framework.response import Response -from rest_framework.views import APIView - -from members.permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin - -from ..exceptions import RelationTypesValidationError -from ..models import Project, RelationTypes -from ..serializers import RelationTypesSerializer - - -class RelationTypesList(generics.ListCreateAPIView): - serializer_class = RelationTypesSerializer - pagination_class = None - permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - - def get_queryset(self): - project = get_object_or_404(Project, pk=self.kwargs['project_id']) - return project.relation_types - - def perform_create(self, serializer): - project = get_object_or_404(Project, pk=self.kwargs['project_id']) - serializer.save(project=project) - - def delete(self, request, *args, **kwargs): - delete_ids = request.data['ids'] - RelationTypes.objects.filter(pk__in=delete_ids).delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - -class RelationTypesDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = RelationTypes.objects.all() - serializer_class = RelationTypesSerializer - lookup_url_kwarg = 'relation_type_id' - permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - - -class RelationTypesUploadAPI(APIView): - parser_classes = (MultiPartParser,) - permission_classes = [IsAuthenticated & IsProjectAdmin] - - @transaction.atomic - def post(self, request, *args, **kwargs): - if 'file' not in request.data: - raise ParseError('Empty content') - project = get_object_or_404(Project, pk=kwargs['project_id']) - try: - relation_types = json.load(request.data) - serializer = RelationTypesSerializer(data=relation_types, many=True) - serializer.is_valid(raise_exception=True) - serializer.save(project=project) - return Response(status=status.HTTP_201_CREATED) - except json.decoder.JSONDecodeError: - raise ParseError('The file format is invalid.') - except IntegrityError: - raise RelationTypesValidationError - - -# class RelationTypesList(APIView): -# permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] -# -# def get(self, request, *args, **kwargs): -# relation_types = RelationTypes.objects.all() -# serializer = RelationTypesSerializer(relation_types, many=True) -# return Response(serializer.data) -# -# def get_by_id(self, request, *args, **kwargs): -# id = json.load(request.data['id']) -# logging.info(f"Requested:: {id}") -# relation_type = RelationTypes.objects.get(pk=id) -# serializer = RelationTypesSerializer(relation_type, many=True) -# return Response(serializer.data) -# -# @transaction.atomic -# def post(self, request, *args, **kwargs): -# if 'file' not in request.data: -# raise ParseError('Empty content') -# # project = get_object_or_404(Project, pk=kwargs['project_id']) -# try: -# logging.info(f"request.data:: {request.data}") -# relation_type = json.load(request.data['relation_type']) -# serializer = RelationTypesSerializer(relation_type, many=False) -# serializer.is_valid(raise_exception=True) -# # serializer.save(project=project) -# serializer.save() -# return Response(status=status.HTTP_201_CREATED) -# except json.decoder.JSONDecodeError: -# raise ParseError('The file format is invalid.') -# except IntegrityError: -# raise RelationTypesValidationError