From 372e466744c7543c48098f4e66c726fdc7662203 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 22 Dec 2023 14:12:10 +0800 Subject: [PATCH] Add clearance viewset for student --- equipment_tracker/accounts/serializers.py | 4 ++++ equipment_tracker/accounts/urls.py | 3 ++- equipment_tracker/accounts/views.py | 27 +++++++++++++++++++++-- equipment_tracker/schema.yml | 25 +++++++++++++++++++-- equipment_tracker/transactions/views.py | 6 ++--- 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/equipment_tracker/accounts/serializers.py b/equipment_tracker/accounts/serializers.py index 4c81def..00e937c 100644 --- a/equipment_tracker/accounts/serializers.py +++ b/equipment_tracker/accounts/serializers.py @@ -52,3 +52,7 @@ class UserRegistrationSerializer(serializers.ModelSerializer): user.save() return user + + +class ClearanceSerializer(serializers.Serializer): + cleared = serializers.CharField() diff --git a/equipment_tracker/accounts/urls.py b/equipment_tracker/accounts/urls.py index 22c121e..2b26513 100644 --- a/equipment_tracker/accounts/urls.py +++ b/equipment_tracker/accounts/urls.py @@ -1,7 +1,8 @@ from django.contrib import admin from django.urls import path, include - +from accounts.views import ClearanceViewSet urlpatterns = [ path('', include('djoser.urls')), path('', include('djoser.urls.jwt')), + path('clearance/', ClearanceViewSet.as_view()), ] diff --git a/equipment_tracker/accounts/views.py b/equipment_tracker/accounts/views.py index 91ea44a..829e300 100644 --- a/equipment_tracker/accounts/views.py +++ b/equipment_tracker/accounts/views.py @@ -1,3 +1,26 @@ -from django.shortcuts import render +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 +from rest_framework.response import Response -# Create your views here. + +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"] + for transaction in queryset: + if transaction.transaction_status in status_list: + return Response({"cleared": "Uncleared"}) + return Response({"cleared": "Cleared"}) diff --git a/equipment_tracker/schema.yml b/equipment_tracker/schema.yml index 84cc793..66a2f3b 100644 --- a/equipment_tracker/schema.yml +++ b/equipment_tracker/schema.yml @@ -4,6 +4,20 @@ info: version: 1.0.0 description: A Project paths: + /api/v1/accounts/clearance/: + get: + operationId: api_v1_accounts_clearance_retrieve + tags: + - api + security: + - jwtAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/Clearance' + description: '' /api/v1/accounts/jwt/create/: post: operationId: api_v1_accounts_jwt_create_create @@ -1051,7 +1065,7 @@ paths: schema: $ref: '#/components/schemas/Transaction' description: '' - /api/v1/transactions/student: + /api/v1/transactions/student/: get: operationId: api_v1_transactions_student_list tags: @@ -1067,7 +1081,7 @@ paths: items: $ref: '#/components/schemas/Transaction' description: '' - /api/v1/transactions/teacher: + /api/v1/transactions/teacher/: get: operationId: api_v1_transactions_teacher_list tags: @@ -1128,6 +1142,13 @@ components: description: |- * `Glassware` - Glassware * `Miscellaneous` - Miscellaneous + Clearance: + type: object + properties: + cleared: + type: string + required: + - cleared CustomUser: type: object properties: diff --git a/equipment_tracker/transactions/views.py b/equipment_tracker/transactions/views.py index bade244..47514ed 100644 --- a/equipment_tracker/transactions/views.py +++ b/equipment_tracker/transactions/views.py @@ -15,8 +15,7 @@ class TransactionViewSet(viewsets.ModelViewSet): class TransactionByStudentViewSet(generics.ListAPIView): - # Only allow GET, POST/CREATE - # Transactions cannot be deleted + # Viewset for GET only http_method_names = ['get'] permission_classes = [IsAuthenticated, IsStudent] serializer_class = TransactionSerializer @@ -29,8 +28,7 @@ class TransactionByStudentViewSet(generics.ListAPIView): class TransactionByTeacherViewSet(generics.ListAPIView): - # Only allow GET, POST/CREATE - # Transactions cannot be deleted + # Viewset for GET only http_method_names = ['get'] permission_classes = [IsAuthenticated, IsTeacher] serializer_class = TransactionSerializer