I have done this application in order to send e-mails asynchronously with showing a graph of temperature's values because I haven't succeeded in adding a thread . To explain my code :
-1 the function foo() retus a table containing temperature sensor's values
-2 conf retus the frequency of getting the value from a configuration form .
from django.shortcuts import render, render_to_response
from django.core.context_processors import csrf
from choix.forms import ConfigurationForm
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from choix.models import Configuration
from django import forms
from rasp import foo
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
l = foo()
subject = request.POST.get('subject', 'subject')
message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ')
from_email = request.POST.get('from_email', '*******@gmail.com')
for i in range(len(l)):
if l[i] > 14:
if subject and message and from_email:
try:
send_mail(subject, message, from_email, [ '**********@gmail.com' ])
retu fonc(request)
except BadHeaderError:
retu HttpResponse('Invalid header found.')
retu HttpResponseRedirect('mail')
else:
retu HttpResponse('Make sure all fields are entered and valid.')
retu fonc(request)
def fonc(request):
conf=Configuration.objects.all()
context={ 'confs':conf , 't':foo() }
retu render(request, 'index1.html', context)
