Added equipment groups model and adjusted equipments postmigration script

This commit is contained in:
Keannu Bernasol 2023-11-13 17:52:09 +08:00
parent 0ea1251507
commit 03a4e07f7f
10 changed files with 124 additions and 5 deletions

View file

@ -71,6 +71,7 @@ INSTALLED_APPS = [
'drf_spectacular_sidecar',
'accounts',
'equipments',
'equipment_groups',
]
MIDDLEWARE = [

View file

@ -0,0 +1,9 @@
from django.contrib import admin
from .models import EquipmentGroup
from simple_history.admin import SimpleHistoryAdmin
@admin.register(EquipmentGroup)
class EquipmentGroupAdmin(SimpleHistoryAdmin):
readonly_fields = ('status',)
list_display = ('name', 'status', 'date_added', 'last_updated')

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class EquipmentGroupsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'equipment_groups'

View file

@ -0,0 +1,53 @@
# Generated by Django 4.2.7 on 2023-11-13 09:43
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import simple_history.models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('equipments', '0003_historicalequipment'),
]
operations = [
migrations.CreateModel(
name='HistoricalEquipmentGroup',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('remarks', models.TextField(max_length=512)),
('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('last_updated', models.DateTimeField(blank=True, editable=False)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'historical equipment group',
'verbose_name_plural': 'historical equipment groups',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='EquipmentGroup',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('remarks', models.TextField(max_length=512)),
('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('last_updated', models.DateTimeField(auto_now=True)),
('equipments', models.ManyToManyField(to='equipments.equipmentinstance')),
],
),
]

View file

@ -0,0 +1,40 @@
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 equipments.models import EquipmentInstance
class EquipmentGroup(models.Model):
name = models.CharField(max_length=200)
remarks = models.TextField(max_length=512)
date_added = models.DateTimeField(default=now, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
equipments = models.ManyToManyField(EquipmentInstance)
history = HistoricalRecords()
@property
def status(self):
if self.equipments.filter(status='Broken').exists():
return 'Broken'
elif self.equipments.filter(status='Under Maintenance').exists():
return 'Under Maintenance'
elif self.equipments.filter(status='Decomissioned').exists():
return 'Decomissioned'
else:
return 'Working'
def __str__(self):
return self.name
@receiver(post_migrate)
def create_superuser(sender, **kwargs):
if sender.name == 'equipment_groups':
PC = EquipmentInstance.objects.filter(id=1).first().id
KEYBOARD = EquipmentInstance.objects.filter(id=2).first().id
MOUSE = EquipmentInstance.objects.filter(id=3).first().id
GROUP, CREATED = EquipmentGroup.objects.get_or_create(
name="HP All-In-One PC Set", remarks="First PC set of citc tracker!")
GROUP.equipments.set([PC, KEYBOARD, MOUSE])

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -42,9 +42,13 @@ def create_superuser(sender, **kwargs):
if sender.name == 'equipments':
EQUIPMENT, CREATED = Equipment.objects.get_or_create(
name="HP All-in-One PC", description="I5 6500 8GB RAM 1TB HDD")
if (CREATED):
print("Created sample equipment")
EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create(
equipment=EQUIPMENT, status="Working", remarks="First PC of USTP!")
if (CREATED):
print("Created sample equipment instance")
equipment=EQUIPMENT, status="Working", remarks="First PC of citc equipment tracker!")
EQUIPMENT, CREATED = Equipment.objects.get_or_create(
name="HP Keyboard", description="Generic Membrane Keyboard")
EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create(
equipment=EQUIPMENT, status="Working", remarks="First keyboard of citc equipment tracker!")
EQUIPMENT, CREATED = Equipment.objects.get_or_create(
name="HP Mouse", description="Generic Mouse")
EQUIPMENT_INSTANCE, CREATED = EquipmentInstance.objects.get_or_create(
equipment=EQUIPMENT, status="Working", remarks="First mouse of citc equipment tracker!")