66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
import argparse
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from meal_manager.main import engine
|
|
from meal_manager.models import Event, Registration, Subscription
|
|
|
|
|
|
def apply_subscriptions():
|
|
parser = argparse.ArgumentParser(description="Apply subscriptions for an event")
|
|
parser.add_argument("event_id", type=int, help="Event ID (required)")
|
|
parser.add_argument(
|
|
"--dry-run", action="store_true", help="Run without making changes"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Access the arguments
|
|
event_id = args.event_id
|
|
dry_run = args.dry_run
|
|
|
|
with Session(engine) as session:
|
|
subscriptions = session.scalars(select(Subscription)).all()
|
|
event = session.scalars(select(Event).where(Event.id == event_id)).one()
|
|
|
|
if dry_run:
|
|
print(f"DRY RUN: Would process event {event_id}")
|
|
else:
|
|
print(f"Processing event {event_id}")
|
|
|
|
print(f"There are {len(subscriptions)} subscriptions to process")
|
|
relevant_subscriptions = [
|
|
s for s in subscriptions if s.valid_on(event.event_time.date())
|
|
]
|
|
print(f"{len(relevant_subscriptions)} of them are relevant to this event")
|
|
|
|
for subscription in relevant_subscriptions:
|
|
existing_registration = session.scalars(
|
|
select(Registration).where(
|
|
Registration.event_id == event.id,
|
|
Registration.household_id == subscription.household_id,
|
|
)
|
|
).one_or_none()
|
|
if existing_registration:
|
|
print(
|
|
f"There is already a registration for {subscription.household.name}. Skipping subscription."
|
|
)
|
|
continue
|
|
reg = Registration(
|
|
event_id=event.id,
|
|
household_id=subscription.household_id,
|
|
num_adult_meals=subscription.num_adult_meals,
|
|
num_children_meals=subscription.num_children_meals,
|
|
num_small_children_meals=subscription.num_small_children_meals,
|
|
comment="Dauerhafte Anmeldung",
|
|
)
|
|
if dry_run:
|
|
print(f"DRY RUN: Would register {subscription.household.name}")
|
|
else:
|
|
session.add(reg)
|
|
print(f"Registered {subscription.household.name}")
|
|
|
|
if not dry_run:
|
|
session.commit()
|