Browse Source

#27642 relation_types get all, get by id, post

pull/1384/head
descansodj@hotmail.it 3 years ago
parent
commit
704b20292b
3 changed files with 79 additions and 33 deletions
  1. 1
      app/api/models.py
  2. 20
      app/api/urls.py
  3. 91
      app/api/views/relation_types.py

1
app/api/models.py

@ -282,6 +282,7 @@ class AnnotationRelations(models.Model):
class RelationTypes(models.Model):
color = models.TextField()
name = models.TextField()
project = models.ForeignKey(Project, related_name='relation_types', on_delete=models.CASCADE)
def __str__(self):
return self.name

20
app/api/urls.py

@ -23,6 +23,21 @@ urlpatterns_project = [
view=views.LabelDetail.as_view(),
name='label_detail'
),
path(
route='relation_types',
view=views.RelationTypesList.as_view(),
name='relation_types_list'
),
path(
route='relation_type-upload',
view=views.RelationTypesUploadAPI.as_view(),
name='relation_type-upload'
),
path(
route='relation_types/<int:relation_type_id>',
view=views.RelationTypesDetail.as_view(),
name='relation_type_detail'
),
path(
route='docs',
view=views.DocumentList.as_view(),
@ -168,11 +183,6 @@ urlpatterns = [
view=views.AutoLabelingConfigParameterTest.as_view(),
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(
route='projects/<int:project_id>',
view=views.ProjectDetail.as_view(),

91
app/api/views/relation_types.py

@ -16,47 +16,82 @@ 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(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):
class RelationTypesDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = RelationTypes.objects.all()
serializer_class = RelationTypesSerializer
lookup_url_kwarg = 'relation_type_id'
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)
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'])
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)
relation_types = json.load(request.data)
serializer = RelationTypesSerializer(data=relation_types, many=True)
serializer.is_valid(raise_exception=True)
# serializer.save(project=project)
serializer.save()
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
Loading…
Cancel
Save