Browse Source

Merge pull request #1459 from dimidloc/fix-label-import

iss1348: fix colors when importing labels
pull/1520/head
Hiroki Nakayama 3 years ago
committed by GitHub
parent
commit
f0250dfda9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 0 deletions
  1. 9
      backend/api/views/label.py

9
backend/api/views/label.py

@ -1,4 +1,5 @@
import json import json
import re
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -14,6 +15,13 @@ from ..models import Label, Project
from ..permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin from ..permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin
from ..serializers import LabelSerializer 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): class LabelList(generics.ListCreateAPIView):
serializer_class = LabelSerializer serializer_class = LabelSerializer
@ -52,6 +60,7 @@ class LabelUploadAPI(APIView):
project = get_object_or_404(Project, pk=kwargs['project_id']) project = get_object_or_404(Project, pk=kwargs['project_id'])
try: try:
labels = json.load(request.data['file']) labels = json.load(request.data['file'])
labels = list(map(camel_to_snake_dict, labels))
serializer = LabelSerializer(data=labels, many=True) serializer = LabelSerializer(data=labels, many=True)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
serializer.save(project=project) serializer.save(project=project)

Loading…
Cancel
Save