From 39351d3f6965079263875f16781693f2cfcfe5a6 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 17 Nov 2021 14:57:49 +0900 Subject: [PATCH 1/2] Assign label colors automatically --- .../0018_alter_label_background_color.py | 19 +++++++++++++++++++ backend/api/models.py | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 backend/api/migrations/0018_alter_label_background_color.py diff --git a/backend/api/migrations/0018_alter_label_background_color.py b/backend/api/migrations/0018_alter_label_background_color.py new file mode 100644 index 00000000..ca7ec81e --- /dev/null +++ b/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), + ), + ] diff --git a/backend/api/models.py b/backend/api/models.py index 7b1660e6..f5490562 100644 --- a/backend/api/models.py +++ b/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) From 4a2f680c3f70158159a0ff5e9f6fdd3c964ba8c8 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 17 Nov 2021 15:12:40 +0900 Subject: [PATCH 2/2] Add a test case for generating color function --- backend/api/tests/test_models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/api/tests/test_models.py b/backend/api/tests/test_models.py index fee0c46d..c7ac049a 100644 --- a/backend/api/tests/test_models.py +++ b/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)