mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2024-11-17 06:19:26 +08:00
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from rest_framework.permissions import IsAuthenticated
|
|
from accounts.permissions import IsTeacher, IsStudent
|
|
from rest_framework import viewsets, generics
|
|
from .serializers import TransactionSerializer
|
|
from .models import Transaction
|
|
from django.core.cache import cache
|
|
|
|
|
|
class TransactionViewSet(viewsets.ModelViewSet):
|
|
# Only allow GET, POST/CREATE
|
|
# Transactions cannot be deleted
|
|
http_method_names = ['get', 'post', 'patch']
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = TransactionSerializer
|
|
queryset = Transaction.objects.all()
|
|
|
|
|
|
class TransactionByStudentViewSet(generics.ListAPIView):
|
|
# Viewset for GET only
|
|
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):
|
|
# Viewset for GET only
|
|
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
|