Did some things...

This commit is contained in:
Joan
2025-09-15 22:45:46 +02:00
parent d3b4cd7eaa
commit 2bfdb539be
9 changed files with 410 additions and 195 deletions

View File

@@ -21,6 +21,7 @@ def init_db():
channel_id TEXT NOT NULL,
main_message_id INTEGER,
update_message_id INTEGER,
international_shipping INTEGER DEFAULT 0,
active INTEGER DEFAULT 1
)
""")
@@ -67,14 +68,14 @@ def connect_db():
# --- Raffle Management --- (remains the same)
# ... create_raffle, end_raffle, get_raffle, etc. ...
def create_raffle(name, description, price, image_file_id, channel_id):
def create_raffle(name, description, price, image_file_id, channel_id, international_shipping=0):
"""Creates a new raffle in the database."""
conn = connect_db()
cur = conn.cursor()
try:
cur.execute(
"INSERT INTO raffles (name, description, price, image_file_id, channel_id) VALUES (?, ?, ?, ?, ?)",
(name, description, price, image_file_id, channel_id)
"INSERT INTO raffles (name, description, price, image_file_id, channel_id, international_shipping) VALUES (?, ?, ?, ?, ?, ?)",
(name, description, price, image_file_id, channel_id, international_shipping)
)
raffle_id = cur.lastrowid
conn.commit()
@@ -171,12 +172,12 @@ def reserve_number(user_id, user_name, raffle_id, number):
if str(number) not in numbers_list:
numbers_list.append(str(number))
numbers_str = ','.join(sorted(numbers_list))
cur.execute("UPDATE participants SET numbers=? WHERE id=?",
(numbers_str, participant_id))
cur.execute("UPDATE participants SET numbers=?, reservation_timestamp=? WHERE id=?",
(numbers_str, int(time.time()), participant_id))
else:
cur.execute(
"INSERT INTO participants (user_id, user_name, raffle_id, numbers, step) VALUES (?, ?, ?, ?, ?)",
(user_id, user_name, raffle_id, str(number), "waiting_for_payment")
"INSERT INTO participants (user_id, user_name, raffle_id, numbers, step, reservation_timestamp) VALUES (?, ?, ?, ?, ?, ?)",
(user_id, user_name, raffle_id, str(number), "waiting_for_payment", int(time.time()))
)
conn.commit()
except Exception as e:
@@ -185,6 +186,20 @@ def reserve_number(user_id, user_name, raffle_id, number):
finally:
conn.close()
def get_total_participations(raffle_id):
"""Gets the total number of participations (both reserved and confirmed) for a raffle."""
conn = connect_db()
cur = conn.cursor()
try:
cur.execute("SELECT COUNT(*) FROM participants WHERE raffle_id=?", (raffle_id,))
count = cur.fetchone()
return count[0] if count else 0
except Exception as e:
logger.error(f"Error getting total participations for raffle {raffle_id}: {e}")
return 0
finally:
conn.close()
def remove_reserved_number(participant_id, number):
"""Removes a specific number from a 'waiting_for_payment' reservation."""
conn = connect_db()
@@ -215,17 +230,17 @@ def remove_reserved_number(participant_id, number):
finally:
conn.close()
def mark_reservation_pending(participant_id, invoice_id, timestamp):
def mark_reservation_pending(participant_id, invoice_id):
"""Sets the invoice ID and reservation timestamp for a participant moving to pending payment."""
conn = connect_db()
cur = conn.cursor()
try:
cur.execute(
"UPDATE participants SET invoice_id=?, reservation_timestamp=? WHERE id=? AND step='waiting_for_payment'",
(invoice_id, int(timestamp), participant_id)
(invoice_id, int(time.time()), participant_id)
)
conn.commit()
logger.info(f"Marked reservation pending for participant {participant_id} with invoice {invoice_id} at {timestamp}")
logger.info(f"Marked reservation pending for participant {participant_id} with invoice {invoice_id} at {time.time()}")
except Exception as e:
logger.error(f"Error marking reservation pending for participant {participant_id}: {e}")
conn.rollback()
@@ -481,6 +496,19 @@ def get_remaining_numbers_amount(raffle_id):
finally:
conn.close()
def get_confirmed_numbers(user_id, raffle_id):
conn = connect_db()
cur = conn.cursor()
try:
cur.execute("SELECT numbers FROM participants WHERE user_id=? AND raffle_id=? AND step='completed'", (user_id, raffle_id))
numbers = cur.fetchone()
return numbers['numbers'].split(',') if numbers and numbers['numbers'] else []
except Exception as e:
logger.error(f"Error getting confirmed numbers for user {user_id}, raffle {raffle_id}: {e}")
return []
finally:
conn.close()
def get_remaining_numbers(raffle_id):
"""Gets a list of all remaining numbers for a raffle, formatted as two digits."""
conn = connect_db()
@@ -599,3 +627,19 @@ def store_paypal_access_token(access_token, expires_in):
logger.error(f"Error storing PayPal access token in DB: {e}")
finally:
conn.close()
def get_all_invoice_ids(raffle_id):
conn = connect_db()
cur = conn.cursor()
try:
cur.execute(
"SELECT invoice_id FROM participants WHERE raffle_id=? AND step='completed' AND invoice_id IS NOT NULL",
(raffle_id,)
)
rows = cur.fetchall()
return [row['invoice_id'] for row in rows if row['invoice_id']]
except Exception as e:
logger.error(f"Error getting invoice IDs for raffle {raffle_id}: {e}")
return []
finally:
conn.close()