mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2024-11-17 06:19:26 +08:00
125 lines
5 KiB
Python
125 lines
5 KiB
Python
from django.db import models
|
|
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
|
|
from django.core.cache import cache
|
|
import csv
|
|
import os
|
|
|
|
|
|
class Equipment(models.Model):
|
|
EQUIPMENT_CATEGORY_CHOICES = (
|
|
('Glassware', 'Glassware'),
|
|
('Miscellaneous', 'Miscellaneous')
|
|
)
|
|
name = models.CharField(max_length=100)
|
|
category = models.CharField(
|
|
max_length=20, choices=EQUIPMENT_CATEGORY_CHOICES, default='Miscellaneous')
|
|
description = models.TextField(max_length=512, null=True)
|
|
date_added = models.DateTimeField(default=now, editable=False)
|
|
last_updated = models.DateTimeField(auto_now=True, editable=False)
|
|
history = HistoricalRecords()
|
|
|
|
def __str__(self):
|
|
return f'{self.name}'
|
|
|
|
def save(self, *args, **kwargs):
|
|
cache.delete('equipments')
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
|
class EquipmentInstance(models.Model):
|
|
EQUIPMENT_INSTANCE_STATUS_CHOICES = (
|
|
('Available', 'Available'),
|
|
('Pending', 'Pending'),
|
|
('Broken', 'Broken'),
|
|
('Borrowed', 'Borrowed'),
|
|
)
|
|
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
|
|
status = models.CharField(
|
|
max_length=20, choices=EQUIPMENT_INSTANCE_STATUS_CHOICES, default='Available', db_index=True)
|
|
remarks = models.TextField(max_length=512, null=True)
|
|
date_added = models.DateTimeField(default=now, editable=False)
|
|
last_updated = models.DateTimeField(auto_now=True, editable=False)
|
|
history = HistoricalRecords()
|
|
|
|
def __str__(self):
|
|
return f'{self.equipment.name}'
|
|
|
|
def save(self, *args, **kwargs):
|
|
cache.delete('available_equipment_instances')
|
|
cache.delete('equipment_instances')
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
|
seed_database = True
|
|
|
|
|
|
@receiver(post_migrate)
|
|
def create_superuser(sender, **kwargs):
|
|
if sender.name == 'equipments':
|
|
if seed_database:
|
|
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:
|
|
|
|
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[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', b, '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)
|