From 16f36df7c761d7f6c8f49377be3b1172ea34761b Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 1 Oct 2024 10:33:34 -0300 Subject: [PATCH 1/6] =?UTF-8?q?refa:=20cria=C3=A7=C3=A3o=20das=20tabelas?= =?UTF-8?q?=20categoria=20e=20estacao=20parametro=20e=20utiliza=C3=A7?= =?UTF-8?q?=C3=A3o=20delas=20como=20chave=20estrangeira?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rta_condicao_alter_alerta_nome_and_more.py | 48 +++++++++++++++++++ ...cao_parametro_medicao_estacao_parametro.py | 25 ++++++++++ src/tupan/alertas/models.py | 5 +- src/tupan/estacoes/admin.py | 17 +++++-- ...acao_parametros_endereco_ativo_and_more.py | 27 +++++++++++ .../migrations/0003_estacaoparametro.py | 28 +++++++++++ .../migrations/0004_estacao_parametros.py | 18 +++++++ ...e_parametro_unidade_parametro_descricao.py | 22 +++++++++ .../estacoes/migrations/0006_categoria.py | 28 +++++++++++ .../migrations/0007_parametro_categoria.py | 19 ++++++++ src/tupan/estacoes/models.py | 38 ++++++++++++--- 11 files changed, 264 insertions(+), 11 deletions(-) create mode 100644 src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py create mode 100644 src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py create mode 100644 src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py create mode 100644 src/tupan/estacoes/migrations/0003_estacaoparametro.py create mode 100644 src/tupan/estacoes/migrations/0004_estacao_parametros.py create mode 100644 src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py create mode 100644 src/tupan/estacoes/migrations/0006_categoria.py create mode 100644 src/tupan/estacoes/migrations/0007_parametro_categoria.py diff --git a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py new file mode 100644 index 0000000..614b78c --- /dev/null +++ b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py @@ -0,0 +1,48 @@ +# Generated by Django 5.1.1 on 2024-09-30 10:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('alertas', '0002_medicao'), + ] + + operations = [ + migrations.AlterField( + model_name='alerta', + name='condicao', + field=models.CharField(help_text='Condição para o alerta acontecer', max_length=4), + ), + migrations.AlterField( + model_name='alerta', + name='nome', + field=models.CharField(help_text='Nome do alerta', max_length=127, unique=True), + ), + migrations.AlterField( + model_name='historicoalerta', + name='timestamp', + field=models.BigIntegerField(help_text='Data/hora do alerta em timestamp'), + ), + migrations.AlterField( + model_name='historicoalerta', + name='timestamp_convertido', + field=models.DateTimeField(blank=True, help_text='Data/hora do alerta em datetime', null=True), + ), + migrations.AlterField( + model_name='medicao', + name='dados', + field=models.CharField(help_text='Valor dos dados da medição', max_length=63), + ), + migrations.AlterField( + model_name='medicao', + name='timestamp', + field=models.BigIntegerField(help_text='Data/hora da medição em timestamp'), + ), + migrations.AlterField( + model_name='medicao', + name='timestamp_convertido', + field=models.DateTimeField(blank=True, help_text='Data/hora da medição em datetime', null=True), + ), + ] diff --git a/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py b/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py new file mode 100644 index 0000000..efd5a49 --- /dev/null +++ b/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1.1 on 2024-09-30 10:34 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('alertas', '0003_alter_alerta_condicao_alter_alerta_nome_and_more'), + ('estacoes', '0004_estacao_parametros'), + ] + + operations = [ + migrations.AddField( + model_name='alerta', + name='estacao_parametro', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro'), + ), + migrations.AddField( + model_name='medicao', + name='estacao_parametro', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro'), + ), + ] diff --git a/src/tupan/alertas/models.py b/src/tupan/alertas/models.py index e5445dd..59b7084 100644 --- a/src/tupan/alertas/models.py +++ b/src/tupan/alertas/models.py @@ -1,5 +1,6 @@ from django.db import models from datetime import datetime +from estacoes.models import EstacaoParametro class Base(models.Model): @@ -14,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) -# falta a chave estrangeira da estacao_parametro + estacao_parametro = models.ForeignKey(EstacaoParametro, on_delete=models.CASCADE, default=1) class Meta: verbose_name = "Alerta" @@ -42,7 +43,7 @@ class Medicao(Base): timestamp = models.BigIntegerField(help_text="Data/hora da medição em timestamp", blank=False) timestamp_convertido = models.DateTimeField(help_text="Data/hora da medição em datetime", blank=True, null=True) dados = models.CharField(help_text="Valor dos dados da medição", max_length=63, blank=False, null=False) -# Falta a chave estrangeira estacao_parametro + estacao_parametro = models.ForeignKey(EstacaoParametro, on_delete=models.CASCADE, default=1) class Meta: verbose_name = "Medição" diff --git a/src/tupan/estacoes/admin.py b/src/tupan/estacoes/admin.py index 18b902b..3d4ca1a 100644 --- a/src/tupan/estacoes/admin.py +++ b/src/tupan/estacoes/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import Endereco, Estacao, Parametro +from .models import Endereco, Estacao, Parametro, Categoria @admin.register(Parametro) class ParametroAdmin(admin.ModelAdmin): @@ -8,7 +8,6 @@ class ParametroAdmin(admin.ModelAdmin): "nome", "fator", "offset", - "unidade", "nome_json", "criado", "modificado", @@ -16,13 +15,25 @@ class ParametroAdmin(admin.ModelAdmin): list_filter = [ "modificado", "nome", - "unidade", "criado" ] readonly_fields = [ "criado", "modificado" ] + + +@admin.register(Categoria) +class CategoriaAdmin(admin.ModelAdmin): + list_display = [ + "unidade", + "nome" + ] + list_filter = [ + "unidade" + ] + + @admin.register(Endereco) class EnderecoAdmin(admin.ModelAdmin): list_display = [ diff --git a/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py b/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py new file mode 100644 index 0000000..b712fc5 --- /dev/null +++ b/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 5.1.1 on 2024-09-30 10:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='estacao', + name='parametros', + ), + migrations.AddField( + model_name='endereco', + name='ativo', + field=models.BooleanField(default=True), + ), + migrations.AddField( + model_name='parametro', + name='ativo', + field=models.BooleanField(default=True), + ), + ] diff --git a/src/tupan/estacoes/migrations/0003_estacaoparametro.py b/src/tupan/estacoes/migrations/0003_estacaoparametro.py new file mode 100644 index 0000000..39ba26d --- /dev/null +++ b/src/tupan/estacoes/migrations/0003_estacaoparametro.py @@ -0,0 +1,28 @@ +# Generated by Django 5.1.1 on 2024-09-30 10:33 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0002_remove_estacao_parametros_endereco_ativo_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='EstacaoParametro', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('criado', models.DateTimeField(auto_now_add=True)), + ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), + ('estacao', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacao')), + ('parametro', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.parametro')), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/src/tupan/estacoes/migrations/0004_estacao_parametros.py b/src/tupan/estacoes/migrations/0004_estacao_parametros.py new file mode 100644 index 0000000..6e0c289 --- /dev/null +++ b/src/tupan/estacoes/migrations/0004_estacao_parametros.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.1 on 2024-09-30 10:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0003_estacaoparametro'), + ] + + operations = [ + migrations.AddField( + model_name='estacao', + name='parametros', + field=models.ManyToManyField(blank=True, through='estacoes.EstacaoParametro', to='estacoes.parametro'), + ), + ] diff --git a/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py b/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py new file mode 100644 index 0000000..dcd86c8 --- /dev/null +++ b/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py @@ -0,0 +1,22 @@ +# Generated by Django 5.1.1 on 2024-10-01 09:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0004_estacao_parametros'), + ] + + operations = [ + migrations.RemoveField( + model_name='parametro', + name='unidade', + ), + migrations.AddField( + model_name='parametro', + name='descricao', + field=models.TextField(blank=True, help_text='Descrição do que abrange o parâmetro', max_length=255), + ), + ] diff --git a/src/tupan/estacoes/migrations/0006_categoria.py b/src/tupan/estacoes/migrations/0006_categoria.py new file mode 100644 index 0000000..788f7d1 --- /dev/null +++ b/src/tupan/estacoes/migrations/0006_categoria.py @@ -0,0 +1,28 @@ +# Generated by Django 5.1.1 on 2024-10-01 09:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0005_remove_parametro_unidade_parametro_descricao'), + ] + + operations = [ + migrations.CreateModel( + name='Categoria', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('criado', models.DateTimeField(auto_now_add=True)), + ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), + ('unidade', models.CharField(help_text='Unidade de medida do parâmetro', max_length=30)), + ('nome', models.CharField(help_text='Nome da categoria do parâmetro', max_length=127)), + ], + options={ + 'verbose_name': 'Categoria', + 'verbose_name_plural': 'Categorias', + }, + ), + ] diff --git a/src/tupan/estacoes/migrations/0007_parametro_categoria.py b/src/tupan/estacoes/migrations/0007_parametro_categoria.py new file mode 100644 index 0000000..c820996 --- /dev/null +++ b/src/tupan/estacoes/migrations/0007_parametro_categoria.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1.1 on 2024-10-01 09:49 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0006_categoria'), + ] + + operations = [ + migrations.AddField( + model_name='parametro', + name='categoria', + field=models.ForeignKey(default=None, help_text='Tipo do parametro e unidade', on_delete=django.db.models.deletion.CASCADE, to='estacoes.categoria'), + ), + ] diff --git a/src/tupan/estacoes/models.py b/src/tupan/estacoes/models.py index 3f9bef6..1ab0614 100644 --- a/src/tupan/estacoes/models.py +++ b/src/tupan/estacoes/models.py @@ -1,21 +1,43 @@ from django.db import models -from alertas.models import Base + +class Base(models.Model): + criado = models.DateTimeField(auto_now_add=True) + modificado = models.DateTimeField(auto_now=True) + ativo = models.BooleanField(default=True) + + class Meta: + abstract = True + + +class Categoria(Base): + unidade = models.CharField(help_text="Unidade de medida do parâmetro", max_length=30, blank=False) + nome = models.CharField(help_text="Nome da categoria do parâmetro", max_length=127, blank=False) + + class Meta: + verbose_name = "Categoria" + verbose_name_plural = "Categorias" + + def __str__(self): + return f"{self.nome} em {self.unidade}" + class Parametro(Base): nome = models.CharField(max_length=127) fator = models.DecimalField(default=1, max_digits=10, decimal_places=4, help_text="Valor a ser multiplicado") offset = models.DecimalField(default=0, max_digits=10, decimal_places=4, help_text="Valor a ser adicionado") - unidade = models.CharField(max_length=30) nome_json = models.CharField(help_text="Nome do campo no json", max_length=127) + descricao = models.TextField(help_text="Descrição do que abrange o parâmetro", max_length=255, blank=True) + categoria = models.ForeignKey(Categoria, help_text="Tipo do parametro e unidade", default=None, on_delete=models.CASCADE) class Meta: verbose_name = "Parâmetro" verbose_name_plural = "Parâmetros" - + def __str__(self): return self.nome + class Endereco(Base): logradouro = models.CharField(max_length=127) bairro = models.CharField(max_length=127) @@ -34,12 +56,12 @@ class Meta: def __str__(self): return self.cep + class Estacao(Base): nome = models.CharField(max_length=127) 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) + parametros = models.ManyToManyField(Parametro, blank=True, through='EstacaoParametro') class Meta: verbose_name = "Estação" @@ -47,4 +69,8 @@ class Meta: def __str__(self): return self.nome - + + +class EstacaoParametro(Base): + estacao = models.ForeignKey(Estacao, on_delete=models.CASCADE) + parametro = models.ForeignKey(Parametro, on_delete=models.CASCADE) From 3cadce32146e06bd6bd8da1e62941d309143665d Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 8 Oct 2024 11:00:19 -0300 Subject: [PATCH 2/6] corr: salvando parametro na estacao --- src/tupan/alertas/migrations/0001_initial.py | 28 +++++++-- src/tupan/alertas/migrations/0002_medicao.py | 28 --------- ...rta_condicao_alter_alerta_nome_and_more.py | 48 ---------------- ...cao_parametro_medicao_estacao_parametro.py | 25 -------- src/tupan/alertas/views.py | 15 ++++- src/tupan/estacoes/migrations/0001_initial.py | 57 +++++++++++++++---- .../0002_alter_estacaoparametro_table.py | 17 ++++++ ...acao_parametros_endereco_ativo_and_more.py | 27 --------- .../migrations/0003_estacaoparametro.py | 28 --------- .../migrations/0004_estacao_parametros.py | 18 ------ ...e_parametro_unidade_parametro_descricao.py | 22 ------- .../estacoes/migrations/0006_categoria.py | 28 --------- .../migrations/0007_parametro_categoria.py | 19 ------- src/tupan/estacoes/models.py | 4 +- src/tupan/estacoes/serializers.py | 14 ++++- src/tupan/estacoes/urls.py | 5 +- src/tupan/estacoes/views.py | 44 +++++++++++++- src/tupan/usuarios/migrations/0001_initial.py | 25 ++++---- ...io_options_remove_usuario_user_and_more.py | 48 ---------------- ..._date_joined_usuario_is_active_and_more.py | 34 ----------- ...oined_remove_usuario_is_active_and_more.py | 53 ----------------- ...5_usuario_is_staff_usuario_is_superuser.py | 23 -------- ...usuario_groups_usuario_user_permissions.py | 24 -------- 23 files changed, 173 insertions(+), 461 deletions(-) delete mode 100644 src/tupan/alertas/migrations/0002_medicao.py delete mode 100644 src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py delete mode 100644 src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py create mode 100644 src/tupan/estacoes/migrations/0002_alter_estacaoparametro_table.py delete mode 100644 src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py delete mode 100644 src/tupan/estacoes/migrations/0003_estacaoparametro.py delete mode 100644 src/tupan/estacoes/migrations/0004_estacao_parametros.py delete mode 100644 src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py delete mode 100644 src/tupan/estacoes/migrations/0006_categoria.py delete mode 100644 src/tupan/estacoes/migrations/0007_parametro_categoria.py delete mode 100644 src/tupan/usuarios/migrations/0002_alter_usuario_options_remove_usuario_user_and_more.py delete mode 100644 src/tupan/usuarios/migrations/0003_usuario_date_joined_usuario_is_active_and_more.py delete mode 100644 src/tupan/usuarios/migrations/0004_remove_usuario_date_joined_remove_usuario_is_active_and_more.py delete mode 100644 src/tupan/usuarios/migrations/0005_usuario_is_staff_usuario_is_superuser.py delete mode 100644 src/tupan/usuarios/migrations/0006_usuario_groups_usuario_user_permissions.py diff --git a/src/tupan/alertas/migrations/0001_initial.py b/src/tupan/alertas/migrations/0001_initial.py index 75a8198..4b68fb5 100644 --- a/src/tupan/alertas/migrations/0001_initial.py +++ b/src/tupan/alertas/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.1.1 on 2024-09-12 01:45 +# Generated by Django 5.1.1 on 2024-10-08 09:52 import django.db.models.deletion from django.db import migrations, models @@ -9,6 +9,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('estacoes', '0001_initial'), ] operations = [ @@ -18,9 +19,10 @@ class Migration(migrations.Migration): ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('criado', models.DateTimeField(auto_now_add=True)), ('modificado', models.DateTimeField(auto_now=True)), - ('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)), + ('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(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro')), ], options={ 'verbose_name': 'Alerta', @@ -33,8 +35,8 @@ class Migration(migrations.Migration): ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('criado', models.DateTimeField(auto_now_add=True)), ('modificado', models.DateTimeField(auto_now=True)), - ('timestamp', models.BigIntegerField()), - ('timestamp_convertido', models.DateTimeField(blank=True, null=True)), + ('timestamp', models.BigIntegerField(help_text='Data/hora do alerta em timestamp')), + ('timestamp_convertido', models.DateTimeField(blank=True, help_text='Data/hora do alerta em datetime', null=True)), ('alerta', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='historico_alertas', to='alertas.alerta')), ], options={ @@ -42,4 +44,20 @@ class Migration(migrations.Migration): 'verbose_name_plural': 'Históricos de Alertas', }, ), + migrations.CreateModel( + name='Medicao', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('criado', models.DateTimeField(auto_now_add=True)), + ('modificado', models.DateTimeField(auto_now=True)), + ('timestamp', models.BigIntegerField(help_text='Data/hora da medição em timestamp')), + ('timestamp_convertido', models.DateTimeField(blank=True, help_text='Data/hora da medição em datetime', null=True)), + ('dados', models.CharField(help_text='Valor dos dados da medição', max_length=63)), + ('estacao_parametro', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro')), + ], + options={ + 'verbose_name': 'Medição', + 'verbose_name_plural': 'Medições', + }, + ), ] diff --git a/src/tupan/alertas/migrations/0002_medicao.py b/src/tupan/alertas/migrations/0002_medicao.py deleted file mode 100644 index 01ef7d6..0000000 --- a/src/tupan/alertas/migrations/0002_medicao.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-13 12:55 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('alertas', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Medicao', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('criado', models.DateTimeField(auto_now_add=True)), - ('modificado', models.DateTimeField(auto_now=True)), - ('timestamp', models.BigIntegerField()), - ('timestamp_convertido', models.DateTimeField(blank=True, null=True)), - ('dados', models.CharField(max_length=63)), - ], - options={ - 'verbose_name': 'Medição', - 'verbose_name_plural': 'Medições', - }, - ), - ] diff --git a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py deleted file mode 100644 index 614b78c..0000000 --- a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py +++ /dev/null @@ -1,48 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-30 10:25 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('alertas', '0002_medicao'), - ] - - operations = [ - migrations.AlterField( - model_name='alerta', - name='condicao', - field=models.CharField(help_text='Condição para o alerta acontecer', max_length=4), - ), - migrations.AlterField( - model_name='alerta', - name='nome', - field=models.CharField(help_text='Nome do alerta', max_length=127, unique=True), - ), - migrations.AlterField( - model_name='historicoalerta', - name='timestamp', - field=models.BigIntegerField(help_text='Data/hora do alerta em timestamp'), - ), - migrations.AlterField( - model_name='historicoalerta', - name='timestamp_convertido', - field=models.DateTimeField(blank=True, help_text='Data/hora do alerta em datetime', null=True), - ), - migrations.AlterField( - model_name='medicao', - name='dados', - field=models.CharField(help_text='Valor dos dados da medição', max_length=63), - ), - migrations.AlterField( - model_name='medicao', - name='timestamp', - field=models.BigIntegerField(help_text='Data/hora da medição em timestamp'), - ), - migrations.AlterField( - model_name='medicao', - name='timestamp_convertido', - field=models.DateTimeField(blank=True, help_text='Data/hora da medição em datetime', null=True), - ), - ] diff --git a/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py b/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py deleted file mode 100644 index efd5a49..0000000 --- a/src/tupan/alertas/migrations/0004_alerta_estacao_parametro_medicao_estacao_parametro.py +++ /dev/null @@ -1,25 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-30 10:34 - -import django.db.models.deletion -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('alertas', '0003_alter_alerta_condicao_alter_alerta_nome_and_more'), - ('estacoes', '0004_estacao_parametros'), - ] - - operations = [ - migrations.AddField( - model_name='alerta', - name='estacao_parametro', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro'), - ), - migrations.AddField( - model_name='medicao', - name='estacao_parametro', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacaoparametro'), - ), - ] diff --git a/src/tupan/alertas/views.py b/src/tupan/alertas/views.py index 7419391..14fd993 100644 --- a/src/tupan/alertas/views.py +++ b/src/tupan/alertas/views.py @@ -1,11 +1,15 @@ from rest_framework.views import APIView from django.http import JsonResponse import json + +from estacoes.models import EstacaoParametro + from .models import Alerta, HistoricoAlerta, Medicao from django.core.exceptions import ValidationError class AlertasView(APIView): + def get(self, request, *args, **kwargs): try: # Obtém o valor do parâmetro 'ativo' da requisição, se disponível @@ -35,23 +39,30 @@ def get(self, request, *args, **kwargs): 'data': f"{e}" }, status=500) + def post(self, request, *args, **kwargs): try: data = json.loads(request.body) nome = data.get('nome') 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) if not nome or not condicao: return JsonResponse({'error': 'Campos obrigatórios: nome, condicao'}, status=400) - alerta = Alerta(nome=nome, condicao=condicao) + 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 + 'ativo': alerta.ativo, + 'estacao_parametro': alerta.estacao_parametro }, status=201) except json.JSONDecodeError: diff --git a/src/tupan/estacoes/migrations/0001_initial.py b/src/tupan/estacoes/migrations/0001_initial.py index 212b78c..a5f8072 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-26 22:15 +# Generated by Django 5.1.1 on 2024-10-08 09:52 import django.db.models.deletion from django.db import migrations, models @@ -12,12 +12,28 @@ class Migration(migrations.Migration): ] operations = [ + migrations.CreateModel( + name='Categoria', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('criado', models.DateTimeField(auto_now_add=True)), + ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), + ('unidade', models.CharField(help_text='Unidade de medida do parâmetro', max_length=30)), + ('nome', models.CharField(help_text='Nome da categoria do parâmetro', max_length=127)), + ], + options={ + 'verbose_name': 'Categoria', + 'verbose_name_plural': 'Categorias', + }, + ), migrations.CreateModel( name='Endereco', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('criado', models.DateTimeField(auto_now_add=True)), ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), ('logradouro', models.CharField(max_length=127)), ('bairro', models.CharField(max_length=127)), ('cidade', models.CharField(max_length=127)), @@ -33,17 +49,35 @@ class Migration(migrations.Migration): 'verbose_name_plural': 'Endereços', }, ), + migrations.CreateModel( + name='Estacao', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('criado', models.DateTimeField(auto_now_add=True)), + ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), + ('nome', models.CharField(max_length=127)), + ('topico', models.CharField(help_text='Tópico do broker MQTT', max_length=127)), + ('endereco', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='estacoes.endereco')), + ], + options={ + 'verbose_name': 'Estação', + 'verbose_name_plural': 'Estações', + }, + ), migrations.CreateModel( name='Parametro', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('criado', models.DateTimeField(auto_now_add=True)), ('modificado', models.DateTimeField(auto_now=True)), + ('ativo', models.BooleanField(default=True)), ('nome', models.CharField(max_length=127)), ('fator', models.DecimalField(decimal_places=4, default=1, help_text='Valor a ser multiplicado', max_digits=10)), ('offset', models.DecimalField(decimal_places=4, default=0, help_text='Valor a ser adicionado', max_digits=10)), - ('unidade', models.CharField(max_length=30)), ('nome_json', models.CharField(help_text='Nome do campo no json', max_length=127)), + ('descricao', models.TextField(blank=True, help_text='Descrição do que abrange o parâmetro', max_length=255)), + ('categoria', models.ForeignKey(default=None, help_text='Tipo do parametro e unidade', on_delete=django.db.models.deletion.CASCADE, to='estacoes.categoria')), ], options={ 'verbose_name': 'Parâmetro', @@ -51,20 +85,19 @@ class Migration(migrations.Migration): }, ), migrations.CreateModel( - name='Estacao', + name='EstacaoParametro', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('criado', models.DateTimeField(auto_now_add=True)), - ('modificado', models.DateTimeField(auto_now=True)), - ('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(on_delete=django.db.models.deletion.CASCADE, to='estacoes.endereco')), - ('parametros', models.ManyToManyField(blank=True, to='estacoes.parametro')), + ('estacao', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacao')), + ('parametro', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.parametro')), ], options={ - 'verbose_name': 'Estação', - 'verbose_name_plural': 'Estações', + 'db_table': 'estacoes_estacao_parametros', }, ), + migrations.AddField( + model_name='estacao', + name='parametros', + field=models.ManyToManyField(blank=True, through='estacoes.EstacaoParametro', to='estacoes.parametro'), + ), ] diff --git a/src/tupan/estacoes/migrations/0002_alter_estacaoparametro_table.py b/src/tupan/estacoes/migrations/0002_alter_estacaoparametro_table.py new file mode 100644 index 0000000..9a2517e --- /dev/null +++ b/src/tupan/estacoes/migrations/0002_alter_estacaoparametro_table.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1.1 on 2024-10-08 10:12 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('estacoes', '0001_initial'), + ] + + operations = [ + migrations.AlterModelTable( + name='estacaoparametro', + table='estacoes_estacao_parametro', + ), + ] diff --git a/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py b/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py deleted file mode 100644 index b712fc5..0000000 --- a/src/tupan/estacoes/migrations/0002_remove_estacao_parametros_endereco_ativo_and_more.py +++ /dev/null @@ -1,27 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-30 10:32 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0001_initial'), - ] - - operations = [ - migrations.RemoveField( - model_name='estacao', - name='parametros', - ), - migrations.AddField( - model_name='endereco', - name='ativo', - field=models.BooleanField(default=True), - ), - migrations.AddField( - model_name='parametro', - name='ativo', - field=models.BooleanField(default=True), - ), - ] diff --git a/src/tupan/estacoes/migrations/0003_estacaoparametro.py b/src/tupan/estacoes/migrations/0003_estacaoparametro.py deleted file mode 100644 index 39ba26d..0000000 --- a/src/tupan/estacoes/migrations/0003_estacaoparametro.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-30 10:33 - -import django.db.models.deletion -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0002_remove_estacao_parametros_endereco_ativo_and_more'), - ] - - operations = [ - migrations.CreateModel( - name='EstacaoParametro', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('criado', models.DateTimeField(auto_now_add=True)), - ('modificado', models.DateTimeField(auto_now=True)), - ('ativo', models.BooleanField(default=True)), - ('estacao', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.estacao')), - ('parametro', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='estacoes.parametro')), - ], - options={ - 'abstract': False, - }, - ), - ] diff --git a/src/tupan/estacoes/migrations/0004_estacao_parametros.py b/src/tupan/estacoes/migrations/0004_estacao_parametros.py deleted file mode 100644 index 6e0c289..0000000 --- a/src/tupan/estacoes/migrations/0004_estacao_parametros.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-30 10:34 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0003_estacaoparametro'), - ] - - operations = [ - migrations.AddField( - model_name='estacao', - name='parametros', - field=models.ManyToManyField(blank=True, through='estacoes.EstacaoParametro', to='estacoes.parametro'), - ), - ] diff --git a/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py b/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py deleted file mode 100644 index dcd86c8..0000000 --- a/src/tupan/estacoes/migrations/0005_remove_parametro_unidade_parametro_descricao.py +++ /dev/null @@ -1,22 +0,0 @@ -# Generated by Django 5.1.1 on 2024-10-01 09:38 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0004_estacao_parametros'), - ] - - operations = [ - migrations.RemoveField( - model_name='parametro', - name='unidade', - ), - migrations.AddField( - model_name='parametro', - name='descricao', - field=models.TextField(blank=True, help_text='Descrição do que abrange o parâmetro', max_length=255), - ), - ] diff --git a/src/tupan/estacoes/migrations/0006_categoria.py b/src/tupan/estacoes/migrations/0006_categoria.py deleted file mode 100644 index 788f7d1..0000000 --- a/src/tupan/estacoes/migrations/0006_categoria.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 5.1.1 on 2024-10-01 09:46 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0005_remove_parametro_unidade_parametro_descricao'), - ] - - operations = [ - migrations.CreateModel( - name='Categoria', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('criado', models.DateTimeField(auto_now_add=True)), - ('modificado', models.DateTimeField(auto_now=True)), - ('ativo', models.BooleanField(default=True)), - ('unidade', models.CharField(help_text='Unidade de medida do parâmetro', max_length=30)), - ('nome', models.CharField(help_text='Nome da categoria do parâmetro', max_length=127)), - ], - options={ - 'verbose_name': 'Categoria', - 'verbose_name_plural': 'Categorias', - }, - ), - ] diff --git a/src/tupan/estacoes/migrations/0007_parametro_categoria.py b/src/tupan/estacoes/migrations/0007_parametro_categoria.py deleted file mode 100644 index c820996..0000000 --- a/src/tupan/estacoes/migrations/0007_parametro_categoria.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 5.1.1 on 2024-10-01 09:49 - -import django.db.models.deletion -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('estacoes', '0006_categoria'), - ] - - operations = [ - migrations.AddField( - model_name='parametro', - name='categoria', - field=models.ForeignKey(default=None, help_text='Tipo do parametro e unidade', on_delete=django.db.models.deletion.CASCADE, to='estacoes.categoria'), - ), - ] diff --git a/src/tupan/estacoes/models.py b/src/tupan/estacoes/models.py index 1ab0614..8ed3040 100644 --- a/src/tupan/estacoes/models.py +++ b/src/tupan/estacoes/models.py @@ -71,6 +71,8 @@ def __str__(self): return self.nome -class EstacaoParametro(Base): +class EstacaoParametro(models.Model): estacao = models.ForeignKey(Estacao, on_delete=models.CASCADE) parametro = models.ForeignKey(Parametro, on_delete=models.CASCADE) + class Meta: + db_table = "estacoes_estacao_parametro" diff --git a/src/tupan/estacoes/serializers.py b/src/tupan/estacoes/serializers.py index 692f325..d702b33 100644 --- a/src/tupan/estacoes/serializers.py +++ b/src/tupan/estacoes/serializers.py @@ -1,10 +1,10 @@ from rest_framework import serializers -from .models import Parametro, Endereco, Estacao +from .models import Categoria, Parametro, Endereco, Estacao, EstacaoParametro class ParametroSerializer(serializers.ModelSerializer): class Meta: model = Parametro - fields = ('id', 'nome', 'fator', 'offset', 'unidade', 'nome_json', 'criado', 'modificado') + fields = ('id', 'nome', 'fator', 'offset', 'nome_json', 'descricao', 'categoria', 'criado', 'modificado') class EnderecoSerializer(serializers.ModelSerializer): class Meta: @@ -15,3 +15,13 @@ class EstacaoSerializer(serializers.ModelSerializer): class Meta: model = Estacao fields = ('id', 'nome', 'endereco', 'topico', 'ativo', 'parametros', 'criado', 'modificado') + +class EstacaoParametroSerializer(serializers.ModelSerializer): + class Meta: + model = EstacaoParametro + fields = ('id', 'estacao', 'parametro') + +class CategoriaSerializer(serializers.ModelSerializer): + class Meta: + model = Categoria + fields = ('unidade', 'nome', 'criado', 'modificado', 'ativo') diff --git a/src/tupan/estacoes/urls.py b/src/tupan/estacoes/urls.py index 82c612e..6bd4e6b 100644 --- a/src/tupan/estacoes/urls.py +++ b/src/tupan/estacoes/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from .views import EstacoesView, EstacoesDetalhesView, EnderecosView, EnderecosDetalhesView, ParametrosView, ParametrosDetalhesView +from .views import CategoriasView, EstacoesView, EstacoesDetalhesView, EnderecosView, EnderecosDetalhesView, ParametrosView, ParametrosDetalhesView urlpatterns = [ path("estacoes", EstacoesView.as_view(), name="Estações"), @@ -7,5 +7,6 @@ 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") + path("parametros/", ParametrosDetalhesView.as_view(), name="Parâmetro"), + path("categorias", CategoriasView.as_view(), name="Categorias") ] \ No newline at end of file diff --git a/src/tupan/estacoes/views.py b/src/tupan/estacoes/views.py index 7a3550a..28c8ebd 100644 --- a/src/tupan/estacoes/views.py +++ b/src/tupan/estacoes/views.py @@ -1,9 +1,13 @@ from .models import Estacao, Parametro, Endereco -from .serializers import EstacaoSerializer, EnderecoSerializer, ParametroSerializer +from .serializers import CategoriaSerializer, EstacaoSerializer, EnderecoSerializer, ParametroSerializer +from django.core.exceptions import ValidationError from rest_framework.response import Response from rest_framework import status from rest_framework.views import APIView + + + class EstacoesView(APIView): def get(self, request, *args, **kwargs): ativo = request.query_params.get('ativo') @@ -13,9 +17,37 @@ def get(self, request, *args, **kwargs): serializer = EstacaoSerializer(estacoes, many=True) return Response(serializer.data) def post(self, request, *args, **kwargs): + + # Suponha que você tenha as instâncias já carregadas: + endereco_instance = Endereco.objects.get(id=request.data["endereco"]) + + # Criando uma nova Estacao + estacao_nome = request.data["nome"] + + # Validando se o nome não está vazio + if not estacao_nome: + raise ValidationError("O nome da estação não pode ser vazio.") + + estacao = Estacao(nome=estacao_nome, endereco=endereco_instance, topico=request.data["topico"]) + + # Antes de salvar, você pode verificar se a estação já existe + if Estacao.objects.filter(nome=estacao.nome).exists(): + raise ValidationError("Já existe uma estação com esse nome.") + + # Salvar a estação + estacao.save() + + # Adicionando parâmetros + parametro_instance = Parametro.objects.get(id=request.data["parametros"]) + + # Validando se o parâmetro existe + if not parametro_instance: + raise ValidationError("O parâmetro não existe.") + + estacao.parametros.add(parametro_instance) + estacao.save() serializer = EstacaoSerializer(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) @@ -121,3 +153,11 @@ def delete(self, request, pk, *args, **kwargs): return Response(status=status.HTTP_404_NOT_FOUND) parametro.delete() return Response(status=status.HTTP_204_NO_CONTENT) + +class CategoriasView(APIView): + def post (self, request, *args, **kwargs): + serializer = CategoriaSerializer(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) diff --git a/src/tupan/usuarios/migrations/0001_initial.py b/src/tupan/usuarios/migrations/0001_initial.py index e31befa..da24a8d 100644 --- a/src/tupan/usuarios/migrations/0001_initial.py +++ b/src/tupan/usuarios/migrations/0001_initial.py @@ -1,7 +1,6 @@ -# Generated by Django 5.1.1 on 2024-09-18 11:02 +# Generated by Django 5.1.1 on 2024-10-08 09:52 -import django.db.models.deletion -from django.conf import settings +import django.utils.timezone from django.db import migrations, models @@ -10,7 +9,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('auth', '0012_alter_user_first_name_max_length'), ] operations = [ @@ -18,14 +17,20 @@ class Migration(migrations.Migration): name='Usuario', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('criacao', models.DateTimeField(auto_now_add=True)), - ('alterado', models.DateTimeField(auto_now=True)), - ('ativo', models.BooleanField(default=True)), - ('email', models.EmailField(max_length=254, unique=True)), - ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')), + ('password', models.CharField(max_length=255, verbose_name='password')), + ('ativo', models.BooleanField(default=True, verbose_name='active')), + ('criacao', models.DateTimeField(default=django.utils.timezone.now, verbose_name='created at')), + ('alterado', models.DateTimeField(auto_now=True, verbose_name='updated at')), + ('is_superuser', models.BooleanField(default=False, verbose_name='superuser')), + ('is_staff', models.BooleanField(default=False, verbose_name='staff')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), ], options={ - 'abstract': False, + 'verbose_name': 'Usuário', + 'verbose_name_plural': 'Usuários', }, ), ] diff --git a/src/tupan/usuarios/migrations/0002_alter_usuario_options_remove_usuario_user_and_more.py b/src/tupan/usuarios/migrations/0002_alter_usuario_options_remove_usuario_user_and_more.py deleted file mode 100644 index 1a2edc6..0000000 --- a/src/tupan/usuarios/migrations/0002_alter_usuario_options_remove_usuario_user_and_more.py +++ /dev/null @@ -1,48 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-20 11:00 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('usuarios', '0001_initial'), - ] - - operations = [ - migrations.AlterModelOptions( - name='usuario', - options={'verbose_name': 'Usuário', 'verbose_name_plural': 'Usuários'}, - ), - migrations.RemoveField( - model_name='usuario', - name='user', - ), - migrations.AddField( - model_name='usuario', - name='is_anonymous', - field=models.BooleanField(default=False), - ), - migrations.AddField( - model_name='usuario', - name='is_authenticated', - field=models.BooleanField(default=False), - preserve_default=False, - ), - migrations.AddField( - model_name='usuario', - name='is_staff', - field=models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status'), - ), - migrations.AddField( - model_name='usuario', - name='password', - field=models.CharField(default=False, max_length=255, verbose_name='password'), - preserve_default=False, - ), - migrations.AlterField( - model_name='usuario', - name='email', - field=models.EmailField(max_length=254, unique=True, verbose_name='email address'), - ), - ] diff --git a/src/tupan/usuarios/migrations/0003_usuario_date_joined_usuario_is_active_and_more.py b/src/tupan/usuarios/migrations/0003_usuario_date_joined_usuario_is_active_and_more.py deleted file mode 100644 index 86be506..0000000 --- a/src/tupan/usuarios/migrations/0003_usuario_date_joined_usuario_is_active_and_more.py +++ /dev/null @@ -1,34 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-20 11:08 - -import django.utils.timezone -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('usuarios', '0002_alter_usuario_options_remove_usuario_user_and_more'), - ] - - operations = [ - migrations.AddField( - model_name='usuario', - name='date_joined', - field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined'), - ), - migrations.AddField( - model_name='usuario', - name='is_active', - field=models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active'), - ), - migrations.AddField( - model_name='usuario', - name='is_superuser', - field=models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status'), - ), - migrations.AddField( - model_name='usuario', - name='last_login', - field=models.DateTimeField(blank=True, null=True, verbose_name='last login'), - ), - ] diff --git a/src/tupan/usuarios/migrations/0004_remove_usuario_date_joined_remove_usuario_is_active_and_more.py b/src/tupan/usuarios/migrations/0004_remove_usuario_date_joined_remove_usuario_is_active_and_more.py deleted file mode 100644 index 80720c2..0000000 --- a/src/tupan/usuarios/migrations/0004_remove_usuario_date_joined_remove_usuario_is_active_and_more.py +++ /dev/null @@ -1,53 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-20 11:39 - -import django.utils.timezone -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('usuarios', '0003_usuario_date_joined_usuario_is_active_and_more'), - ] - - operations = [ - migrations.RemoveField( - model_name='usuario', - name='date_joined', - ), - migrations.RemoveField( - model_name='usuario', - name='is_active', - ), - migrations.RemoveField( - model_name='usuario', - name='is_anonymous', - ), - migrations.RemoveField( - model_name='usuario', - name='is_authenticated', - ), - migrations.RemoveField( - model_name='usuario', - name='is_staff', - ), - migrations.RemoveField( - model_name='usuario', - name='is_superuser', - ), - migrations.AlterField( - model_name='usuario', - name='alterado', - field=models.DateTimeField(auto_now=True, verbose_name='updated at'), - ), - migrations.AlterField( - model_name='usuario', - name='ativo', - field=models.BooleanField(default=True, verbose_name='active'), - ), - migrations.AlterField( - model_name='usuario', - name='criacao', - field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='created at'), - ), - ] diff --git a/src/tupan/usuarios/migrations/0005_usuario_is_staff_usuario_is_superuser.py b/src/tupan/usuarios/migrations/0005_usuario_is_staff_usuario_is_superuser.py deleted file mode 100644 index 00082fa..0000000 --- a/src/tupan/usuarios/migrations/0005_usuario_is_staff_usuario_is_superuser.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-20 11:43 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('usuarios', '0004_remove_usuario_date_joined_remove_usuario_is_active_and_more'), - ] - - operations = [ - migrations.AddField( - model_name='usuario', - name='is_staff', - field=models.BooleanField(default=False, verbose_name='staff'), - ), - migrations.AddField( - model_name='usuario', - name='is_superuser', - field=models.BooleanField(default=False, verbose_name='superuser'), - ), - ] diff --git a/src/tupan/usuarios/migrations/0006_usuario_groups_usuario_user_permissions.py b/src/tupan/usuarios/migrations/0006_usuario_groups_usuario_user_permissions.py deleted file mode 100644 index 62f26f6..0000000 --- a/src/tupan/usuarios/migrations/0006_usuario_groups_usuario_user_permissions.py +++ /dev/null @@ -1,24 +0,0 @@ -# Generated by Django 5.1.1 on 2024-09-20 12:44 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), - ('usuarios', '0005_usuario_is_staff_usuario_is_superuser'), - ] - - operations = [ - migrations.AddField( - model_name='usuario', - name='groups', - field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups'), - ), - migrations.AddField( - model_name='usuario', - name='user_permissions', - field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'), - ), - ] From c6e397f5895e5714e8c043a29588b5d40f1ecf11 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Tue, 8 Oct 2024 22:51:18 -0300 Subject: [PATCH 3/6] corr: alterando post estacao para receber varios parametros e arrumando rota update estacao --- src/tupan/estacoes/views.py | 48 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/tupan/estacoes/views.py b/src/tupan/estacoes/views.py index 28c8ebd..25a7dfe 100644 --- a/src/tupan/estacoes/views.py +++ b/src/tupan/estacoes/views.py @@ -5,9 +5,6 @@ from rest_framework import status from rest_framework.views import APIView - - - class EstacoesView(APIView): def get(self, request, *args, **kwargs): ativo = request.query_params.get('ativo') @@ -17,37 +14,28 @@ def get(self, request, *args, **kwargs): serializer = EstacaoSerializer(estacoes, many=True) return Response(serializer.data) def post(self, request, *args, **kwargs): - - # Suponha que você tenha as instâncias já carregadas: - endereco_instance = Endereco.objects.get(id=request.data["endereco"]) - - # Criando uma nova Estacao + try: + endereco_instance = Endereco.objects.get(id=request.data["endereco"]) + except Endereco.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + estacao_nome = request.data["nome"] - - # Validando se o nome não está vazio if not estacao_nome: raise ValidationError("O nome da estação não pode ser vazio.") - - estacao = Estacao(nome=estacao_nome, endereco=endereco_instance, topico=request.data["topico"]) - - # Antes de salvar, você pode verificar se a estação já existe - if Estacao.objects.filter(nome=estacao.nome).exists(): + if Estacao.objects.filter(nome=estacao_nome).exists(): raise ValidationError("Já existe uma estação com esse nome.") - - # Salvar a estação + + estacao = Estacao(nome=estacao_nome, endereco=endereco_instance, topico=request.data["topico"]) estacao.save() - # Adicionando parâmetros - parametro_instance = Parametro.objects.get(id=request.data["parametros"]) - - # Validando se o parâmetro existe - if not parametro_instance: - raise ValidationError("O parâmetro não existe.") + parametros = request.data["parametros"] + for p in parametros: + parametro = Parametro.objects.get(id=p) + estacao.parametros.add(parametro) - estacao.parametros.add(parametro_instance) - estacao.save() - serializer = EstacaoSerializer(data=request.data) + serializer = EstacaoSerializer(estacao, 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) @@ -64,6 +52,14 @@ def put(self, request, pk, *args, **kwargs): estacao = Estacao.objects.get(pk=pk) except Estacao.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) + + parametros = request.data["parametros"] + novosParametros = [] + for p in parametros: + parametro = Parametro.objects.get(id=p) + novosParametros.append(parametro) + estacao.parametros.set(novosParametros) + serializer = EstacaoSerializer(estacao, data=request.data) if serializer.is_valid(): serializer.save() From 033b25ee8041b2f760837db6e33fd8c7abb9eed9 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 9 Oct 2024 10:44:15 -0300 Subject: [PATCH 4/6] refa: criando serializer de alertas e modificando metodo post --- .../0002_alter_alerta_estacao_parametro.py | 20 +++++++++++++++ src/tupan/alertas/models.py | 2 +- src/tupan/alertas/serializers.py | 8 ++++++ src/tupan/alertas/views.py | 25 ++++++++----------- 4 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 src/tupan/alertas/migrations/0002_alter_alerta_estacao_parametro.py create mode 100644 src/tupan/alertas/serializers.py diff --git a/src/tupan/alertas/migrations/0002_alter_alerta_estacao_parametro.py b/src/tupan/alertas/migrations/0002_alter_alerta_estacao_parametro.py new file mode 100644 index 0000000..b11268a --- /dev/null +++ b/src/tupan/alertas/migrations/0002_alter_alerta_estacao_parametro.py @@ -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'), + ), + ] diff --git a/src/tupan/alertas/models.py b/src/tupan/alertas/models.py index 59b7084..a37a15d 100644 --- a/src/tupan/alertas/models.py +++ b/src/tupan/alertas/models.py @@ -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" diff --git a/src/tupan/alertas/serializers.py b/src/tupan/alertas/serializers.py new file mode 100644 index 0000000..158eaee --- /dev/null +++ b/src/tupan/alertas/serializers.py @@ -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'] \ No newline at end of file diff --git a/src/tupan/alertas/views.py b/src/tupan/alertas/views.py index 14fd993..b1976e8 100644 --- a/src/tupan/alertas/views.py +++ b/src/tupan/alertas/views.py @@ -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 @@ -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) From 17343bfb37e0672a426b6d185a592fccb6fb4e6d Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Fri, 11 Oct 2024 08:46:40 -0300 Subject: [PATCH 5/6] corr: url detalhes alerta --- src/tupan/alertas/urls.py | 2 +- src/tupan/alertas/views.py | 16 +++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/tupan/alertas/urls.py b/src/tupan/alertas/urls.py index 1ad748b..3868381 100644 --- a/src/tupan/alertas/urls.py +++ b/src/tupan/alertas/urls.py @@ -3,7 +3,7 @@ urlpatterns = [ path('', AlertasView.as_view(), name='alertas'), - path('', AlertasDetalhesView.as_view(), name='alertas-detalhes'), + path('/', AlertasDetalhesView.as_view(), name='alertas-detalhes'), path('historicos', HistoricoAlertaView.as_view(), name='historico-alertas'), path('medicoes', MedicaoView.as_view(), name='medicoes'), path('medicoes/', MedicaoDetalhesView.as_view(), name='medicoes-detalhes') diff --git a/src/tupan/alertas/views.py b/src/tupan/alertas/views.py index b1976e8..2d9dce0 100644 --- a/src/tupan/alertas/views.py +++ b/src/tupan/alertas/views.py @@ -59,7 +59,6 @@ def post(self, request, *args, **kwargs): 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) @@ -77,18 +76,13 @@ def get(self, request, id, *args, **kwargs): def put(self, request, id, *args, **kwargs): try: - data = json.loads(request.body) alerta = Alerta.objects.filter(id=id, ativo=True).first() if alerta: - alerta.nome = data.get('nome', alerta.nome) - alerta.condicao = data.get('condicao', alerta.condicao) - alerta.save() - return JsonResponse({ - 'id': alerta.pk, - 'nome': alerta.nome, - 'condicao': alerta.condicao, - 'ativo': alerta.ativo - }, status=200) + serializer = AlertaSerializer(alerta, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse({'error': 'Alerta não encontrado ou inativo'}, status=404) except json.JSONDecodeError: From f6c5fe0ee0f7de1743ab08032a745e423e645094 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Fri, 11 Oct 2024 12:17:32 -0300 Subject: [PATCH 6/6] refa: alterando urls --- src/tupan/alertas/urls.py | 4 ++-- src/tupan/tupan/urls.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tupan/alertas/urls.py b/src/tupan/alertas/urls.py index 3868381..7799ab1 100644 --- a/src/tupan/alertas/urls.py +++ b/src/tupan/alertas/urls.py @@ -2,8 +2,8 @@ from .views import AlertasView, AlertasDetalhesView, HistoricoAlertaView, MedicaoView, MedicaoDetalhesView urlpatterns = [ - path('', AlertasView.as_view(), name='alertas'), - path('/', AlertasDetalhesView.as_view(), name='alertas-detalhes'), + path('alertas', AlertasView.as_view(), name='alertas'), + path('alertas/', AlertasDetalhesView.as_view(), name='alertas-detalhes'), path('historicos', HistoricoAlertaView.as_view(), name='historico-alertas'), path('medicoes', MedicaoView.as_view(), name='medicoes'), path('medicoes/', MedicaoDetalhesView.as_view(), name='medicoes-detalhes') diff --git a/src/tupan/tupan/urls.py b/src/tupan/tupan/urls.py index 89c60c8..b101390 100644 --- a/src/tupan/tupan/urls.py +++ b/src/tupan/tupan/urls.py @@ -23,7 +23,7 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('alertas', include('alertas.urls')), + path('', include('alertas.urls')), path('', include('estacoes.urls')), path('api/schema/', SpectacularAPIView.as_view(), name='schema'), path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),