Professional Documents
Culture Documents
urls et views mon projet
urls et views mon projet
import datetime
import token
def test(request):
return HttpResponse('hello world')
def dht_tab(request):
tab = Dht11.objects.all()
s = {'tables': tab}
return render(request, 'tables.html',context=s)
def download_csv(request):
model_values = Dht11.objects.all()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="dht.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'temp', 'hum', 'dt'])
liste = model_values.values_list('id', 'temp', 'hum', 'dt')
for row in liste:
writer.writerow(row)
return response
def graphique():
return None
def chart_data_jour(request):
dht = Dht11.objects.all()
now = timezone.now()
last_24_hours = now - timezone.timedelta(hours=24)
dht = Dht11.objects.filter(dt__range=(last_24_hours, now))
data = {
'temps': [Dt.dt for Dt in dht],
'temperature': [Temp.temp for Temp in dht],
'humidity': [Hum.hum for Hum in dht]
}
return JsonResponse(data)
dht = Dht11.objects.filter(dt__gte=date_debut_mois)
data = {
'temps': [Dt.dt for Dt in dht],
'temperature': [Temp.temp for Temp in dht],
'humidity': [Hum.hum for Hum in dht]
}
return JsonResponse(data)
def telegram_alert (request, message):
async def send_telegram_message (token, chat_id, message_text):
bot = Bot (token=token)
await bot.send_message (chat_id=chat_id, text=message_text)
# Replace 'YOUR_BOT_TOKEN', 'YOUR_CHAT_ID', and "YOUR_MESSAGE' with
your actual
bot_token = '6907613091:AAGKCNxgrGT1tRMFhIpJbqsoIWvZm4an5oA'
chat_id = '6920063447'
message_text = message
# Run the asynchronous code within an event loop
asyncio.run (send_telegram_message (bot_token, chat_id, message_text))
path("api",api.dhtser,name='json'),
path('data/',views.dht_tab,name = 'Data'),
path('download_csv/', views.download_csv, name='download_csv'),
path('index/',views.table,name='table'),
path('myChart/',views.graphique,name='myChart'),
path ('chart-data/',views.chart_data, name='chart-data'),
path('chart-data-jour/',views.chart_data_jour,name='chart-data-
jour'),
path('chart-data-
semaine/',views.chart_data_semaine,name='chart-data-semaine'),
path('chart-data-mois/',views.chart_data_mois,name='chart-data-
mois'),