Skip to content

Commit

Permalink
chore: use TextChoices for color choices
Browse files Browse the repository at this point in the history
This makes it behave more standard and allows to expose this nicely in
spectator.
  • Loading branch information
nijel committed Oct 1, 2024
1 parent 305f601 commit bd19dcb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
3 changes: 3 additions & 0 deletions weblate/settings_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,9 @@
The OpenAPI specification is available as feature preview, feedback welcome!
""",
"VERSION": None,
"ENUM_NAME_OVERRIDES": {
"ColorEnum": "weblate.utils.colors.ColorChoices.choices",
},
}

# Fonts CDN URL
Expand Down
3 changes: 3 additions & 0 deletions weblate/settings_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@
The OpenAPI specification is available as feature preview, feedback welcome!
""",
"VERSION": None,
"ENUM_NAME_OVERRIDES": {
"ColorEnum": "weblate.utils.colors.ColorChoices.choices",
},
}

# Fonts CDN URL
Expand Down
6 changes: 3 additions & 3 deletions weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
)
from weblate.utils import messages
from weblate.utils.celery import get_task_progress
from weblate.utils.colors import COLOR_CHOICES
from weblate.utils.colors import ColorChoices
from weblate.utils.decorators import disable_for_loaddata
from weblate.utils.errors import report_error
from weblate.utils.fields import EmailField
Expand Down Expand Up @@ -762,9 +762,9 @@ class Component(models.Model, PathMixin, CacheKeyMixin, ComponentCategoryMixin):
glossary_color = models.CharField(
verbose_name=gettext_lazy("Glossary color"),
max_length=30,
choices=COLOR_CHOICES,
choices=ColorChoices.choices,
blank=False,
default="silver",
default=ColorChoices.SILVER,
)
remote_revision = models.CharField(max_length=200, default="", blank=True)
local_revision = models.CharField(max_length=200, default="", blank=True)
Expand Down
4 changes: 2 additions & 2 deletions weblate/trans/models/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils.translation import gettext_lazy

from weblate.checks.flags import Flags
from weblate.utils.colors import COLOR_CHOICES
from weblate.utils.colors import ColorChoices

TRANSLATION_LABELS = {"Automatically translated"}

Expand All @@ -20,7 +20,7 @@ class Label(models.Model):
color = models.CharField(
verbose_name=gettext_lazy("Color"),
max_length=30,
choices=COLOR_CHOICES,
choices=ColorChoices.choices,
blank=False,
default=None,
)
Expand Down
37 changes: 19 additions & 18 deletions weblate/utils/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from django.db.models import TextChoices
from django.utils.translation import gettext_lazy

COLOR_CHOICES = (

class ColorChoices(TextChoices):
# Translators: Name of a color
("navy", gettext_lazy("Navy")),
NAVY = "navy", gettext_lazy("Navy")
# Translators: Name of a color
("blue", gettext_lazy("Blue")),
BLUE = "blue", gettext_lazy("Blue")
# Translators: Name of a color
("aqua", gettext_lazy("Aqua")),
AQUA = "aqua", gettext_lazy("Aqua")
# Translators: Name of a color
("teal", gettext_lazy("Teal")),
TEAL = "teal", gettext_lazy("Teal")
# Translators: Name of a color
("olive", gettext_lazy("Olive")),
OLIVE = "olive", gettext_lazy("Olive")
# Translators: Name of a color
("green", gettext_lazy("Green")),
GREEN = "green", gettext_lazy("Green")
# Translators: Name of a color
("lime", gettext_lazy("Lime")),
LIME = "lime", gettext_lazy("Lime")
# Translators: Name of a color
("yellow", gettext_lazy("Yellow")),
YELLOW = "yellow", gettext_lazy("Yellow")
# Translators: Name of a color
("orange", gettext_lazy("Orange")),
ORANGE = "orange", gettext_lazy("Orange")
# Translators: Name of a color
("red", gettext_lazy("Red")),
RED = "red", gettext_lazy("Red")
# Translators: Name of a color
("maroon", gettext_lazy("Maroon")),
MAROON = "maroon", gettext_lazy("Maroon")
# Translators: Name of a color
("fuchsia", gettext_lazy("Fuchsia")),
FUCHSIA = "fuchsia", gettext_lazy("Fuchsia")
# Translators: Name of a color
("purple", gettext_lazy("Purple")),
PURPLE = "purple", gettext_lazy("Purple")
# Translators: Name of a color
("black", gettext_lazy("Black")),
BLACK = "black", gettext_lazy("Black")
# Translators: Name of a color
("gray", gettext_lazy("Gray")),
GRAY = "gray", gettext_lazy("Gray")
# Translators: Name of a color
("silver", gettext_lazy("Silver")),
)
SILVER = "silver", gettext_lazy("Silver")

0 comments on commit bd19dcb

Please sign in to comment.