Added breakage report model

This commit is contained in:
Keannu Christian Bernasol 2023-12-09 00:38:29 +08:00
parent aa078a78c5
commit c1a7e21e95
10 changed files with 222 additions and 31 deletions

View file

@ -1,11 +0,0 @@
EQUIPMENT_CATEGORY_CHOICES = (
('Glassware', 'Glassware'),
('Miscellaneous', 'Miscellaneous')
)
EQUIPMENT_INSTANCE_STATUS_CHOICES = (
('Working', 'Working'),
('Broken', 'Broken'),
('Borrowed', 'Borrowed'),
)

View file

@ -3,7 +3,7 @@ from .models import Equipment, EquipmentInstance
from drf_spectacular.utils import extend_schema_field
from drf_spectacular.types import OpenApiTypes
from django.db.models import F
from breakages.models import BreakageReport
# -- Equipment Serializers
@ -147,7 +147,25 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
# Forbid user from changing equipment field once the instance is already created
# Ignore any changes to 'equipment'
validated_data.pop('equipment', None)
return super().update(instance, validated_data)
# This is for Breakage Report handling
# First we update the EquipmentInstance
instance = super().update(instance, validated_data)
# Then we check if the EquipmentInstance has an associated BreakageReport which is still pending
associated_breakage_report = BreakageReport.objects.filter(
equipments=instance, resolved=False).first()
# If there is one
if associated_breakage_report:
# Check if all the equipments of the currently associated BreakageReport are "Working"
all_working = all(
eq.status == 'Working' for eq in associated_breakage_report.equipments.all())
# If all the equipments are "Working", set Breakage Report to be resolved (resolved=True)
if all_working:
associated_breakage_report.resolved = True
associated_breakage_report.save()
return instance
# Do not allow users that are not technicians to delete equipment instances
def delete(self, instance):