googlecodelabs / tools

Codelabs management & hosting tools
Apache License 2.0
4.23k stars 1.14k forks source link

ChatGPT 7.0 AURORA #909

Open felipeliliti opened 3 months ago

felipeliliti commented 3 months ago

Instale o Django e o Django REST Framework:

pip install django djangorestframework

2. Criação do Projeto e do Aplicativo Django

Crie um novo projeto Django:

django-admin startproject floodguard
cd floodguard

Crie um novo aplicativo Django:

python manage.py startapp monitoring

3. Estrutura do Projeto

floodguard/settings.py: Adicione rest_framework e monitoring aos INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'monitoring',
]

floodguard/urls.py: Inclua as URLs do aplicativo monitoring:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('monitoring.urls')),
]

4. Modelo de Dados

monitoring/models.py: Defina um modelo para os dados dos sensores e previsões:

from django.db import models

class SensorData(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    location = models.CharField(max_length=255)
    water_level = models.FloatField()
    precipitation = models.FloatField()
    humidity = models.FloatField()

class FloodAlert(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    location = models.CharField(max_length=255)
    alert_level = models.CharField(max_length=50)
    message = models.TextField()

monitoring/admin.py: Registre os modelos para administração:

from django.contrib import admin
from .models import SensorData, FloodAlert

admin.site.register(SensorData)
admin.site.register(FloodAlert)

5. Serializadores e Views

monitoring/serializers.py: Crie serializadores para os modelos:

from rest_framework import serializers
from .models import SensorData, FloodAlert

class SensorDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = SensorData
        fields = '__all__'

class FloodAlertSerializer(serializers.ModelSerializer):
    class Meta:
        model = FloodAlert
        fields = '__all__'

monitoring/views.py: Implemente as views para acessar os dados via API:

from rest_framework import viewsets
from .models import SensorData, FloodAlert
from .serializers import SensorDataSerializer, FloodAlertSerializer

class SensorDataViewSet(viewsets.ModelViewSet):
    queryset = SensorData.objects.all()
    serializer_class = SensorDataSerializer

class FloodAlertViewSet(viewsets.ModelViewSet):
    queryset = FloodAlert.objects.all()
    serializer_class = FloodAlertSerializer

monitoring/urls.py: Configure as URLs do API:

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import SensorDataViewSet, FloodAlertViewSet

router = DefaultRouter()
router.register(r'sensor-data', SensorDataViewSet)
router.register(r'flood-alerts', FloodAlertViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

6. Configuração da Base de Dados

floodguard/settings.py: Configure a base de dados, por exemplo, usando SQLite (por padrão) ou outra base de dados como PostgreSQL se preferir.

7. Migrações e Inicialização

Execute as migrações:

python manage.py makemigrations
python manage.py migrate

Crie um superusuário para acessar o admin:

python manage.py createsuperuser

Inicie o servidor de desenvolvimento:

python manage.py runserver

8. Teste o Projeto

Acesse o painel de administração em http://127.0.0.1:8000/admin e a API em http://127.0.0.1:8000/api/.