Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions api/src/scripts/populate_db_gbfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from shared.common.license_utils import assign_license_by_url
from shared.database.database import generate_unique_id, configure_polymorphic_mappers
from shared.database_gen.sqlacodegen_models import Gbfsfeed, Location, Externalid
from shared.notifications.notification_event_service import emit_url_replaced

GBFS_PUBSUB_TOPIC_NAME = "validate-gbfs-feed"

Expand Down Expand Up @@ -108,9 +109,18 @@ def populate_db(self, session, fetch_url=True):
gbfs_feed.operator = row["Name"]
gbfs_feed.provider = row["Name"]
gbfs_feed.operator_url = row["URL"]
gbfs_feed.producer_url = row["Auto-Discovery URL"]
gbfs_feed.auto_discovery_url = row["Auto-Discovery URL"]
old_producer_url = gbfs_feed.producer_url
new_producer_url = row["Auto-Discovery URL"]
gbfs_feed.producer_url = new_producer_url
gbfs_feed.auto_discovery_url = new_producer_url
gbfs_feed.updated_at = datetime.now(pytz.utc)
if not is_new_feed and old_producer_url and old_producer_url != new_producer_url:
emit_url_replaced(
feed_stable_id=stable_id,
old_url=old_producer_url,
new_url=new_producer_url,
source="populate_db_gbfs",
)

if not gbfs_feed.locations: # If locations are empty, create a new location (no overwrite)
country_code = self.get_safe_value(row, "Country Code", "")
Expand Down
20 changes: 20 additions & 0 deletions api/src/scripts/populate_db_gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
Location,
Redirectingid,
)
from shared.notifications.notification_event_service import (
emit_feed_redirected,
emit_url_replaced,
)
from utils.data_utils import set_up_defaults

if TYPE_CHECKING:
Expand Down Expand Up @@ -200,6 +204,14 @@ def process_redirects(self, session: "Session"):
)
# Flush to avoid FK violation
session.flush()
emit_feed_redirected(
source_stable_id=stable_id,
target_stable_id=target_stable_id,
old_url=getattr(feed, "producer_url", None),
new_url=getattr(target_feed, "producer_url", None),
source="populate_db_gtfs",
extra_data={"redirect_comment": comment} if comment else None,
)

def populate_db(self, session: "Session", fetch_url: bool = True):
"""
Expand Down Expand Up @@ -252,7 +264,15 @@ def populate_db(self, session: "Session", fetch_url: bool = True):
feed.note = self.get_safe_value(row, "note", "")
producer_url = self.get_safe_value(row, "urls.direct_download", "")
if "transitfeeds" not in producer_url: # Avoid setting transitfeeds as producer_url
old_producer_url = feed.producer_url
feed.producer_url = producer_url
if not is_new_feed and old_producer_url and old_producer_url != producer_url:
emit_url_replaced(
feed_stable_id=stable_id,
old_url=old_producer_url,
new_url=producer_url,
source="populate_db_gtfs",
)
feed.authentication_type = str(int(float(self.get_safe_value(row, "urls.authentication_type", "0"))))
feed.authentication_info_url = self.get_safe_value(row, "urls.authentication_info", "")
feed.api_key_parameter_name = self.get_safe_value(row, "urls.api_key_parameter_name", "")
Expand Down
6 changes: 6 additions & 0 deletions api/src/shared/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Shared notification utilities.

Packages exported from here:
notification_event_service — emit_feed_redirected / emit_url_replaced
brevo_notification_sender — send_single / send_digest
"""
Loading
Loading