This commit is contained in:
jonaswinkler 2021-02-25 22:16:31 +01:00
parent b45630080b
commit 7b1145c75e
3 changed files with 8 additions and 6 deletions

View File

@ -60,7 +60,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='tag',
name='color',
field=models.CharField(blank=True, max_length=7, verbose_name='color'),
field=models.CharField(default='#a6cee3', max_length=7, verbose_name='color'),
),
migrations.RunPython(forward, reverse),
migrations.RemoveField(

View File

@ -79,7 +79,9 @@ class Tag(MatchingModel):
color = models.CharField(
_("color"),
blank=True, max_length=7)
max_length=7,
default="#a6cee3"
)
is_inbox_tag = models.BooleanField(
_("is inbox tag"),

View File

@ -111,7 +111,7 @@ class ColorField(serializers.Field):
for id, color in self.COLOURS:
if id == data:
return color
return "#a6cee3"
raise serializers.ValidationError()
def to_representation(self, value):
for id, color in self.COLOURS:
@ -122,7 +122,7 @@ class ColorField(serializers.Field):
class TagSerializerVersion1(MatchingModelSerializer):
colour = ColorField(source='color')
colour = ColorField(source='color', default="#a6cee3")
class Meta:
model = Tag
@ -142,7 +142,7 @@ class TagSerializerVersion1(MatchingModelSerializer):
class TagSerializer(MatchingModelSerializer):
def get_text_color(self, obj):
if obj.color:
try:
h = obj.color.lstrip('#')
rgb = tuple(int(h[i:i + 2], 16)/256 for i in (0, 2, 4))
luminance = math.sqrt(
@ -151,7 +151,7 @@ class TagSerializer(MatchingModelSerializer):
0.114 * math.pow(rgb[2], 2)
)
return "#ffffff" if luminance < 0.5 else "#000000"
else:
except ValueError:
return "#000000"
text_color = serializers.SerializerMethodField()