Borrowing-TrackerBackend/equipment_tracker/accounts/views.py

50 lines
1.9 KiB
Python

from transactions.models import Transaction
from accounts.permissions import IsStudent
from rest_framework.permissions import IsAuthenticated
from rest_framework import generics
from accounts.serializers import ClearanceSerializer, CustomUserSerializer
from rest_framework.response import Response
from accounts.models import CustomUser
class ClearanceViewSet(generics.GenericAPIView):
# Check all transactions associated with the student. If any are uncleared or not finalized, return uncleared
http_method_names = ['get']
permission_classes = [IsAuthenticated, IsStudent]
serializer_class = ClearanceSerializer
def get_queryset(self):
user = self.request.user
return Transaction.objects.filter(borrower=user)
def get(self, request, *args, **kwargs):
queryset = self.get_queryset()
status_list = ["Approved", "Borrowed", "Returned: Pending Checking",
"With Breakages: Pending Resolution"]
uncleared_transactions = 0
for transaction in queryset:
if transaction.transaction_status in status_list:
uncleared_transactions += 1
if uncleared_transactions > 0:
return Response({"cleared": "Uncleared", "uncleared_transactions": uncleared_transactions})
return Response({"cleared": "Cleared", "uncleared_transactions": 0})
class TeacherViewSet(generics.ListAPIView):
# Returns list of users who are teachers
http_method_names = ['get']
permission_classes = [IsAuthenticated]
serializer_class = CustomUserSerializer
def get_queryset(self):
return CustomUser.objects.filter(is_teacher=True)
class TechnicianViewSet(generics.ListAPIView):
# Returns list of users who are technicians
http_method_names = ['get']
permission_classes = [IsAuthenticated]
serializer_class = CustomUserSerializer
def get_queryset(self):
return CustomUser.objects.filter(is_technician=True)