Browse Source

Merge pull request #1564 from doccano/enhancement/assignRandomColor

Assign label colors automatically
pull/1565/head
Hiroki Nakayama 3 years ago
committed by GitHub
parent
commit
f5c52b7353
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions
  1. 19
      backend/api/migrations/0018_alter_label_background_color.py
  2. 7
      backend/api/models.py
  3. 11
      backend/api/tests/test_models.py

19
backend/api/migrations/0018_alter_label_background_color.py

@ -0,0 +1,19 @@
# Generated by Django 3.2.8 on 2021-11-17 05:56
import api.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0017_example_uuid'),
]
operations = [
migrations.AlterField(
model_name='label',
name='background_color',
field=models.CharField(default=api.models.generate_random_hex_color, max_length=7),
),
]

7
backend/api/models.py

@ -1,3 +1,4 @@
import random
import string
import uuid
from typing import Literal
@ -94,6 +95,10 @@ class ImageClassificationProject(Project):
return task == 'image'
def generate_random_hex_color():
return f'#{random.randint(0, 0xFFFFFF):06x}'
class Label(models.Model):
text = models.CharField(max_length=100, db_index=True)
prefix_key = models.CharField(
@ -119,7 +124,7 @@ class Label(models.Model):
on_delete=models.CASCADE,
related_name='labels'
)
background_color = models.CharField(max_length=7, default='#209cee')
background_color = models.CharField(max_length=7, default=generate_random_hex_color)
text_color = models.CharField(max_length=7, default='#ffffff')
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
updated_at = models.DateTimeField(auto_now=True)

11
backend/api/tests/test_models.py

@ -3,7 +3,8 @@ from django.db.utils import IntegrityError
from django.test import TestCase, override_settings
from model_mommy import mommy
from ..models import Category, Label, Span, TextLabel
from ..models import (Category, Label, Span, TextLabel,
generate_random_hex_color)
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
@ -141,3 +142,11 @@ class TestSeq2seqAnnotation(TestCase):
TextLabel(example=a.example,
user=a.user,
text=a.text).save()
class TestGeneratedColor(TestCase):
def test_length(self):
for i in range(100):
color = generate_random_hex_color()
self.assertEqual(len(color), 7)
Loading…
Cancel
Save