|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
from django.http import JsonResponse, HttpResponse
|
|
|
from django.shortcuts import render, redirect
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
from .forms import BasicPlantForm
|
|
|
from .models import BasicPlant, Temperature, Moisture
|
|
|
|
|
|
|
|
|
# 🌱 Startseite – zeigt z.B. allgemeine Infos oder Dashboard (noch leer)
|
|
|
def startpage(request):
|
|
|
return render(request, "startpage.html")
|
|
|
|
|
|
|
|
|
# 🌿 Pflanzenliste – alle Pflanzen anzeigen
|
|
|
def plant_list(request):
|
|
|
plants = BasicPlant.objects.all()
|
|
|
return render(request, "plant_list.html", {"plants": plants})
|
|
|
|
|
|
|
|
|
# 🌱 Pflanze anlegen
|
|
|
def plant_create(request):
|
|
|
if request.method == 'POST':
|
|
|
form = BasicPlantForm(request.POST)
|
|
|
if form.is_valid():
|
|
|
plant = form.save()
|
|
|
return redirect('plant_detail', plant_id=plant.pk)
|
|
|
else:
|
|
|
form = BasicPlantForm()
|
|
|
return render(request, 'plant_create.html', {'form': form})
|
|
|
|
|
|
|
|
|
# 🌿 Pflanzendetailseite
|
|
|
def plant_page(request, plant_id):
|
|
|
try:
|
|
|
plant = BasicPlant.objects.get(id=plant_id)
|
|
|
return render(request, "plant.html", {"plant": plant})
|
|
|
except ObjectDoesNotExist:
|
|
|
return redirect("plant_list")
|
|
|
|
|
|
|
|
|
# 🌡️ Temporäre Test-Seite (z.B. für Sensor-Entwicklung)
|
|
|
def dataPage01(request):
|
|
|
plant = BasicPlant.objects.get(id=1)
|
|
|
temps = Temperature.objects.filter(plant_id=1).all()
|
|
|
moists = Moisture.objects.filter(plant_id=1).all()
|
|
|
return render(request, "dataPage.html", {"plant": plant, "temps": temps, "moists": moists})
|
|
|
|
|
|
|
|
|
# 📡 Temperatur-Daten-API
|
|
|
@csrf_exempt
|
|
|
def data_temp(request):
|
|
|
if request.method == "POST":
|
|
|
p = BasicPlant.objects.get(id=request.POST["plant_id"])
|
|
|
Temperature.objects.create(temp=request.POST["temp"], plant_id=p)
|
|
|
return HttpResponse("data received")
|
|
|
return redirect("/data/")
|
|
|
|
|
|
|
|
|
# 📡 Feuchtigkeits-Daten-API
|
|
|
@csrf_exempt
|
|
|
def data_moist(request):
|
|
|
if request.method == "POST":
|
|
|
p = BasicPlant.objects.get(id=request.POST["plant_id"])
|
|
|
Moisture.objects.create(moisture=request.POST["moist"], plant_id=p)
|
|
|
return HttpResponse("data received")
|
|
|
return redirect("/data/")
|
|
|
|
|
|
|
|
|
# 📡 Kombinierte Daten-API
|
|
|
@csrf_exempt
|
|
|
def data_moist_temp(request):
|
|
|
if request.method == "POST":
|
|
|
p = BasicPlant.objects.get(id=request.POST["plant_id"])
|
|
|
Temperature.objects.create(temp=request.POST["temp"], plant_id=p)
|
|
|
Moisture.objects.create(moisture=request.POST["moist"], plant_id=p)
|
|
|
return HttpResponse("data received")
|
|
|
return redirect("/data/")
|