Skip to content

Commit

Permalink
add colour(s) and css field validators
Browse files Browse the repository at this point in the history
  • Loading branch information
jclgoodwin committed Jan 19, 2025
1 parent 7b1f6e3 commit e7560ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
27 changes: 24 additions & 3 deletions vehicles/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django import forms
from django.utils.text import normalize_newlines
from django.db.models import CharField
from webcolors import html5_parse_legacy_color


class RegField(forms.CharField):
Expand All @@ -24,13 +25,33 @@ def to_python(self, value):
return value


def validate_colour(value):
if value:
try:
html5_parse_legacy_color(value)
except ValueError as e:
raise forms.ValidationError(str(e))


def validate_colours(value):
for colour in value.split():
validate_colour(colour)


def validate_css(value):
if value.count("(") != value.count(")"):
raise forms.ValidationError("Must contain equal numbers of ( and )")
if "{" in value or "}" in value:
raise forms.ValidationError("Must not contain { or }")


class ColourField(CharField):
pass
default_validators = [validate_colour]


class ColoursField(CharField):
pass
default_validators = [validate_colours]


class CSSField(CharField):
pass
default_validators = [validate_css]
31 changes: 3 additions & 28 deletions vehicles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
from autoslug import AutoSlugField
from django.conf import settings
from django.contrib.gis.db import models
from django.core.exceptions import ValidationError
from django.db.models import Q, UniqueConstraint
from django.db.models.functions import TruncDate, Upper
from django.urls import reverse
from django.utils import timezone
from django.utils.html import escape, format_html
from simple_history.models import HistoricalRecords
from webcolors import html5_parse_simple_color
from webcolors import html5_parse_legacy_color

from busstops.models import DataSource, Operator, Service
from bustimes.utils import get_trip
Expand Down Expand Up @@ -70,10 +69,10 @@ def get_brightness(colour):


def get_text_colour(colours):
if not colours or colours == "Other":
if not colours:
return
colours = colours.split()
colours = [html5_parse_simple_color(colour) for colour in colours]
colours = [html5_parse_legacy_color(colour) for colour in colours]
brightnesses = [get_brightness(colour) for colour in colours]
colours_length = len(colours)
if colours_length > 2:
Expand Down Expand Up @@ -194,24 +193,6 @@ def preview(self, name=False):
else:
return format_html(div + ' title="{}"></div>', self.name)

def clean(self):
Vehicle.clean(self) # validate colours field

for attr in ("colour", "stroke_colour", "text_colour"):
value = getattr(self, attr)
if value:
try:
html5_parse_simple_color(value)
except ValueError as e:
raise ValidationError({attr: str(e)})

for attr in ("left_css", "right_css"):
value = getattr(self, attr)
if value.count("(") != value.count(")"):
raise ValidationError({attr: "Must contain equal numbers of ( and )"})
if "{" in value or "}" in value:
raise ValidationError({attr: "Must not contain { or }"})

def save(self, *args, update_fields=None, **kwargs):
self.updated_at = timezone.now()
if update_fields is None:
Expand Down Expand Up @@ -424,12 +405,6 @@ def get_flickr_link(self):

get_flickr_link.short_description = "Flickr"

def clean(self):
try:
get_text_colour(self.colours)
except ValueError as e:
raise ValidationError({"colours": str(e)})

def get_json(self):
json = {
"url": self.get_absolute_url(),
Expand Down

0 comments on commit e7560ab

Please sign in to comment.