Created a script to update the csv imports that don't have the date
This commit is contained in:
101
update_csv_import_dates.py
Normal file
101
update_csv_import_dates.py
Normal file
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Update the created_at timestamps for CSV-imported leads with the new email receive dates.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import csv
|
||||
from datetime import datetime
|
||||
from sqlalchemy import text, select
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.alpine_bits_python.config_loader import load_config
|
||||
from src.alpine_bits_python.db import Reservation, Customer
|
||||
|
||||
|
||||
async def main():
|
||||
# Load config
|
||||
config = load_config()
|
||||
db_url = config["database"]["url"]
|
||||
schema = config["database"]["schema"]
|
||||
|
||||
# Create async engine
|
||||
engine = create_async_engine(db_url)
|
||||
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.execute(text(f"SET search_path TO {schema}"))
|
||||
|
||||
# Load the CSV with the new dates
|
||||
csv_dates = {}
|
||||
try:
|
||||
with open("leads_export.csv", "r", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
name = row.get("name", "").strip()
|
||||
lastname = row.get("lastname", "").strip()
|
||||
email = row.get("mail", "").strip()
|
||||
received_date = row.get("received_date", "").strip()
|
||||
if email and received_date:
|
||||
# Use email as primary key since it's unique
|
||||
csv_dates[email.lower()] = {
|
||||
"name": name,
|
||||
"lastname": lastname,
|
||||
"received_date": received_date,
|
||||
}
|
||||
except FileNotFoundError:
|
||||
print("ERROR: leads_export.csv not found. Run extract_leads.py first.")
|
||||
return
|
||||
|
||||
print(f"Loaded {len(csv_dates)} date entries from CSV")
|
||||
|
||||
# Fetch CSV-imported reservations
|
||||
async with async_session() as session:
|
||||
async with engine.begin() as conn:
|
||||
await conn.execute(text(f"SET search_path TO {schema}"))
|
||||
|
||||
# Query for CSV imports
|
||||
result = await session.execute(
|
||||
select(Reservation, Customer).join(
|
||||
Customer, Reservation.customer_id == Customer.id
|
||||
).where(Reservation.unique_id.like("csv_%"))
|
||||
)
|
||||
rows = result.all()
|
||||
|
||||
print(f"\nFound {len(rows)} CSV-imported reservations to update")
|
||||
updated = 0
|
||||
failed = 0
|
||||
|
||||
for reservation, customer in rows:
|
||||
email = customer.email_address
|
||||
if email and email.lower() in csv_dates:
|
||||
new_date_str = csv_dates[email.lower()]["received_date"]
|
||||
try:
|
||||
# Parse ISO format date
|
||||
new_date = datetime.fromisoformat(new_date_str)
|
||||
old_date = reservation.created_at
|
||||
print(f" Updating: {customer.given_name} ({email})")
|
||||
print(f" Old: {old_date}")
|
||||
print(f" New: {new_date}")
|
||||
reservation.created_at = new_date
|
||||
updated += 1
|
||||
except ValueError as e:
|
||||
print(f" FAILED to parse date for {email}: {e}")
|
||||
failed += 1
|
||||
elif email:
|
||||
print(f" WARNING: No CSV date found for {customer.given_name} ({email})")
|
||||
|
||||
print(f"\nSummary: {updated} updated, {failed} failed")
|
||||
|
||||
if updated > 0:
|
||||
await session.commit()
|
||||
print("Changes committed to database")
|
||||
else:
|
||||
print("No changes made")
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user