From 832bb48983f025825051609e9ec539cbfeab755c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 9 Nov 2024 23:51:28 +0100 Subject: [PATCH] Device: safe long type names directly in database --- games/migrations/0039_alter_device_type.py | 18 ++++++++++ games/migrations/0040_migrate_device_types.py | 33 +++++++++++++++++++ games/models.py | 16 ++++----- 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 games/migrations/0039_alter_device_type.py create mode 100644 games/migrations/0040_migrate_device_types.py diff --git a/games/migrations/0039_alter_device_type.py b/games/migrations/0039_alter_device_type.py new file mode 100644 index 0000000..0016ffb --- /dev/null +++ b/games/migrations/0039_alter_device_type.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.2 on 2024-11-09 22:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('games', '0038_alter_purchase_price'), + ] + + operations = [ + migrations.AlterField( + model_name='device', + name='type', + field=models.CharField(choices=[('PC', 'PC'), ('Console', 'Console'), ('Handheld', 'Handheld'), ('Mobile', 'Mobile'), ('Single-board computer', 'Single-board computer'), ('Unknown', 'Unknown')], default='Unknown', max_length=255), + ), + ] diff --git a/games/migrations/0040_migrate_device_types.py b/games/migrations/0040_migrate_device_types.py new file mode 100644 index 0000000..37a5667 --- /dev/null +++ b/games/migrations/0040_migrate_device_types.py @@ -0,0 +1,33 @@ +# Generated by Django 5.1.2 on 2024-11-09 22:39 + +from django.db import migrations + + +def update_device_types(apps, schema_editor): + Device = apps.get_model("games", "Device") + + # Mapping of short names to long names + type_map = { + "pc": "PC", + "co": "Console", + "ha": "Handheld", + "mo": "Mobile", + "sbc": "Single-board computer", + "un": "Unknown", + } + + # Loop through all devices and update the type field + for device in Device.objects.all(): + if device.type in type_map: + device.type = type_map[device.type] + device.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("games", "0039_alter_device_type"), + ] + + operations = [ + migrations.RunPython(update_device_types), + ] diff --git a/games/models.py b/games/models.py index f016585..b863b6a 100644 --- a/games/models.py +++ b/games/models.py @@ -259,12 +259,12 @@ class Session(models.Model): class Device(models.Model): - PC = "pc" - CONSOLE = "co" - HANDHELD = "ha" - MOBILE = "mo" - SBC = "sbc" - UNKNOWN = "un" + PC = "PC" + CONSOLE = "Console" + HANDHELD = "Handheld" + MOBILE = "Mobile" + SBC = "Single-board computer" + UNKNOWN = "Unknown" DEVICE_TYPES = [ (PC, "PC"), (CONSOLE, "Console"), @@ -274,8 +274,8 @@ class Device(models.Model): (UNKNOWN, "Unknown"), ] name = models.CharField(max_length=255) - type = models.CharField(max_length=3, choices=DEVICE_TYPES, default=UNKNOWN) + type = models.CharField(max_length=255, choices=DEVICE_TYPES, default=UNKNOWN) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): - return f"{self.name} ({self.get_type_display()})" + return f"{self.name} ({self.type})"