summaryrefslogtreecommitdiffstats
path: root/storage/migrations/0007_label_styles.py
diff options
context:
space:
mode:
Diffstat (limited to 'storage/migrations/0007_label_styles.py')
-rw-r--r--storage/migrations/0007_label_styles.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/storage/migrations/0007_label_styles.py b/storage/migrations/0007_label_styles.py
new file mode 100644
index 0000000..18176f2
--- /dev/null
+++ b/storage/migrations/0007_label_styles.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+import uuid
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+def create_default_label(apps, schema_editor):
+ LabelStyle = apps.get_model("storage", "LabelStyle")
+ LabelStyle.objects.create(
+ old_name="basic_99012_v1",
+ description="Basic Dymo 89x36mm label",
+ length=89,
+ width=36,
+ )
+
+
+def fix_label_styles(apps, schema_editor):
+ Label = apps.get_model("storage", "Label")
+ LabelStyle = apps.get_model("storage", "LabelStyle")
+ style = LabelStyle.objects.get(old_name="basic_99012_v1")
+ for label in Label.objects.all():
+ if label.style == "basic_99012_v1":
+ label.style = style.uuid
+ label.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("storage", "0006_category_icon_id"),
+ ]
+
+ operations = [
+ # First set length to 36, to accomodate for UUIDs
+ migrations.AlterField(
+ model_name="label",
+ name="style",
+ field=models.CharField(max_length=36),
+ ),
+ migrations.CreateModel(
+ name="LabelStyle",
+ fields=[
+ (
+ "uuid",
+ models.UUIDField(
+ primary_key=True,
+ default=uuid.uuid4,
+ editable=False,
+ serialize=False,
+ ),
+ ),
+ ("old_name", models.CharField(max_length=64)),
+ ("description", models.CharField(max_length=64)),
+ ("length", models.PositiveIntegerField()),
+ ("width", models.PositiveIntegerField()),
+ ],
+ ),
+ # Create default label, equivalent to old basic_99012_v1 style
+ migrations.RunPython(create_default_label),
+ migrations.RunPython(fix_label_styles),
+ migrations.AlterField(
+ model_name="label",
+ name="style",
+ field=models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE, to="storage.LabelStyle"
+ ),
+ ),
+ ]