mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2024-11-17 06:19:26 +08:00
144 lines
4.9 KiB
Python
144 lines
4.9 KiB
Python
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework import viewsets, generics
|
|
from .models import Equipment, EquipmentInstance
|
|
from django.db.models import Q
|
|
from . import serializers
|
|
from config.settings import DEBUG
|
|
from accounts.permissions import IsTechnician
|
|
from transactions.models import Transaction
|
|
from django.core.cache import cache
|
|
# -- Equipment Viewsets
|
|
|
|
|
|
class EquipmentViewSet(viewsets.ModelViewSet):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = serializers.EquipmentSerializer
|
|
queryset = Equipment.objects.all().order_by('id')
|
|
|
|
def get_queryset(self):
|
|
key = 'equipments'
|
|
|
|
queryset = cache.get(key)
|
|
if not queryset:
|
|
queryset = super().get_queryset()
|
|
cache.set(key, queryset, timeout=60*60*24)
|
|
return queryset
|
|
|
|
# For viewing all logs for all equipments
|
|
|
|
|
|
class EquipmentsLogsViewSet(generics.ListAPIView):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentLogsSerializer
|
|
queryset = Equipment.history.all().order_by('-history_date')
|
|
|
|
# For viewing logs per individual equipment
|
|
|
|
|
|
class EquipmentLogViewSet(viewsets.ReadOnlyModelViewSet):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentLogSerializer
|
|
|
|
def get_queryset(self):
|
|
equipment_id = self.kwargs['equipment_id']
|
|
return Equipment.objects.filter(id=equipment_id)
|
|
|
|
# Last changed equipment
|
|
|
|
|
|
class LastUpdatedEquipmentViewSet(generics.ListAPIView):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentSerializer
|
|
queryset = Equipment.objects.all().order_by('-date_added')
|
|
|
|
def get_queryset(self):
|
|
return super().get_queryset()[:1]
|
|
|
|
# -- Equipment Instance Viewsets
|
|
|
|
|
|
class EquipmentInstanceViewSet(viewsets.ModelViewSet):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = serializers.EquipmentInstanceSerializer
|
|
queryset = EquipmentInstance.objects.all().order_by('id')
|
|
|
|
def get_queryset(self):
|
|
key = 'equipment_instances'
|
|
queryset = cache.get(key)
|
|
if not queryset:
|
|
queryset = super().get_queryset()
|
|
cache.set(key, queryset, timeout=60*60*24)
|
|
return queryset
|
|
|
|
|
|
class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = serializers.EquipmentInstanceSerializer
|
|
|
|
def get_queryset(self):
|
|
"""
|
|
This view should return a list of all the equipment instances
|
|
that are not associated with a non-finalized transaction.
|
|
"""
|
|
key = 'available_equipment_instances'
|
|
|
|
queryset = cache.get(key)
|
|
if not queryset:
|
|
# Get all non-finalized transactions
|
|
non_finalized_transactions = Transaction.objects.filter(
|
|
~Q(transaction_status__in=[
|
|
'Finalized', 'Rejected', 'Cancelled'])
|
|
).prefetch_related('equipments')
|
|
|
|
# Get all equipment instances associated with non-finalized transactions
|
|
non_finalized_equipments = EquipmentInstance.objects.filter(
|
|
transaction__in=non_finalized_transactions
|
|
).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)
|
|
)
|
|
cache.set(key, queryset, timeout=60*60*24)
|
|
|
|
return queryset.prefetch_related('equipment')
|
|
|
|
# For viewing all equipment instance logs
|
|
|
|
|
|
class EquipmentInstancesLogsViewSet(generics.ListAPIView):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentInstanceLogsSerializer
|
|
queryset = EquipmentInstance.history.all().order_by('-history_date')
|
|
|
|
# For viewing logs per individual equipment instance
|
|
|
|
|
|
class EquipmentInstanceLogViewSet(viewsets.ReadOnlyModelViewSet):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentInstanceLogSerializer
|
|
|
|
def get_queryset(self):
|
|
equipment_id = self.kwargs['equipment_id']
|
|
return EquipmentInstance.objects.filter(id=equipment_id)
|
|
|
|
# Last changed equipment instance
|
|
|
|
|
|
class LastUpdatedEquipmentInstanceViewSet(generics.ListAPIView):
|
|
if (not DEBUG):
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
|
serializer_class = serializers.EquipmentInstanceSerializer
|
|
queryset = EquipmentInstance.objects.all().order_by('-date_added')
|
|
|
|
def get_queryset(self):
|
|
return super().get_queryset()[:1]
|