Improved transaction and breakage report serializers when handling breakages

This commit is contained in:
Keannu Christian Bernasol 2023-12-27 20:38:43 +08:00
parent d0ca68149a
commit 0b1c065a80
4 changed files with 47 additions and 40 deletions

View file

@ -4,6 +4,7 @@ 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
from config.settings import DEBUG
# -- Equipment Serializers
@ -46,6 +47,31 @@ class EquipmentSerializer(serializers.HyperlinkedModelSerializer):
if not user.is_technician:
raise exceptions.ValidationError(
"Non-technician users cannot update equipments")
# 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).order_by('id').first()
# If there is one
print('Associated Breakage Report?', bool(associated_breakage_report))
if associated_breakage_report:
# Check if all the equipments of the currently associated BreakageReport are "Available"
all_available = all(
eq.status == 'Available' for eq in associated_breakage_report.equipments.all())
print('All Available?', all_available)
# If all the equipments are "Available", set Breakage Report to be resolved (resolved=True)
# Also set the related Transaction to "Finalized"
if all_available:
associated_breakage_report.resolved = True
associated_breakage_report.save()
print('Breakage Status', associated_breakage_report.resolved)
transaction = associated_breakage_report.transaction
transaction.transaction_status = "Finalized"
transaction.save()
print('Transaction Status',
associated_breakage_report.transaction.transaction_status)
return super().update(instance, validated_data)
# Do not allow users that are not technicians to delete equipments
@ -143,32 +169,41 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
def update(self, instance, validated_data):
user = self.context['request'].user
# Do not allow users that are not technicians to update equipment instances
# Do not allow users that are not technicians to update equipments
if not user.is_technician:
raise exceptions.ValidationError(
"Non-technician users cannot update equipment instances")
# Forbid user from changing equipment field once the instance is already created
# Ignore any changes to 'equipment'
validated_data.pop('equipment', None)
"Non-technician users cannot update equipments")
# 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()
equipments=instance, resolved=False).order_by('id').first()
# If there is one
if (DEBUG):
print('Associated Breakage Report?',
bool(associated_breakage_report))
if associated_breakage_report:
# Check if all the equipments of the currently associated BreakageReport are "Available"
all_available = all(
eq.status == 'Available' for eq in associated_breakage_report.equipments.all())
if (DEBUG):
print('All Available?', all_available)
# If all the equipments are "Available", set Breakage Report to be resolved (resolved=True)
# Also set the related Transaction to "Finalized"
if all_available:
associated_breakage_report.resolved = True
associated_breakage_report.save()
if (DEBUG):
print('Breakage Status', associated_breakage_report.resolved)
transaction = associated_breakage_report.transaction
transaction.transaction_status = "Finalized"
transaction.save()
if (DEBUG):
print('Transaction Status',
associated_breakage_report.transaction.transaction_status)
return instance
return super().update(instance, validated_data)
# Do not allow users that are not technicians to delete equipment instances
def delete(self, instance):

View file

@ -71,7 +71,7 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
"""
# Get all non-finalized transactions
non_finalized_transactions = Transaction.objects.filter(
~Q(transaction_status__in=['Finalized', 'Cancelled']))
~Q(transaction_status__in=['Finalized', 'Rejected', 'Cancelled']))
# Get all equipment instances associated with non-finalized transactions
non_finalized_equipments = EquipmentInstance.objects.filter(