mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2025-04-27 10:11:24 +08:00
Added breakage report model
This commit is contained in:
parent
aa078a78c5
commit
c1a7e21e95
10 changed files with 222 additions and 31 deletions
|
@ -1,11 +0,0 @@
|
|||
EQUIPMENT_CATEGORY_CHOICES = (
|
||||
('Glassware', 'Glassware'),
|
||||
('Miscellaneous', 'Miscellaneous')
|
||||
)
|
||||
|
||||
|
||||
EQUIPMENT_INSTANCE_STATUS_CHOICES = (
|
||||
('Working', 'Working'),
|
||||
('Broken', 'Broken'),
|
||||
('Borrowed', 'Borrowed'),
|
||||
)
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue