2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-12-08 23:00:15 +08:00
|
|
|
EQUIPMENT_CATEGORY_CHOICES = (
|
|
|
|
('Glassware', 'Glassware'),
|
|
|
|
('Miscellaneous', 'Miscellaneous')
|
2023-12-02 20:58:55 +08:00
|
|
|
)
|
2023-11-12 21:45:39 +08:00
|
|
|
name = models.CharField(max_length=40)
|
2023-12-08 23:00:15 +08:00
|
|
|
category = models.CharField(
|
|
|
|
max_length=20, choices=EQUIPMENT_CATEGORY_CHOICES, default='Miscellaneous')
|
2023-12-02 22:01:27 +08:00
|
|
|
description = models.TextField(max_length=512, null=True)
|
2023-11-12 21:45:39 +08:00
|
|
|
date_added = models.DateTimeField(default=now, editable=False)
|
|
|
|
last_updated = models.DateTimeField(auto_now=True, editable=False)
|
|
|
|
history = HistoricalRecords()
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-12-27 18:36:52 +08:00
|
|
|
return f'{self.name}'
|
2023-11-12 21:45:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class EquipmentInstance(models.Model):
|
2023-12-08 23:00:15 +08:00
|
|
|
EQUIPMENT_INSTANCE_STATUS_CHOICES = (
|
2023-12-16 15:00:13 +08:00
|
|
|
('Available', 'Available'),
|
2023-12-27 18:36:52 +08:00
|
|
|
('Pending', 'Pending'),
|
2023-12-08 23:00:15 +08:00
|
|
|
('Broken', 'Broken'),
|
|
|
|
('Borrowed', 'Borrowed'),
|
2023-11-12 21:45:39 +08:00
|
|
|
)
|
|
|
|
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
|
|
|
|
status = models.CharField(
|
2023-12-16 15:00:13 +08:00
|
|
|
max_length=20, choices=EQUIPMENT_INSTANCE_STATUS_CHOICES, default='Available')
|
2023-12-02 22:01:27 +08:00
|
|
|
remarks = models.TextField(max_length=512, null=True)
|
2023-11-12 21:45:39 +08:00
|
|
|
date_added = models.DateTimeField(default=now, editable=False)
|
|
|
|
last_updated = models.DateTimeField(auto_now=True, editable=False)
|
|
|
|
history = HistoricalRecords()
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-12-27 18:36:52 +08:00
|
|
|
return f'{self.equipment.name}'
|
2023-11-12 21:45:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_migrate)
|
|
|
|
def create_superuser(sender, **kwargs):
|
|
|
|
if sender.name == 'equipments':
|
2023-12-26 19:47:51 +08:00
|
|
|
equipment_data = [
|
|
|
|
{
|
|
|
|
'name': 'Pyrex Beaker',
|
|
|
|
'description': '',
|
|
|
|
'category': 'Glassware',
|
2023-12-27 18:36:52 +08:00
|
|
|
'remarks': 'A beaker for storing fluids'
|
2023-12-26 19:47:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'Bunsen Burner',
|
|
|
|
'description': '',
|
|
|
|
'category': 'Miscellaneous',
|
2023-12-27 18:36:52 +08:00
|
|
|
'remarks': 'A burner for heating things'
|
2023-12-26 19:47:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'Microscope',
|
|
|
|
'description': '',
|
|
|
|
'category': 'Miscellaneous',
|
2023-12-27 18:36:52 +08:00
|
|
|
'remarks': 'A microscope for zooming into tiny objects'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'Petri Dish',
|
|
|
|
'description': '',
|
|
|
|
'category': 'Glassware',
|
|
|
|
'remarks': 'A petri dish'
|
2023-12-26 19:47:51 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
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'])
|
2023-12-27 18:36:52 +08:00
|
|
|
print(
|
|
|
|
'Generating 3 Equipment Instances for Equipment: ' + data['name'])
|
2023-12-27 16:31:37 +08:00
|
|
|
# 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)
|