From d58cd28921586fb96caeb4a8fd4eb53b5bc3dbee Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Mon, 26 Jul 2021 15:51:53 +0300 Subject: [PATCH] iss1348: fix colors when importing labels Signed-off-by: Dimid Duchovny --- backend/api/views/label.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/api/views/label.py b/backend/api/views/label.py index be7551e9..dea5ac00 100644 --- a/backend/api/views/label.py +++ b/backend/api/views/label.py @@ -1,4 +1,5 @@ import json +import re from django.db import IntegrityError, transaction from django.shortcuts import get_object_or_404 @@ -14,6 +15,13 @@ from ..models import Label, Project from ..permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin from ..serializers import LabelSerializer +def camel_to_snake(name): + name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower() + +def camel_to_snake_dict(d): + return {camel_to_snake(k): v for k,v in d.items()} + class LabelList(generics.ListCreateAPIView): serializer_class = LabelSerializer @@ -52,6 +60,7 @@ class LabelUploadAPI(APIView): project = get_object_or_404(Project, pk=kwargs['project_id']) try: labels = json.load(request.data['file']) + labels = list(map(camel_to_snake_dict, labels)) serializer = LabelSerializer(data=labels, many=True) serializer.is_valid(raise_exception=True) serializer.save(project=project)