From 9d2f777ffc5c1e2f5aa215a4de5496057a35889e Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 22 Dec 2023 15:50:22 +0800 Subject: [PATCH] Add viewsets for listing teachers and technicians --- equipment_tracker/accounts/serializers.py | 3 +- equipment_tracker/accounts/urls.py | 6 ++-- equipment_tracker/accounts/views.py | 30 +++++++++++++++++-- equipment_tracker/schema.yml | 35 +++++++++++++++++++++++ 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/equipment_tracker/accounts/serializers.py b/equipment_tracker/accounts/serializers.py index 00e937c..f2a6410 100644 --- a/equipment_tracker/accounts/serializers.py +++ b/equipment_tracker/accounts/serializers.py @@ -18,7 +18,7 @@ class CustomUserSerializer(BaseUserSerializer): class Meta(BaseUserSerializer.Meta): model = CustomUser - fields = ('username', 'email', 'avatar', 'first_name', + fields = ('id', 'username', 'email', 'avatar', 'first_name', 'last_name', 'is_teacher', 'is_technician') @@ -56,3 +56,4 @@ class UserRegistrationSerializer(serializers.ModelSerializer): class ClearanceSerializer(serializers.Serializer): cleared = serializers.CharField() + uncleared_transactions = serializers.IntegerField() diff --git a/equipment_tracker/accounts/urls.py b/equipment_tracker/accounts/urls.py index 2b26513..0473322 100644 --- a/equipment_tracker/accounts/urls.py +++ b/equipment_tracker/accounts/urls.py @@ -1,8 +1,10 @@ from django.contrib import admin from django.urls import path, include -from accounts.views import ClearanceViewSet +from accounts import views urlpatterns = [ path('', include('djoser.urls')), path('', include('djoser.urls.jwt')), - path('clearance/', ClearanceViewSet.as_view()), + path('clearance/', views.ClearanceViewSet.as_view()), + path('teachers/', views.TeacherViewSet.as_view()), + path('technicians/', views.TechnicianViewSet.as_view()), ] diff --git a/equipment_tracker/accounts/views.py b/equipment_tracker/accounts/views.py index 829e300..8fdae31 100644 --- a/equipment_tracker/accounts/views.py +++ b/equipment_tracker/accounts/views.py @@ -2,8 +2,9 @@ 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 accounts.serializers import ClearanceSerializer, CustomUserSerializer from rest_framework.response import Response +from accounts.models import CustomUser class ClearanceViewSet(generics.GenericAPIView): @@ -20,7 +21,30 @@ class ClearanceViewSet(generics.GenericAPIView): 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: - return Response({"cleared": "Uncleared"}) - return Response({"cleared": "Cleared"}) + 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) diff --git a/equipment_tracker/schema.yml b/equipment_tracker/schema.yml index 66a2f3b..b5fd661 100644 --- a/equipment_tracker/schema.yml +++ b/equipment_tracker/schema.yml @@ -99,6 +99,38 @@ paths: schema: $ref: '#/components/schemas/TokenVerify' description: '' + /api/v1/accounts/teachers/: + get: + operationId: api_v1_accounts_teachers_list + tags: + - api + security: + - jwtAuth: [] + responses: + '200': + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/CustomUser' + description: '' + /api/v1/accounts/technicians/: + get: + operationId: api_v1_accounts_technicians_list + tags: + - api + security: + - jwtAuth: [] + responses: + '200': + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/CustomUser' + description: '' /api/v1/accounts/users/: get: operationId: api_v1_accounts_users_list @@ -1147,8 +1179,11 @@ components: properties: cleared: type: string + uncleared_transactions: + type: integer required: - cleared + - uncleared_transactions CustomUser: type: object properties: