summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <ar@bash.org.pl>2020-03-07 12:34:19 +0100
committerRobert Gerus <ar@bash.org.pl>2020-03-07 15:34:50 +0100
commitd44df4cc455f225a393e47939f9b6a7bc4aebff7 (patch)
tree45eebda7bf96f36c3d231eea00ba5944597ead66
parent7c267dc4be7d2a343703f77b03ca7b548a0e141b (diff)
downloadspejstore-d44df4cc455f225a393e47939f9b6a7bc4aebff7.tar.gz
spejstore-d44df4cc455f225a393e47939f9b6a7bc4aebff7.tar.bz2
spejstore-d44df4cc455f225a393e47939f9b6a7bc4aebff7.zip
Give label styles their own model.
-rw-r--r--storage/admin.py3
-rw-r--r--storage/migrations/0007_label_styles.py70
-rw-r--r--storage/models.py15
-rw-r--r--storage/serializers.py10
4 files changed, 93 insertions, 5 deletions
diff --git a/storage/admin.py b/storage/admin.py
index 144d32c..3c0507f 100644
--- a/storage/admin.py
+++ b/storage/admin.py
@@ -4,7 +4,7 @@ from django.contrib import admin
from django_select2.forms import ModelSelect2Widget, Select2MultipleWidget
-from .models import Item, ItemImage, Category, Label
+from .models import Item, ItemImage, Category, Label, LabelStyle
from .widgets import ItemSelectWidget, PropsSelectWidget
@@ -83,6 +83,7 @@ class ItemAdmin(StaffModelAdmin):
admin.site.register(Item, ItemAdmin)
admin.site.register(Category, StaffModelAdmin)
+admin.site.register(LabelStyle, StaffModelAdmin)
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
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"
+ ),
+ ),
+ ]
diff --git a/storage/models.py b/storage/models.py
index 2333e4e..72f8154 100644
--- a/storage/models.py
+++ b/storage/models.py
@@ -97,12 +97,21 @@ class ItemImage(models.Model):
return '{}'.format(self.image.name)
+class LabelStyle(models.Model):
+ uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+ old_name = models.CharField(max_length=64, editable=False, blank=True)
+ description = models.CharField(max_length=64)
+ length = models.PositiveIntegerField()
+ width = models.PositiveIntegerField()
+
+ def __str__(self):
+ return self.description
+
+
class Label(models.Model):
id = models.CharField(max_length=64, primary_key=True)
item = models.ForeignKey(Item, related_name='labels')
- style = models.CharField(max_length=32, choices=(
- ('basic_99012_v1', 'Basic Dymo 89x36mm label'),
- ), default='basic_99012_v1')
+ style = models.ForeignKey(LabelStyle)
created = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
diff --git a/storage/serializers.py b/storage/serializers.py
index f7f58a8..f554c8f 100644
--- a/storage/serializers.py
+++ b/storage/serializers.py
@@ -1,4 +1,4 @@
-from storage.models import Item, Label
+from storage.models import Item, Label, LabelStyle
from rest_framework import serializers
from rest_framework_hstore.serializers import HStoreSerializer
@@ -8,9 +8,17 @@ class ItemSerializer(HStoreSerializer):
model = Item
fields = ('uuid', 'name', 'description', 'props', 'state', 'parent')
+
+class LabelStyleSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = LabelStyle
+ fields = ('uuid', 'description', 'length', 'width')
+
+
class LabelSerializer(serializers.ModelSerializer):
item = ItemSerializer(required=False)
item_id = serializers.PrimaryKeyRelatedField(queryset=Item.objects, source='item')
+ style = LabelStyleSerializer(required=True)
class Meta:
model = Label
fields = ('id', 'item', 'item_id', 'style')