Skip to content

Commit

Permalink
refa: criando serializer de alertas e modificando metodo post
Browse files Browse the repository at this point in the history
  • Loading branch information
iagocpv committed Oct 9, 2024
1 parent c6e397f commit 033b25e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.1 on 2024-10-09 08:05

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('alertas', '0001_initial'),
('estacoes', '0002_alter_estacaoparametro_table'),
]

operations = [
migrations.AlterField(
model_name='alerta',
name='estacao_parametro',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro'),
),
]
2 changes: 1 addition & 1 deletion src/tupan/alertas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Alerta(Base):
nome = models.CharField(help_text="Nome do alerta", max_length=127, unique=True)
condicao = models.CharField(help_text="Condição para o alerta acontecer", max_length=4)
ativo = models.BooleanField(default=True)
estacao_parametro = models.ForeignKey(EstacaoParametro, on_delete=models.CASCADE, default=1)
estacao_parametro = models.ForeignKey(EstacaoParametro, on_delete=models.CASCADE)

class Meta:
verbose_name = "Alerta"
Expand Down
8 changes: 8 additions & 0 deletions src/tupan/alertas/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from .models import Alerta

class AlertaSerializer(serializers.ModelSerializer):
class Meta:
model = Alerta
fields = ('id', 'nome', 'condicao', 'estacao_parametro', 'criado', 'modificado', 'ativo')
read_only_fields = ['estacao_parametro']
25 changes: 10 additions & 15 deletions src/tupan/alertas/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
import json

from estacoes.models import EstacaoParametro

from .serializers import AlertaSerializer
from .models import Alerta, HistoricoAlerta, Medicao
from django.core.exceptions import ValidationError

Expand Down Expand Up @@ -47,23 +48,17 @@ def post(self, request, *args, **kwargs):
condicao = data.get('condicao')
ativo = data.get('ativo')

id_estacao = data.get('id_estacao')
id_parametro = data.get('id_parametro')
estacao_parametro = EstacaoParametro.objects.filter(estacao = id_estacao, parametro = id_parametro)

parametro_estacao = data.get('estacao_parametro')
estacao_parametro = EstacaoParametro.objects.get(estacao = parametro_estacao['estacao'], parametro = parametro_estacao['parametro'])
if not nome or not condicao:
return JsonResponse({'error': 'Campos obrigatórios: nome, condicao'}, status=400)

alerta = Alerta(nome=nome, condicao=condicao, ativo=ativo, estacao_parametro=estacao_parametro)
alerta.save()

return JsonResponse({
'id': alerta.pk,
'nome': alerta.nome,
'condicao': alerta.condicao,
'ativo': alerta.ativo,
'estacao_parametro': alerta.estacao_parametro
}, status=201)
serializer = AlertaSerializer(alerta, 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)

except json.JSONDecodeError:
return JsonResponse({'error': 'Dados inválidos'}, status=400)
Expand Down

0 comments on commit 033b25e

Please sign in to comment.