diff --git a/src/documents/migrations/1013_migrate_tag_colour.py b/src/documents/migrations/1013_migrate_tag_colour.py index 0c1189074..323ff2bfb 100644 --- a/src/documents/migrations/1013_migrate_tag_colour.py +++ b/src/documents/migrations/1013_migrate_tag_colour.py @@ -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( diff --git a/src/documents/models.py b/src/documents/models.py index abbcc124e..3d81efea4 100755 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -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"), diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 689e2fe8d..675bb464a 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -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()