Preserve last_modified and etag fields after reloading GTFS Feeds

This commit is contained in:
Emmy D'Anello 2024-05-12 17:11:54 +02:00
parent c60a105b2d
commit 0d622302ac
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 20 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.db.models.signals import pre_save
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -6,3 +7,8 @@ class TrainvelGTFSConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField" default_auto_field = "django.db.models.BigAutoField"
name = "trainvel.gtfs" name = "trainvel.gtfs"
verbose_name = _("Trainvel - GTFS") verbose_name = _("Trainvel - GTFS")
def ready(self):
from trainvel.gtfs import signals
pre_save.connect(signals.keep_gtfs_feed_modification_date, sender="gtfs.GTFSFeed")

14
trainvel/gtfs/signals.py Normal file
View File

@ -0,0 +1,14 @@
from trainvel.gtfs.models import GTFSFeed
def keep_gtfs_feed_modification_date(instance: GTFSFeed, raw: bool = True, **_kwargs):
"""
Keep the last_modified and etag fields from the existing GTFSFeed instance when saving a new one.
"""
if raw and GTFSFeed.objects.filter(pk=instance.pk).exists():
# If a GTFSFeed instance is being saved from a raw query,
# we want to keep the last_modified and etag fields from the existing instance.
# This is useful when loading initial data from a fixture, for example.
old_instance = GTFSFeed.objects.get(pk=instance.pk)
instance.last_modified = old_instance.last_modified
instance.etag = old_instance.etag