diff --git a/equipment_tracker/equipments/views.py b/equipment_tracker/equipments/views.py index d1a03a7..aaf559c 100644 --- a/equipment_tracker/equipments/views.py +++ b/equipment_tracker/equipments/views.py @@ -99,11 +99,12 @@ class AvailableEquipmentInstanceViewSet(generics.ListAPIView): # Get all equipment instances associated with non-finalized transactions non_finalized_equipments = EquipmentInstance.objects.filter( transaction__in=non_finalized_transactions - ).prefetch_related('equipment') + ).order_by('id').prefetch_related('equipment') # Get all equipment instances which are not associated with non-finalized transactions queryset = EquipmentInstance.objects.exclude( - id__in=non_finalized_equipments.values_list('id', flat=True) + id__in=non_finalized_equipments.values_list( + 'id', flat=True) ) cache.set(key, queryset, timeout=60*60*24) diff --git a/equipment_tracker/transactions/models.py b/equipment_tracker/transactions/models.py index dd968de..65f735c 100644 --- a/equipment_tracker/transactions/models.py +++ b/equipment_tracker/transactions/models.py @@ -43,5 +43,7 @@ class Transaction(models.Model): return f"Transaction #{self.id} under {self.teacher} by {self.borrower}" def save(self, *args, **kwargs): + cache.delete('available_equipment_instances') + cache.delete('equipment_instances') cache.delete('non_finalized_transactions') return super().save(*args, **kwargs) diff --git a/equipment_tracker/transactions/serializers.py b/equipment_tracker/transactions/serializers.py index eaad849..ee57a97 100644 --- a/equipment_tracker/transactions/serializers.py +++ b/equipment_tracker/transactions/serializers.py @@ -5,6 +5,7 @@ from .models import Transaction from breakages.models import BreakageReport from accounts.models import CustomUser from config.settings import DEBUG +from django.core.cache import cache class CustomUserSerializer(serializers.ModelSerializer): @@ -128,6 +129,7 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): return transaction def update(self, instance, validated_data): + cache.delete('non_finalized_transactions') user = self.context['request'].user # User Validation @@ -189,7 +191,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): # This updates the status field of all equipment instances in a single query EquipmentInstance.objects.filter( id__in=[equipment.id for equipment in equipments]).update(status='Available') - return super().update(instance, validated_data) + q = super().update(instance, validated_data) + cache.delete('available_equipment_instances') + cache.delete('equipment_instances') + return q # For Approved transactions, # If not changing to Borrowed or Cancelled, throw an error @@ -206,7 +211,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): # This updates the status field of all equipment instances in a single query EquipmentInstance.objects.filter( id__in=[equipment.id for equipment in equipments]).update(status='Available') - return super().update(instance, validated_data) + q = super().update(instance, validated_data) + cache.delete('available_equipment_instances') + cache.delete('equipment_instances') + return q # If there are no issues and a transaction changes from Approved to Borrowed, label the selected equipment's statuses as Borrowed if instance.transaction_status == "Approved" and validated_data.get('transaction_status') == "Borrowed": @@ -248,7 +256,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): # This updates the status field of all equipment instances in a single query EquipmentInstance.objects.filter( id__in=[equipment.id for equipment in equipments]).update(status='Available') - return super().update(instance, validated_data) + q = super().update(instance, validated_data) + cache.delete('available_equipment_instances') + cache.delete('equipment_instances') + return q # If the transaction changes from Returned: Pending Checking to With Breakages, we create a Breakage Report instance if instance.transaction_status == "Returned: Pending Checking" and validated_data.get('transaction_status') == "With Breakages: Pending Resolution": diff --git a/equipment_tracker/transactions/views.py b/equipment_tracker/transactions/views.py index 47514ed..50e5219 100644 --- a/equipment_tracker/transactions/views.py +++ b/equipment_tracker/transactions/views.py @@ -3,6 +3,7 @@ from accounts.permissions import IsTeacher, IsStudent from rest_framework import viewsets, generics from .serializers import TransactionSerializer from .models import Transaction +from django.core.cache import cache class TransactionViewSet(viewsets.ModelViewSet):