Files
Fil-A-Management/filament/migrations/0005_variant_swatch.py
T

47 lines
1.6 KiB
Python

# Generated by Django 5.2.19 on 2026-11-14 13:44
import django.core.validators
from django.db import migrations, models
from filament.bambu_swatches_0005 import BAMBU_SWATCHES
def fill_bambu_swatches(apps, schema_editor):
variant_model = apps.get_model("filament", "Variant")
variants = variant_model.objects.using(schema_editor.connection.alias).filter(
product_type__manufacturer__name="Bambu Lab", swatch=""
)
updates = []
for variant in variants.select_related("product_type", "color"):
colors = BAMBU_SWATCHES.get(variant.product_type.name, {})
if swatch := colors.get(variant.manufacturer_color_code or variant.color.name):
variant.swatch = swatch
updates.append(variant)
variants.bulk_update(updates, ["swatch"])
class Migration(migrations.Migration):
dependencies = [
("filament", "0004_catalogbootstrap"),
]
operations = [
migrations.AddField(
model_name="variant",
name="swatch",
field=models.CharField(
blank=True,
help_text="Ein Hexwert oder zwei durch ein Komma getrennte Hexwerte.",
max_length=15,
validators=[
django.core.validators.RegexValidator(
"\\A#[0-9A-Fa-f]{6}(,#[0-9A-Fa-f]{6})?\\Z",
"Bitte einen oder zwei Hexwerte eingeben (z. B. #DE4343 oder #000000,#A34342).",
)
],
verbose_name="Farbvorschau",
),
),
migrations.RunPython(fill_bambu_swatches, migrations.RunPython.noop),
]