Skip to content

Commit

Permalink
livery: remove confusing 'css' field, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jclgoodwin committed Dec 23, 2024
1 parent 29911f7 commit 769cafe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 5.1.4 on 2024-12-23 03:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('vehicles', '0009_remove_vehiclerevision_unique_pending_operator_and_more'),
]

operations = [
migrations.RemoveField(
model_name='historicallivery',
name='css',
),
migrations.RemoveField(
model_name='livery',
name='css',
),
migrations.AlterField(
model_name='historicallivery',
name='colours',
field=models.CharField(blank=True, help_text='Left and right CSS will be generated from this', max_length=512),
),
migrations.AlterField(
model_name='historicallivery',
name='right_css',
field=models.CharField(blank=True, help_text='Should be a mirror image of the left CSS', max_length=1024, verbose_name='Right CSS'),
),
migrations.AlterField(
model_name='livery',
name='colours',
field=models.CharField(blank=True, help_text='Left and right CSS will be generated from this', max_length=512),
),
migrations.AlterField(
model_name='livery',
name='right_css',
field=models.CharField(blank=True, help_text='Should be a mirror image of the left CSS', max_length=1024, verbose_name='Right CSS'),
),
migrations.AlterField(
model_name='vehicletype',
name='style',
field=models.CharField(blank=True, choices=[('double decker', 'double decker'), ('minibus', 'minibus'), ('coach', 'coach'), ('articulated', 'articulated'), ('train', 'train'), ('tram', 'tram'), ('amphibious', 'amphibious')], max_length=13),
),
]
24 changes: 3 additions & 21 deletions vehicles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class VehicleTypeType(models.TextChoices):
ARTICULATED = "articulated", "articulated"
TRAIN = "train", "train"
TRAM = "tram", "tram"
AMPHIBIOUS = "amphibious", "amphibious"


class FuelType(models.TextChoices):
Expand All @@ -106,9 +107,6 @@ class FuelType(models.TextChoices):

class VehicleType(models.Model):
name = models.CharField(max_length=255, unique=True)
# double_decker = models.BooleanField(null=True)
# coach = models.BooleanField(null=True)
# electric = models.BooleanField(null=True)
style = models.CharField(choices=VehicleTypeType.choices, max_length=13, blank=True)
fuel = models.CharField(choices=FuelType.choices, max_length=8, blank=True)

Expand All @@ -130,13 +128,6 @@ class Livery(models.Model):
help_text="""Left and right CSS will be generated from this""",
)
angle = models.PositiveSmallIntegerField(null=True, blank=True)
css = models.CharField(
max_length=1024,
blank=True,
verbose_name="CSS",
help_text="""Leave this blank.
A livery can be adequately represented with a list of colours and an angle.""",
)
left_css = models.CharField(
max_length=1024,
blank=True,
Expand Down Expand Up @@ -181,16 +172,7 @@ def minify(css):
return css[19:-1]

def set_css(self):
if self.css:
css = self.css
self.left_css = self.css
for angle in re.findall(r"\((\d+)deg,", css):
replacement = 360 - int(angle)
css = css.replace(f"({angle}deg,", f"({replacement}deg,", 1)
# doesn't work with e.g. angles {a, b} where a = 360 - b
self.right_css = css.replace("left", "right")

elif self.colours:
if self.colours:
self.left_css = get_css(
self.colours.split(), None, self.horizontal, self.angle
)
Expand Down Expand Up @@ -235,7 +217,7 @@ def clean(self):
def save(self, *args, update_fields=None, **kwargs):
self.updated_at = timezone.now()
if update_fields is None:
if self.css or self.colours:
if self.colours:
self.set_css()
if self.colours and not self.id:
self.white_text = get_text_colour(self.colours) == "#fff"
Expand Down
12 changes: 0 additions & 12 deletions vehicles/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,6 @@ def test_livery(self):
vehicle.livery.colours = "#c0c0c0"
vehicle.livery.save()
self.assertEqual("silver", vehicle.get_livery(200))
# self.assertEqual("#c0c0c0", vehicle.get_livery(200))

livery.css = "linear-gradient(45deg,#ed1b23 35%,#fff 35%,#fff 45%,#ed1b23 45%)"
livery.set_css()
self.assertEqual(
livery.left_css,
"linear-gradient(45deg,#ed1b23 35%,#fff 35%,#fff 45%,#ed1b23 45%)",
)
self.assertEqual(
livery.right_css,
"linear-gradient(315deg,#ed1b23 35%,#fff 35%,#fff 45%,#ed1b23 45%)",
)

def test_livery_validation(self):
livery = Livery()
Expand Down

0 comments on commit 769cafe

Please sign in to comment.