Improved transaction and breakage report serializers when handling breakages

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

View file

@ -14,33 +14,3 @@ class BreakageReport(models.Model):
def __str__(self):
return f"Breakage report for transaction #{self.transaction.id} by {self.transaction.borrower} under {self.transaction.teacher}"
def save(self, *args, **kwargs):
# Check if the instance is being updated
if not self._state.adding:
# Check if all associated equipment instances have status "Working"
all_working = all(
eq.status == 'Working' for eq in self.equipments.all())
# If all equipment instances are working
if all_working:
# set resolved field to True
self.resolved = True
# set the status of the associated transaction to "Finalized"
self.transaction.status = 'Finalized'
self.transaction.save()
# Then save the instance again to reflect the changes
super().save(*args, **kwargs)
# If not, set the resolved field to False
else:
if (self.resolved != False or self.transaction.status != 'With Breakages: Pending Resolution'):
# set resolved field to False
self.resolved = False
# set the status of the associated transaction to still be pending
self.transaction.status = 'With Breakages: Pending Resolution'
self.transaction.save()
# Then save the instance again to reflect the changes
super().save(*args, **kwargs)
else:
super().save(*args, **kwargs)

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(

View file

@ -4,6 +4,8 @@ from accounts.models import CustomUser
class TransactionAdmin(admin.ModelAdmin):
list_display = ('id', 'borrower', 'teacher', 'transaction_status')
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "borrower":
kwargs["queryset"] = CustomUser.objects.exclude(