-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
64 lines (52 loc) · 2.63 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from rest_framework import generics, status
from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .serializers import *
from .models import Transaction
from rest_framework.views import APIView
from rest_framework.response import Response
import json
class TransactionView(generics.ListAPIView):
def get(self, request, format=None):
serializer_class = TransactionSearializer
queryset = Transaction.objects.filter(user=request.user)
dat = []
for i in queryset:
dat.append(TransactionSearializer(i).data)
return Response({'data':dat}, status=status.HTTP_200_OK)
class CreateTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:
user = request.user
name = request.data.get('name')
amount = request.data.get('amount')
category = request.data.get('category')
note = request.data.get('note')
transaction = Transaction(user=user, name=name, amount=amount, category=category, note=note)
transaction.save()
return Response(TransactionSearializer(transaction).data, status=status.HTTP_201_CREATED)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)
class UpdateTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:
id = request.data.get('id')
queryset = Transaction.objects.filter(id=id)
transaction = queryset[0]
transaction.name = request.data.get('name')
transaction.amount = request.data.get('amount')
transaction.category = request.data.get('category')
transaction.note = request.data.get('note')
transaction.save(update_fields=['name','amount','category','note'])
return Response(TransactionSearializer(transaction).data, status=status.HTTP_200_OK)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)
class DeleteTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:
id = request.data.get('id')
queryset = Transaction.objects.filter(id=id)
transaction = queryset[0]
transaction.delete()
return Response({'message':'Deleted Successfully'}, status=status.HTTP_200_OK)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)