From 6d25157c5f4b95fcc940c99d54fdca9aba669a12 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 26 Dec 2023 19:47:51 +0800 Subject: [PATCH] Improved postmigration data seeding for equipments --- equipment_tracker/equipments/models.py | 50 +++++++++++++++++++------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/equipment_tracker/equipments/models.py b/equipment_tracker/equipments/models.py index 7b57265..7c4119e 100644 --- a/equipment_tracker/equipments/models.py +++ b/equipment_tracker/equipments/models.py @@ -43,15 +43,41 @@ class EquipmentInstance(models.Model): @receiver(post_migrate) def create_superuser(sender, **kwargs): if sender.name == 'equipments': - EQUIPMENT, CREATED = Equipment.objects.get_or_create( - name="Pyrex Beaker", description="", category="Glassware") - EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create( - equipment=EQUIPMENT, status="Available", remarks="First beaker of equipment tracker!") - EQUIPMENT, CREATED = Equipment.objects.get_or_create( - name="Bunsen Burner", description="", category="Miscellaneous") - EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create( - equipment=EQUIPMENT, status="Available", remarks="First bunsen burner of equipment tracker!") - EQUIPMENT, CREATED = Equipment.objects.get_or_create( - name="Microscope", description="", category="Miscellaneous") - EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create( - equipment=EQUIPMENT, status="Available", remarks="First microscope of equipment tracker!") + equipment_data = [ + { + 'name': 'Pyrex Beaker', + 'description': '', + 'category': 'Glassware', + 'remarks': 'First beaker of equipment tracker!' + }, + { + 'name': 'Bunsen Burner', + 'description': '', + 'category': 'Miscellaneous', + 'remarks': 'First bunsen burner of equipment tracker!' + }, + { + 'name': 'Microscope', + 'description': '', + 'category': 'Miscellaneous', + 'remarks': 'First microscope of equipment tracker!' + } + ] + + for data in equipment_data: + EQUIPMENT, CREATED = Equipment.objects.get_or_create( + name=data['name'], + description=data['description'], + category=data['category'] + ) + if (CREATED): + print('Created Equipment: ' + data['name']) + # Generate 3 equipment instances per SKU + for x in range(3): + EQUIPMENT_INSTANCE = EquipmentInstance.objects.create( + equipment=EQUIPMENT, + status='Available', + remarks=data['remarks'] + ) + print('Created Equipment Instances: ' + + EQUIPMENT_INSTANCE.equipment.name)