Better pass-midnight management

This commit is contained in:
Emmy D'Anello 2024-01-27 11:43:01 +01:00
parent 1192e89a01
commit 8619553a30
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
6 changed files with 37 additions and 38 deletions

View File

@ -39,9 +39,9 @@ class TripAdmin(admin.ModelAdmin):
@admin.register(StopTime)
class StopTimeAdmin(admin.ModelAdmin):
list_display = ('trip', 'stop', 'arrival_time', 'departure_time', 'arrival_next_day',
list_display = ('trip', 'stop', 'arrival_time', 'departure_time',
'stop_sequence', 'pickup_type', 'drop_off_type',)
list_filter = ('arrival_next_day', 'pickup_type', 'drop_off_type', 'trip__service__transport_type',)
list_filter = ('pickup_type', 'drop_off_type', 'trip__service__transport_type',)
search_fields = ('trip__id', 'stop__name', 'arrival_time', 'departure_time',)
ordering = ('trip', 'stop_sequence',)
autocomplete_fields = ('trip', 'stop',)

View File

@ -1,5 +1,5 @@
import csv
from datetime import datetime
from datetime import datetime, timedelta
from io import BytesIO
from zipfile import ZipFile
@ -260,24 +260,15 @@ class Command(BaseCommand):
for stop_time_dict in csv.DictReader(zipfile.read("stop_times.txt").decode().splitlines()):
stop_time_dict: dict
arrival_next_day = False
arrival_time = stop_time_dict['arrival_time']
if int(arrival_time.split(":")[0]) >= 24:
split = arrival_time.split(':')
arrival_time = f"{int(split[0]) - 24:02}:{split[1]}:{split[2]}"
arrival_next_day = True
departure_time = stop_time_dict['departure_time']
if int(departure_time.split(":")[0]) >= 24:
split = departure_time.split(':')
departure_time = f"{int(split[0]) - 24:02}:{split[1]}:{split[2]}"
arrival_next_day = True
arr_time = stop_time_dict['arrival_time']
arr_time = int(arr_time[:2]) * 3600 + int(arr_time[3:5]) * 60 + int(arr_time[6:])
dep_time = stop_time_dict['departure_time']
dep_time = int(dep_time[:2]) * 3600 + int(dep_time[3:5]) * 60 + int(dep_time[6:])
st = StopTime(
id=f"{stop_time_dict['trip_id']}-{stop_time_dict['stop_sequence']}",
trip_id=stop_time_dict['trip_id'],
arrival_time=arrival_time,
departure_time=departure_time,
arrival_next_day=arrival_next_day,
arrival_time=timedelta(seconds=arr_time),
departure_time=timedelta(seconds=dep_time),
stop_id=stop_time_dict['stop_id'],
stop_sequence=stop_time_dict['stop_sequence'],
stop_headsign=stop_time_dict['stop_headsign'],
@ -291,7 +282,7 @@ class Command(BaseCommand):
StopTime.objects.bulk_create(stop_times,
update_conflicts=True,
update_fields=['stop_id', 'arrival_time', 'departure_time',
'arrival_next_day', 'stop_headsign', 'pickup_type',
'stop_headsign', 'pickup_type',
'drop_off_type', 'timepoint'],
unique_fields=['id'])
stop_times.clear()
@ -299,7 +290,7 @@ class Command(BaseCommand):
StopTime.objects.bulk_create(stop_times,
update_conflicts=True,
update_fields=['stop_id', 'arrival_time', 'departure_time',
'arrival_next_day', 'stop_headsign', 'pickup_type',
'stop_headsign', 'pickup_type',
'drop_off_type', 'timepoint'],
unique_fields=['id'])
stop_times.clear()

View File

@ -1,4 +1,4 @@
# Generated by Django 5.0.1 on 2024-01-27 09:09
# Generated by Django 5.0.1 on 2024-01-27 10:29
import django.db.models.deletion
from django.db import migrations, models
@ -506,12 +506,8 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("arrival_time", models.TimeField(verbose_name="Arrival time")),
("departure_time", models.TimeField(verbose_name="Departure time")),
(
"arrival_next_day",
models.BooleanField(default=False, verbose_name="Arrival next day"),
),
("arrival_time", models.DurationField(verbose_name="Arrival time")),
("departure_time", models.DurationField(verbose_name="Departure time")),
("stop_sequence", models.IntegerField(verbose_name="Stop sequence")),
(
"stop_headsign",

View File

@ -347,19 +347,14 @@ class StopTime(models.Model):
related_name="stop_times",
)
arrival_time = models.TimeField(
arrival_time = models.DurationField(
verbose_name=_("Arrival time"),
)
departure_time = models.TimeField(
departure_time = models.DurationField(
verbose_name=_("Departure time"),
)
arrival_next_day = models.BooleanField(
verbose_name=_("Arrival next day"),
default=False,
)
stop = models.ForeignKey(
to="Stop",
on_delete=models.CASCADE,
@ -397,6 +392,20 @@ class StopTime(models.Model):
null=True,
)
@property
def pretty_arrival_time(self):
seconds = self.arrival_time.total_seconds()
hours = int(seconds // 3600) % 24
minutes = int((seconds % 3600) // 60)
return f"{hours:02}:{minutes:02}"
@property
def pretty_departure_time(self):
seconds = self.departure_time.total_seconds()
hours = int(seconds // 3600) % 24
minutes = int((seconds % 3600) // 60)
return f"{hours:02}:{minutes:02}"
def __str__(self):
return f"{self.trip.route.long_name} - {self.trip_id} - {self.stop.name}"

View File

@ -41,10 +41,10 @@
<tbody>
{% for departure in next_departures %}
<tr>
<td>{{ departure.departure_time }}</td>
<td>{{ departure.pretty_departure_time }}</td>
<td>
<h3>{{ departure.trip.destination }}</h3>
{% for trip_stop in departure.trip.stop_times.all %}{% if trip_stop.stop_sequence > departure.stop_sequence %}{% if trip_stop.drop_off_type == 0 %}{{ trip_stop.stop.name }}{% if trip_stop.pickup_type == 0 %}, {% endif %}{% endif %}{% endif %}{% endfor %}
{% for trip_stop in departure.trip.stop_times.all %}{% if trip_stop.stop_sequence > departure.stop_sequence %}{% if trip_stop.drop_off_type == 0 %}{{ trip_stop.stop.name }}{% if trip_stop.stop.name != departure.trip.destination %}, {% endif %}{% endif %}{% endif %}{% endfor %}
</td>
</tr>
{% endfor %}

View File

@ -1,4 +1,4 @@
from datetime import date, datetime, time
from datetime import date, datetime, time, timedelta
from django.db.models import Q
from django.views.generic import TemplateView
@ -11,7 +11,7 @@ class GareView(TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
stop = Stop.objects.get(name__iexact=kwargs["gare"], location_type=1)
stop = Stop.objects.filter(name__iexact=kwargs["gare"], location_type=1).first()
context['stop'] = stop
next_departures = []
@ -27,6 +27,9 @@ class GareView(TemplateView):
context['query_date'] = query_date
context['query_time'] = query_time
query_time = query_time.isoformat(timespec='seconds')
query_time = timedelta(seconds=int(query_time[:2]) * 3600 + int(query_time[3:5]) * 60 + int(query_time[6:]))
qs = StopTime.objects.none()
for child in Stop.objects.filter(name__iexact=kwargs["gare"], location_type=0).all():
qs_child = StopTime.objects.filter(stop=child, pickup_type=0, departure_time__gte=query_time)