From 01dd0a960dbf15c835461373642867a2577d2287 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 25 Sep 2024 10:45:32 -0300 Subject: [PATCH 1/2] refa: alterando view de decorators para classe --- src/tupan/estacoes/models.py | 2 +- src/tupan/estacoes/urls.py | 14 ++-- src/tupan/estacoes/views.py | 124 +++++++++++++++++++---------------- 3 files changed, 75 insertions(+), 65 deletions(-) diff --git a/src/tupan/estacoes/models.py b/src/tupan/estacoes/models.py index 102850e..3f9bef6 100644 --- a/src/tupan/estacoes/models.py +++ b/src/tupan/estacoes/models.py @@ -36,7 +36,7 @@ def __str__(self): class Estacao(Base): nome = models.CharField(max_length=127) - endereco = models.OneToOneField(Endereco, null=True, on_delete=models.SET_NULL) + endereco = models.OneToOneField(Endereco, on_delete=models.CASCADE) topico = models.CharField(help_text="Tópico do broker MQTT", max_length=127) ativo = models.BooleanField(default=True) parametros = models.ManyToManyField(Parametro, blank=True) diff --git a/src/tupan/estacoes/urls.py b/src/tupan/estacoes/urls.py index 650fd5f..82c612e 100644 --- a/src/tupan/estacoes/urls.py +++ b/src/tupan/estacoes/urls.py @@ -1,11 +1,11 @@ from django.urls import path -from .views import estacao_list, estacao_details, parametro_list, parametro_details, endereco_list, endereco_details +from .views import EstacoesView, EstacoesDetalhesView, EnderecosView, EnderecosDetalhesView, ParametrosView, ParametrosDetalhesView urlpatterns = [ - path("estacoes", estacao_list, name="Estações"), - path("estacoes/", estacao_details, name="Estação"), - path("enderecos", endereco_list, name="Endereços"), - path("enderecos/", endereco_details, name="Endereço"), - path("parametros", parametro_list, name="Parâmetros"), - path("parametros/", parametro_details, name="parâmetro") + path("estacoes", EstacoesView.as_view(), name="Estações"), + path("estacoes/", EstacoesDetalhesView.as_view(), name="Estação"), + path("enderecos", EnderecosView.as_view(), name="Endereços"), + path("enderecos/", EnderecosDetalhesView.as_view(), name="Endereço"), + path("parametros", ParametrosView.as_view(), name="Parâmetros"), + path("parametros/", ParametrosDetalhesView.as_view(), name="Parâmetro") ] \ No newline at end of file diff --git a/src/tupan/estacoes/views.py b/src/tupan/estacoes/views.py index 4bfc62b..26e0bdf 100644 --- a/src/tupan/estacoes/views.py +++ b/src/tupan/estacoes/views.py @@ -2,112 +2,122 @@ from .serializers import EstacaoSerializer, EnderecoSerializer, ParametroSerializer from rest_framework.response import Response from rest_framework import status -from rest_framework.decorators import api_view +from rest_framework.views import APIView -@api_view(['GET', 'POST']) -def estacao_list(request): - if request.method == 'GET': +class EstacoesView(APIView): + def get(self, request, *args, **kwargs): + ativo = request.query_params.get('ativo') estacoes = Estacao.objects.all() + if (ativo): + estacoes = estacoes.filter(ativo=ativo) serializer = EstacaoSerializer(estacoes, many=True) return Response(serializer.data) - elif request.method == 'POST': - serializer = EstacaoSerializer(data=request.data) + def post(self, request, *args, **kwargs): + serializer = EstacaoSerializer(data=request.body) 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) - -@api_view(['GET', 'PUT', 'DELETE']) -def estacao_details(request, pk): - try: - estacao = Estacao.objects.get(pk=pk) - except Estacao.DoesNotExist: - return Response(status=status.HTTP_404_NOT_FOUND) - - if request.method == 'GET': + +class EstacoesDetalhesView(APIView): + def get(self, request, pk, *args, **kwargs): + try: + estacao = Estacao.objects.get(pk=pk) + except Estacao.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) serializer = EstacaoSerializer(estacao) return Response(serializer.data) - - elif request.method == 'PUT': - serializer = EstacaoSerializer(estacao, data=request.data) + def put(self, request, pk, *args, **kwargs): + try: + estacao = Estacao.objects.get(pk=pk) + except Estacao.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + serializer = EstacaoSerializer(estacao, data=request.body) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': - estacao.delete() + def delete(self, request, pk, *args, **kwargs): + try: + estacao = Estacao.objects.get(pk=pk) + except Estacao.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + estacao.ativo = False + estacao.save() return Response(status=status.HTTP_204_NO_CONTENT) -@api_view(['GET', 'POST']) -def endereco_list(request): - if request.method == 'GET': +class EnderecosView(APIView): + def get(self, request, *args, **kwargs): enderecos = Endereco.objects.all() serializer = EnderecoSerializer(enderecos, many=True) return Response(serializer.data) - elif request.method == 'POST': + def post(self, request, *args, **kwargs): serializer = EnderecoSerializer(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) -@api_view(['GET', 'PUT', 'DELETE']) -def endereco_details(request, pk): - try: - endereco = Endereco.objects.get(pk=pk) - except Endereco.DoesNotExist: - return Response(status=status.HTTP_404_NOT_FOUND) - - if request.method == 'GET': +class EnderecosDetalhesView(APIView): + def get(self, request, pk, *args, **kwargs): + try: + endereco = Endereco.objects.get(pk=pk) + except Endereco.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) serializer = EnderecoSerializer(endereco) return Response(serializer.data) - - elif request.method == 'PUT': + def put(self, request, pk, *args, **kwargs): + try: + endereco = Endereco.objects.get(pk=pk) + except Endereco.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) serializer = EnderecoSerializer(endereco, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': + def delete(self, request, pk, *args, **kwargs): + try: + endereco = Endereco.objects.get(pk=pk) + except Endereco.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) endereco.delete() - return Response(status=status.HTTP_204_NO_CONTENT) + return Response(status=status.HTTP_204_NO_CONTENT) -@api_view(['GET', 'POST']) -def parametro_list(request): - if request.method == 'GET': +class ParametrosView(APIView): + def get(self, request, *args, **kwargs): parametros = Parametro.objects.all() serializer = ParametroSerializer(parametros, many=True) return Response(serializer.data) - elif request.method == 'POST': + def post (self, request, *args, **kwargs): serializer = ParametroSerializer(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) -@api_view(['GET', 'PUT', 'DELETE']) -def parametro_details(request, pk): - try: - parametro = Parametro.objects.get(pk=pk) - except Parametro.DoesNotExist: - return Response(status=status.HTTP_404_NOT_FOUND) - - if request.method == 'GET': +class ParametrosDetalhesView(APIView): + def get(self, request, pk, *args, **kwargs): + try: + parametro = Parametro.objects.get(pk=pk) + except Parametro.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) serializer = ParametroSerializer(parametro) return Response(serializer.data) - - elif request.method == 'PUT': + def put(self, request, pk, *args, **kwargs): + try: + parametro = Parametro.objects.get(pk=pk) + except Parametro.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) serializer = ParametroSerializer(parametro, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': + def delete(self, request, pk, *args, **kwargs): + try: + parametro = Parametro.objects.get(pk=pk) + except Parametro.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) parametro.delete() return Response(status=status.HTTP_204_NO_CONTENT) - - - From 784ddca9e2d0e1096dd0b8c790018b15c7009e25 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Thu, 26 Sep 2024 19:33:17 -0300 Subject: [PATCH 2/2] refa: finalizando refatoracao das views --- src/tupan/estacoes/migrations/0001_initial.py | 4 ++-- src/tupan/estacoes/views.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tupan/estacoes/migrations/0001_initial.py b/src/tupan/estacoes/migrations/0001_initial.py index 4675c42..212b78c 100644 --- a/src/tupan/estacoes/migrations/0001_initial.py +++ b/src/tupan/estacoes/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.1.1 on 2024-09-23 13:45 +# Generated by Django 5.1.1 on 2024-09-26 22:15 import django.db.models.deletion from django.db import migrations, models @@ -59,7 +59,7 @@ class Migration(migrations.Migration): ('nome', models.CharField(max_length=127)), ('topico', models.CharField(help_text='Tópico do broker MQTT', max_length=127)), ('ativo', models.BooleanField(default=True)), - ('endereco', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='estacoes.endereco')), + ('endereco', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='estacoes.endereco')), ('parametros', models.ManyToManyField(blank=True, to='estacoes.parametro')), ], options={ diff --git a/src/tupan/estacoes/views.py b/src/tupan/estacoes/views.py index 26e0bdf..7a3550a 100644 --- a/src/tupan/estacoes/views.py +++ b/src/tupan/estacoes/views.py @@ -13,7 +13,7 @@ def get(self, request, *args, **kwargs): serializer = EstacaoSerializer(estacoes, many=True) return Response(serializer.data) def post(self, request, *args, **kwargs): - serializer = EstacaoSerializer(data=request.body) + serializer = EstacaoSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) @@ -32,7 +32,7 @@ def put(self, request, pk, *args, **kwargs): estacao = Estacao.objects.get(pk=pk) except Estacao.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - serializer = EstacaoSerializer(estacao, data=request.body) + serializer = EstacaoSerializer(estacao, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)