import json
import os
from django.conf import settings
from django.template import Context
from django.template.loader import get_template
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from datetime import datetime
from django.core.mail import EmailMultiAlternatives

from fijos.models import AltaSensoresFijos
from .models import sensormovil
from .forms import Formsensoresm

from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from django.http.response import JsonResponse
from django.views import View

# Create your views here.



## sensor movil
@csrf_exempt
def sensoresm(request):
    if request.method == "POST":
        form = Formsensoresm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return render(request, "sensores.html")
        else:
            for field, items in form.errors.items():
                for item in items:
                    messages.error(request, '{}: {}'.format(field, item))
    
    form = Formsensoresm()
    datos = sensormovil.objects.all()
    sensores = AltaSensoresFijos.objects.all()
    return render(request, "sensoresm.html", {"form": form, "datos": datos, "menus": sensores})