forked from alexiusstrauss/pyTransfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
70 lines (53 loc) · 2.45 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
65
66
67
68
69
70
from .models import History, Pessoa
from .serializers import PessoaSerializer, HistorySerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status, generics
from django.core.exceptions import ObjectDoesNotExist
class PessoaListCreate(generics.ListCreateAPIView):
queryset = Pessoa.objects.all()
serializer_class = PessoaSerializer
class PessoaRetrive(generics.RetrieveUpdateDestroyAPIView):
queryset = Pessoa.objects.all()
serializer_class = PessoaSerializer
# desativa pessoa no banco
def perform_destroy(self, instance):
instance.ativo = False
instance.save()
class HistorysAPIView(generics.ListAPIView):
serializer_class = HistorySerializer
def get_queryset(self):
queryset = History.objects.all()
parametros = self.request.query_params # busca a lista de argumentos GET
# filtra todo historico por pessoa "Remetente"
if self.kwargs.get('pessoa_pk'):
queryset = self.queryset.filter(
remetente_id=self.kwargs.get('pessoa_pk'))
# filtra entre datas passadas como parametros na url.
if parametros.get('start', None) is not None and parametros.get('end', None) is not None:
queryset = queryset.filter(created_at__range=(
parametros.get('start'), parametros.get('end')))
# filtra por cpf do destinatario nos registros de transaçoes.
if parametros.get('cpf'):
try:
loDestino = Pessoa.objects.filter(
cpf=parametros.get('cpf')).last()
if loDestino is not None:
queryset = queryset.filter(destino_id=loDestino.pk)
except ObjectDoesNotExist:
pass
return queryset
class HistoryListAndCreate(APIView):
def get(self, request):
history = History.objects.all()
serializer = HistorySerializer(history, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request):
serializer = HistorySerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class HistoryListCreate(generics.ListCreateAPIView):
queryset = History.objects.all()
serializer_class = HistorySerializer