Skip to content

Commit

Permalink
Label name editing (#2806)
Browse files Browse the repository at this point in the history
* Label name edition

* Added wrong label id handling

* Restored missed logging
  • Loading branch information
ActiveChooN authored Feb 16, 2021
1 parent 383e6ec commit 5504ba1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
10 changes: 7 additions & 3 deletions cvat-core/src/labels.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019-2020 Intel Corporation
// Copyright (C) 2019-2021 Intel Corporation
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -171,17 +171,21 @@
* @name name
* @type {string}
* @memberof module:API.cvat.classes.Label
* @readonly
* @instance
*/
name: {
get: () => data.name,
set: (name) => {
if (typeof name !== 'string') {
throw new ArgumentError(`Name must be a string, but ${typeof name} was given`);
}
data.name = name;
},
},
/**
* @name color
* @type {string}
* @memberof module:API.cvat.classes.Label
* @readonly
* @instance
*/
color: {
Expand Down
3 changes: 1 addition & 2 deletions cvat-ui/src/components/labels-editor/label-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ export default class LabelForm extends React.Component<Props> {
private renderLabelNameInput(): JSX.Element {
const { label, labelNames } = this.props;
const value = label ? label.name : '';
const locked = label ? label.id >= 0 : false;

return (
<Form.Item
Expand All @@ -399,7 +398,7 @@ export default class LabelForm extends React.Component<Props> {
},
]}
>
<Input disabled={locked} placeholder='Label name' />
<Input placeholder='Label name' />
</Form.Item>
);
}
Expand Down
18 changes: 12 additions & 6 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import shutil

from rest_framework import serializers
from rest_framework import serializers, exceptions
from django.contrib.auth.models import User, Group

from cvat.apps.engine import models
Expand Down Expand Up @@ -66,6 +66,7 @@ def to_representation(self, instance):
return attribute

class LabelSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False)
attributes = AttributeSerializer(many=True, source='attributespec_set',
default=[])
color = serializers.CharField(allow_blank=True, required=False)
Expand All @@ -84,12 +85,17 @@ def update_instance(validated_data, parent_instance):
else:
instance['task'] = parent_instance
logger = slogger.task[parent_instance.id]
(db_label, created) = models.Label.objects.get_or_create(name=validated_data['name'],
**instance)
if created:
logger.info("New {} label was created".format(db_label.name))
if not validated_data.get('id') is None:
try:
db_label = models.Label.objects.get(id=validated_data['id'],
**instance)
except models.Label.DoesNotExist:
raise exceptions.NotFound(detail='Not found label with id #{} to change'.format(validated_data['id']))
db_label.name = validated_data.get('name', db_label.name)
logger.info("{}({}) label was updated".format(db_label.name, db_label.id))
else:
logger.info("{} label was updated".format(db_label.name))
db_label = models.Label.objects.create(name=validated_data.get('name'), **instance)
logger.info("New {} label was created".format(db_label.name))
if not validated_data.get('color', None):
label_names = [l.name for l in
instance[tuple(instance.keys())[0]].label_set.exclude(id=db_label.id).order_by('id')
Expand Down

0 comments on commit 5504ba1

Please sign in to comment.