diff --git a/equipment_tracker/config/settings.py b/equipment_tracker/config/settings.py index cc422ea..f3a8477 100644 --- a/equipment_tracker/config/settings.py +++ b/equipment_tracker/config/settings.py @@ -71,6 +71,7 @@ INSTALLED_APPS = [ 'drf_spectacular_sidecar', 'accounts', 'equipments', + 'equipment_groups', ] MIDDLEWARE = [ diff --git a/equipment_tracker/equipment_groups/__init__.py b/equipment_tracker/equipment_groups/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/equipment_tracker/equipment_groups/admin.py b/equipment_tracker/equipment_groups/admin.py new file mode 100644 index 0000000..da70e13 --- /dev/null +++ b/equipment_tracker/equipment_groups/admin.py @@ -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') diff --git a/equipment_tracker/equipment_groups/apps.py b/equipment_tracker/equipment_groups/apps.py new file mode 100644 index 0000000..3992bc6 --- /dev/null +++ b/equipment_tracker/equipment_groups/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EquipmentGroupsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'equipment_groups' diff --git a/equipment_tracker/equipment_groups/migrations/0001_initial.py b/equipment_tracker/equipment_groups/migrations/0001_initial.py new file mode 100644 index 0000000..dfa74f7 --- /dev/null +++ b/equipment_tracker/equipment_groups/migrations/0001_initial.py @@ -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')), + ], + ), + ] diff --git a/equipment_tracker/equipment_groups/migrations/__init__.py b/equipment_tracker/equipment_groups/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/equipment_tracker/equipment_groups/models.py b/equipment_tracker/equipment_groups/models.py new file mode 100644 index 0000000..87cde52 --- /dev/null +++ b/equipment_tracker/equipment_groups/models.py @@ -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]) diff --git a/equipment_tracker/equipment_groups/tests.py b/equipment_tracker/equipment_groups/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/equipment_tracker/equipment_groups/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/equipment_tracker/equipment_groups/views.py b/equipment_tracker/equipment_groups/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/equipment_tracker/equipment_groups/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/equipment_tracker/equipments/models.py b/equipment_tracker/equipments/models.py index 937f3be..8c7730f 100644 --- a/equipment_tracker/equipments/models.py +++ b/equipment_tracker/equipments/models.py @@ -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!")