From 0b1c065a80de1bb63a67703083c558f0ddb6e0ce Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Wed, 27 Dec 2023 20:38:43 +0800 Subject: [PATCH] Improved transaction and breakage report serializers when handling breakages --- equipment_tracker/breakages/models.py | 30 ------------ equipment_tracker/equipments/serializers.py | 53 +++++++++++++++++---- equipment_tracker/equipments/views.py | 2 +- equipment_tracker/transactions/admin.py | 2 + 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/equipment_tracker/breakages/models.py b/equipment_tracker/breakages/models.py index f05457b..ee448b6 100644 --- a/equipment_tracker/breakages/models.py +++ b/equipment_tracker/breakages/models.py @@ -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) diff --git a/equipment_tracker/equipments/serializers.py b/equipment_tracker/equipments/serializers.py index 4d11259..9b5d73f 100644 --- a/equipment_tracker/equipments/serializers.py +++ b/equipment_tracker/equipments/serializers.py @@ -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): diff --git a/equipment_tracker/equipments/views.py b/equipment_tracker/equipments/views.py index cd30e07..5c4e359 100644 --- a/equipment_tracker/equipments/views.py +++ b/equipment_tracker/equipments/views.py @@ -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( diff --git a/equipment_tracker/transactions/admin.py b/equipment_tracker/transactions/admin.py index cda940a..d038fc8 100644 --- a/equipment_tracker/transactions/admin.py +++ b/equipment_tracker/transactions/admin.py @@ -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(