mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2025-04-27 10:11:24 +08:00
Add imports for equipments from csv
This commit is contained in:
parent
6f5911144c
commit
062b017e16
9 changed files with 508 additions and 46 deletions
|
@ -3,6 +3,9 @@ from django.utils.timezone import now
|
|||
from simple_history.models import HistoricalRecords
|
||||
from django.db.models.signals import post_migrate
|
||||
from django.dispatch import receiver
|
||||
from config import settings
|
||||
import csv
|
||||
import os
|
||||
|
||||
|
||||
class Equipment(models.Model):
|
||||
|
@ -44,49 +47,63 @@ class EquipmentInstance(models.Model):
|
|||
@receiver(post_migrate)
|
||||
def create_superuser(sender, **kwargs):
|
||||
if sender.name == 'equipments':
|
||||
equipment_data = [
|
||||
{
|
||||
'name': 'Pyrex Beaker',
|
||||
'description': '',
|
||||
'category': 'Glassware',
|
||||
'remarks': 'A beaker for storing fluids'
|
||||
},
|
||||
{
|
||||
'name': 'Bunsen Burner',
|
||||
'description': '',
|
||||
'category': 'Miscellaneous',
|
||||
'remarks': 'A burner for heating things'
|
||||
},
|
||||
{
|
||||
'name': 'Microscope',
|
||||
'description': '',
|
||||
'category': 'Miscellaneous',
|
||||
'remarks': 'A microscope for zooming into tiny objects'
|
||||
},
|
||||
{
|
||||
'name': 'Petri Dish',
|
||||
'description': '',
|
||||
'category': 'Glassware',
|
||||
'remarks': 'A petri dish'
|
||||
}
|
||||
]
|
||||
root_path = os.path.join(settings.MEDIA_ROOT, 'equipment_records')
|
||||
csv_files = [f for f in os.listdir(root_path) if f.endswith('.csv')]
|
||||
print('Warning: Postmigration script will migrate without checking for existing item instances')
|
||||
for csv_file in csv_files:
|
||||
csv_file_path = os.path.join(root_path, csv_file)
|
||||
filename = os.path.splitext(csv_file)[0]
|
||||
print('---', 'Adding Equipments from', filename, '---')
|
||||
with open(csv_file_path, newline='') as csvfile:
|
||||
|
||||
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'])
|
||||
print(
|
||||
'Generating 3 Equipment Instances for 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)
|
||||
reader = csv.reader(csvfile)
|
||||
next(reader) # Skip the header row
|
||||
for row in reader:
|
||||
if not any(row):
|
||||
continue
|
||||
|
||||
# Get equipment information
|
||||
category = filename.split('-')[0]
|
||||
name = row[2] + ' ' + row[1]
|
||||
# If quantity is missing, set value as 0
|
||||
if (row[3] == ''):
|
||||
available = 0
|
||||
else:
|
||||
available = int(row[3])
|
||||
# If quantity of broken instances is missing, set value as 0
|
||||
if (row[4] == ''):
|
||||
broken = 0
|
||||
else:
|
||||
available = available - int(row[4])
|
||||
broken = int(row[4])
|
||||
|
||||
def create_instances(a, b, c):
|
||||
print('Adding', a, 'number of working', c.name)
|
||||
# Add working equipments
|
||||
if (a >= 1):
|
||||
for i in range(a):
|
||||
EquipmentInstance.objects.create(
|
||||
equipment=c, status='Available')
|
||||
|
||||
if (b >= 1):
|
||||
print('Adding', a, 'number of broken', c.name)
|
||||
# Add broken equipments
|
||||
for i in range(b):
|
||||
EquipmentInstance.objects.create(
|
||||
equipment=c, status='Broken')
|
||||
|
||||
EQUIPMENT = Equipment.objects.filter(
|
||||
name=name, category=category).first()
|
||||
|
||||
# Check if equipment exists
|
||||
if (EQUIPMENT):
|
||||
# If so, add equipment instances
|
||||
create_instances(available, broken, EQUIPMENT)
|
||||
|
||||
# If not, create equipment
|
||||
else:
|
||||
# Create the equipment first
|
||||
EQUIPMENT = Equipment.objects.create(
|
||||
name=name, category=category)
|
||||
# Then create the instances
|
||||
create_instances(available, broken, EQUIPMENT)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue