This commit is contained in:
Prince Kurt Laurence 2024-01-07 20:41:42 +08:00
commit e8524e1d45
4 changed files with 20 additions and 5 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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":

View file

@ -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):