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 # -- Equipment Viewsets class EquipmentViewSet(viewsets.ModelViewSet): if (not DEBUG): permission_classes = [IsAuthenticated] serializer_class = serializers.EquipmentSerializer queryset = Equipment.objects.all().order_by('id') # 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') 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. """ # Get all non-finalized transactions non_finalized_transactions = Transaction.objects.filter( ~Q(transaction_status__in=['Finalized', 'Rejected', 'Cancelled'])) # Get all equipment instances associated with non-finalized transactions non_finalized_equipments = EquipmentInstance.objects.filter( transaction__in=non_finalized_transactions) # 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)).order_by('id') return queryset # 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]