def jour(request):
"""Fonction qui sélectionne les photos des caméras de jour de x minutes avant le lever du soleil à x minutes après le coucher du soleil
On rassemble les photos pour couvrir un nycthémère
On crée autant de listes qu'il y a de caméras de jour
Ici, on a 4 caméras de jour qui sont répertoriées dans la base de données par les identifiants 1, 2, 4, 5
ADAPTER CES IDENTIFIAANTS AUX NUMEROS DE CAMERA ET A LEUR NOMBRE"""
lieu = SunTimes(longitude, latitude, altitude)
maintenant = local_tz.localize(datetime.now()) #voir http://pytz.sourceforge.net/
lever = lieu.riselocal(maintenant)
coucher = lieu.setlocal(maintenant)
coucherHier = lieu.setlocal(maintenant - timedelta(1))
leverHier = lieu.riselocal(maintenant - timedelta(1))
leverDelta = lever - timedelta(minutes=deltaSetRise) # par ex 30 minutes avant lever soleil
coucherDelta = coucher + timedelta(minutes=deltaSetRise) # par ex 30 minuts après coucher soleil
coucherDeltaHier = coucherHier + timedelta(minutes=deltaSetRise)
leverDeltaHier = leverHier - timedelta(minutes=deltaSetRise)
maintenantHier= maintenant - timedelta(1)
if leverDelta <= maintenant <= coucherDelta:
#On est en journée ; on sélectionne les photos allant du lever à maintenant. On sélectionne aussi les photos allant de maintenant - 24 heures à coucher hier
jour_photo_list_1 = Photo.objects.filter(appareil=1).filter(Q(date__gt=leverDelta, date__lt=maintenant) | Q(date__gt=maintenantHier, date__lt=coucherDeltaHier)).order_by('-date')
jour_photo_list_2 = Photo.objects.filter(appareil=2).filter(Q(date__gt=leverDelta, date__lt=maintenant) | Q(date__gt=maintenantHier, date__lt=coucherDeltaHier)).order_by('-date')
jour_photo_list_4 = Photo.objects.filter(appareil=4).filter(Q(date__gt=leverDelta, date__lt=maintenant) | Q(date__gt=maintenantHier, date__lt=coucherDeltaHier)).order_by('-date')
jour_photo_list_5 = Photo.objects.filter(appareil=5).filter(Q(date__gt=leverDelta, date__lt=maintenant) | Q(date__gt=maintenantHier, date__lt=coucherDeltaHier)).order_by('-date')
elif leverDelta > maintenant:
# On est la nuit mais après minuit: on selectionne les photos allant du jour précédent du matin au soir
jour_photo_list_1 = Photo.objects.filter(appareil=1).filter(date__gt=leverDeltaHier, date__lt=coucherDeltaHier).order_by('-date')
jour_photo_list_2 = Photo.objects.filter(appareil=2).filter(date__gt=leverDeltaHier, date__lt=coucherDeltaHier).order_by('-date')
jour_photo_list_4= Photo.objects.filter(appareil=4).filter(date__gt=leverDeltaHier, date__lt=coucherDeltaHier).order_by('-date')
jour_photo_list_5= Photo.objects.filter(appareil=5).filter(date__gt=leverDeltaHier, date__lt=coucherDeltaHier).order_by('-date')
else:
# On est la nuit mais avant minuit : on selectionne les photos du même jour allant du matin au soir
jour_photo_list_1 = Photo.objects.filter(appareil=1).filter(date__gt=leverDelta, date__lt=coucherDelta).order_by('-date')
jour_photo_list_2 = Photo.objects.filter(appareil=2).filter(date__gt=leverDelta, date__lt=coucherDelta).order_by('-date')
jour_photo_list_4 = Photo.objects.filter(appareil=4).filter(date__gt=leverDelta, date__lt=coucherDelta).order_by('-date')
jour_photo_list_5 = Photo.objects.filter(appareil=5).filter(date__gt=leverDelta, date__lt=coucherDelta).order_by('-date')
min_length = min(len(jour_photo_list_1), len(
jour_photo_list_2), len(jour_photo_list_4), len(jour_photo_list_5))
jour_photo_list_group = []
for i in range(min_length):
new_group = [jour_photo_list_1[i], jour_photo_list_2[i], jour_photo_list_4[i], jour_photo_list_5[i]]
jour_photo_list_group.extend(new_group)
context = {
'jour_photo_list_group': jour_photo_list_group,
}
return render(request, "{}/jour.html".format(appli), context)
path('jour/', views.jour, name="jour"),
{% extends "camera/base.html" %}
{% load static %}
{% block header %}
<h1 class="monh1">Photos de Jour</h1>
{% endblock %}
{% block content %}
<table>
<tr>
{% for photo in jour_photo_list_group %}
<td>
<a href="{% static photo.file_photo_jpg %}">
<img class="centre-image imgresponsive" src="{% static photo.file_photo_jpg %}" height="150" width="225" alt="photo non disponible" loading="lazy" /><figcaption>{{photo.name}} - {{photo.appareil}}</figcaption>
</a>
</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"4" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
{% endblock %}