Browse Source

#27642 relation_types serializer, url, exception and get all api

pull/1384/head
descansodj@hotmail.it 3 years ago
parent
commit
9f9bd971cf
6 changed files with 92 additions and 3 deletions
  1. 5
      app/api/exceptions.py
  2. 10
      app/api/models.py
  3. 12
      app/api/serializers.py
  4. 5
      app/api/urls.py
  5. 1
      app/api/views/__init__.py
  6. 62
      app/api/views/relation_types.py

5
app/api/exceptions.py

@ -41,6 +41,11 @@ class LabelValidationError(APIException):
default_detail = 'You cannot create a label with same name or shortcut key.' default_detail = 'You cannot create a label with same name or shortcut key.'
class RelationTypesValidationError(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = 'You cannot create a relation type with same name or color.'
class RoleConstraintException(APIException): class RoleConstraintException(APIException):
status_code = status.HTTP_400_BAD_REQUEST status_code = status.HTTP_400_BAD_REQUEST
default_detail = 'The project needs at least one administrator.' default_detail = 'The project needs at least one administrator.'

10
app/api/models.py

@ -265,21 +265,27 @@ class SequenceAnnotation(Annotation):
unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset') unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
class AnnotationRelations:
class AnnotationRelations(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True) timestamp = models.DateTimeField(auto_now_add=True)
annotation_id_1 = models.IntegerField() annotation_id_1 = models.IntegerField()
annotation_id_2 = models.IntegerField() annotation_id_2 = models.IntegerField()
type = models.CharField(max_length=50) type = models.CharField(max_length=50)
def __str__(self):
return f"{self.annotation_id_1} - {self.annotation_id_2} - {type}"
class Meta: class Meta:
unique_together = ('timestamp', 'user', 'annotation_id_1', 'annotation_id_2', 'type') unique_together = ('timestamp', 'user', 'annotation_id_1', 'annotation_id_2', 'type')
class RelationTypes:
class RelationTypes(models.Model):
color = models.TextField() color = models.TextField()
name = models.TextField() name = models.TextField()
def __str__(self):
return self.name
class Meta: class Meta:
unique_together = ('color', 'name') unique_together = ('color', 'name')

12
app/api/serializers.py

@ -10,7 +10,7 @@ from .models import (AutoLabelingConfig, Comment, Document, DocumentAnnotation,
Label, Project, Role, RoleMapping, Seq2seqAnnotation, Label, Project, Role, RoleMapping, Seq2seqAnnotation,
Seq2seqProject, SequenceAnnotation, Seq2seqProject, SequenceAnnotation,
SequenceLabelingProject, Speech2textAnnotation, SequenceLabelingProject, Speech2textAnnotation,
Speech2textProject, TextClassificationProject)
Speech2textProject, TextClassificationProject, RelationTypes)
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
@ -277,3 +277,13 @@ class AutoLabelingConfigSerializer(serializers.ModelSerializer):
'You need to correctly specify the required fields: {}'.format(required_fields) 'You need to correctly specify the required fields: {}'.format(required_fields)
) )
return data return data
class RelationTypesSerializer(serializers.ModelSerializer):
def validate(self, attrs):
return super().validate(attrs)
class Meta:
model = RelationTypes
fields = ('id', 'color', 'name')

5
app/api/urls.py

@ -168,6 +168,11 @@ urlpatterns = [
view=views.AutoLabelingConfigParameterTest.as_view(), view=views.AutoLabelingConfigParameterTest.as_view(),
name='auto_labeling_parameter_testing' name='auto_labeling_parameter_testing'
), ),
path( #TODO: questo va sotto urlpatterns_project e deve prendere un int:project_id> come sotto
route='relationtypes',
view=views.RelationTypesList.as_view(),
name='roles'
),
path( path(
route='projects/<int:project_id>', route='projects/<int:project_id>',
view=views.ProjectDetail.as_view(), view=views.ProjectDetail.as_view(),

1
app/api/views/__init__.py

@ -9,3 +9,4 @@ from .project import *
from .role import * from .role import *
from .statistics import * from .statistics import *
from .user import * from .user import *
from .relation_types import *

62
app/api/views/relation_types.py

@ -0,0 +1,62 @@
import json
import logging
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 ..exceptions import RelationTypesValidationError
from ..models import RelationTypes, Project
from ..permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin
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 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)
@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
Loading…
Cancel
Save