Borrowing-TrackerBackend/equipment_tracker/equipments/models.py

92 lines
3.3 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
class Equipment(models.Model):
EQUIPMENT_CATEGORY_CHOICES = (
('Glassware', 'Glassware'),
('Miscellaneous', 'Miscellaneous')
)
name = models.CharField(max_length=40)
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}'
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')
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}'
@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'
}
]
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)