Got db saving working

This commit is contained in:
Jonas Linter
2025-09-29 13:56:34 +02:00
parent 384fb2b558
commit 06739ebea9
21 changed files with 1188 additions and 830 deletions

View File

@@ -5,27 +5,24 @@ import os
Base = declarative_base()
# Async SQLAlchemy setup
def get_database_url(config=None):
db_url = None
if config and 'database' in config and 'url' in config['database']:
db_url = config['database']['url']
if config and "database" in config and "url" in config["database"]:
db_url = config["database"]["url"]
if not db_url:
db_url = os.environ.get('DATABASE_URL')
db_url = os.environ.get("DATABASE_URL")
if not db_url:
db_url = 'sqlite+aiosqlite:///alpinebits.db'
db_url = "sqlite+aiosqlite:///alpinebits.db"
return db_url
DATABASE_URL = get_database_url()
engine = create_async_engine(DATABASE_URL, echo=True)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def get_async_session():
async with AsyncSessionLocal() as session:
yield session
class Customer(Base):
__tablename__ = 'customers'
__tablename__ = "customers"
id = Column(Integer, primary_key=True)
given_name = Column(String)
contact_id = Column(String, unique=True)
@@ -42,13 +39,14 @@ class Customer(Base):
birth_date = Column(String)
language = Column(String)
address_catalog = Column(Boolean) # Added for XML
name_title = Column(String) # Added for XML
reservations = relationship('Reservation', back_populates='customer')
name_title = Column(String) # Added for XML
reservations = relationship("Reservation", back_populates="customer")
class Reservation(Base):
__tablename__ = 'reservations'
__tablename__ = "reservations"
id = Column(Integer, primary_key=True)
customer_id = Column(Integer, ForeignKey('customers.id'))
customer_id = Column(Integer, ForeignKey("customers.id"))
form_id = Column(String, unique=True)
start_date = Column(Date)
end_date = Column(Date)
@@ -70,16 +68,14 @@ class Reservation(Base):
# Add hotel_code and hotel_name for XML
hotel_code = Column(String)
hotel_name = Column(String)
customer = relationship('Customer', back_populates='reservations')
customer = relationship("Customer", back_populates="reservations")
class HashedCustomer(Base):
__tablename__ = 'hashed_customers'
__tablename__ = "hashed_customers"
id = Column(Integer, primary_key=True)
customer_id = Column(Integer)
hashed_email = Column(String)
hashed_phone = Column(String)
hashed_name = Column(String)
redacted_at = Column(DateTime)