2023-11-12 21:45:39 +08:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from rest_framework import viewsets, generics
|
|
|
|
from .models import Equipment, EquipmentInstance
|
2023-12-27 18:36:52 +08:00
|
|
|
from django.db.models import Q
|
2023-11-12 21:45:39 +08:00
|
|
|
from . import serializers
|
|
|
|
from config.settings import DEBUG
|
2023-11-13 18:22:53 +08:00
|
|
|
from accounts.permissions import IsTechnician
|
2023-12-27 18:36:52 +08:00
|
|
|
from transactions.models import Transaction
|
2023-11-12 21:45:39 +08:00
|
|
|
|
|
|
|
# -- Equipment Viewsets
|
|
|
|
|
|
|
|
|
|
|
|
class EquipmentViewSet(viewsets.ModelViewSet):
|
|
|
|
if (not DEBUG):
|
2023-12-08 23:07:08 +08:00
|
|
|
permission_classes = [IsAuthenticated]
|
2023-11-12 21:45:39 +08:00
|
|
|
serializer_class = serializers.EquipmentSerializer
|
2023-12-02 22:08:36 +08:00
|
|
|
queryset = Equipment.objects.all().order_by('id')
|
2023-11-12 21:45:39 +08:00
|
|
|
|
|
|
|
# For viewing all logs for all equipments
|
|
|
|
|
|
|
|
|
|
|
|
class EquipmentsLogsViewSet(generics.ListAPIView):
|
|
|
|
if (not DEBUG):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-12-08 23:07:08 +08:00
|
|
|
permission_classes = [IsAuthenticated]
|
2023-11-12 21:45:39 +08:00
|
|
|
serializer_class = serializers.EquipmentInstanceSerializer
|
2023-12-02 22:08:36 +08:00
|
|
|
queryset = EquipmentInstance.objects.all().order_by('id')
|
2023-11-12 21:45:39 +08:00
|
|
|
|
2023-12-27 18:36:52 +08:00
|
|
|
|
|
|
|
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(
|
2023-12-27 20:38:43 +08:00
|
|
|
~Q(transaction_status__in=['Finalized', 'Rejected', 'Cancelled']))
|
2023-12-27 18:36:52 +08:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2023-11-12 21:45:39 +08:00
|
|
|
# For viewing all equipment instance logs
|
|
|
|
|
|
|
|
|
|
|
|
class EquipmentInstancesLogsViewSet(generics.ListAPIView):
|
|
|
|
if (not DEBUG):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
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):
|
2023-11-13 18:22:53 +08:00
|
|
|
permission_classes = [IsAuthenticated, IsTechnician]
|
2023-11-12 21:45:39 +08:00
|
|
|
serializer_class = serializers.EquipmentInstanceSerializer
|
|
|
|
queryset = EquipmentInstance.objects.all().order_by('-date_added')
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset()[:1]
|