2023-12-08 23:00:15 +08:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-12-21 14:57:42 +08:00
|
|
|
from accounts.permissions import IsTeacher, IsStudent
|
2023-12-08 23:00:15 +08:00
|
|
|
from rest_framework import viewsets, generics
|
|
|
|
from .serializers import TransactionSerializer
|
|
|
|
from .models import Transaction
|
2024-01-07 11:02:16 +08:00
|
|
|
from django.core.cache import cache
|
2023-12-08 23:00:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TransactionViewSet(viewsets.ModelViewSet):
|
|
|
|
# Only allow GET, POST/CREATE
|
|
|
|
# Transactions cannot be deleted
|
2023-12-16 15:00:13 +08:00
|
|
|
http_method_names = ['get', 'post', 'patch']
|
2023-12-14 19:28:10 +08:00
|
|
|
permission_classes = [IsAuthenticated]
|
2023-12-08 23:00:15 +08:00
|
|
|
serializer_class = TransactionSerializer
|
|
|
|
queryset = Transaction.objects.all()
|
2023-12-21 14:57:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TransactionByStudentViewSet(generics.ListAPIView):
|
2023-12-22 14:12:10 +08:00
|
|
|
# Viewset for GET only
|
2023-12-21 14:57:42 +08:00
|
|
|
http_method_names = ['get']
|
|
|
|
permission_classes = [IsAuthenticated, IsStudent]
|
|
|
|
serializer_class = TransactionSerializer
|
|
|
|
queryset = Transaction.objects.all()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
transactions = Transaction.objects.filter(borrower=user)
|
|
|
|
return transactions
|
|
|
|
|
|
|
|
|
|
|
|
class TransactionByTeacherViewSet(generics.ListAPIView):
|
2023-12-22 14:12:10 +08:00
|
|
|
# Viewset for GET only
|
2023-12-21 14:57:42 +08:00
|
|
|
http_method_names = ['get']
|
|
|
|
permission_classes = [IsAuthenticated, IsTeacher]
|
|
|
|
serializer_class = TransactionSerializer
|
|
|
|
queryset = Transaction.objects.all()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
transactions = Transaction.objects.filter(teacher=user)
|
|
|
|
return transactions
|