summaryrefslogtreecommitdiffstats
path: root/storage/migrations/0007_label_styles.py
blob: 18176f2b6ce773e403903e808cf78a2246f22ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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"
            ),
        ),
    ]