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) 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)