def main(crsid, postgres_settings): logger.info("Creating half price ticket for %s", crsid) logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_syslog_handler() logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) user = users.get_raven_user(crsid, pg=conn) if user is None: conn.commit() lookup_data = lookup.get_crsid(crsid) user = users.new_raven_user(crsid, ["current"], lookup_data, pg=conn) if tickets.tickets(user_id=user["user_id"], expired=False, pg=conn): raise ValueError("User {0} already has some tickets" .format(user["user_id"])) ticket_id, = tickets.buy("standard", True, 1, user=user, pg=conn) with conn.cursor() as cur: cur.execute("UPDATE tickets " "SET waiting_list = FALSE, quota_exempt = TRUE, " " expires = NULL, expires_reason = NULL, " " price = 3450, notes = E'Ents half price ticket\n' " "WHERE ticket_id = %s", (ticket_id, )) conn.commit()
def prepare(postgres_settings, pairs): # This is a hack, to cover up a design flaw. logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) for user_id, ticket_id in pairs: with conn.cursor(True) as cur: cur.execute("SELECT * FROM tickets " "WHERE ticket_id = %s AND user_id = %s", (ticket_id, user_id)) assert cur.rowcount == 1 ticket = cur.fetchone() cur.execute("SELECT * FROM tickets " "WHERE user_id = %s AND surname = %s " "AND othernames = %s AND finalised IS NOT NULL", (user_id, ticket["surname"], ticket["othernames"])) if cur.rowcount != 2: print("Couldn't find standard ticket for", user_id, ticket_id) continue standard_ticket, b = cur.fetchall() if standard_ticket["ticket_id"] == ticket_id: standard_ticket = b if standard_ticket["waiting_list"] or standard_ticket["vip"] \ or not standard_ticket["paid"]: print("Bad standard ticket for", user_id, ticket_id) continue logger.info("Preparing waiting ticket %s to VIP upgrade %s", ticket_id, standard_ticket["ticket_id"], extra={"user_id": user_id}) notes = "Prepared to replace {0} " \ "(VIP upgrade waiting list release) on {1}\n" \ .format(standard_ticket["ticket_id"], datetime.utcnow()) cur.execute("UPDATE tickets " "SET price = 1000, notes = notes || %s " "WHERE ticket_id = %s AND user_id = %s", (notes, ticket_id, user_id)) notes = "Replaced by VIP ticket {0} from waiting list on {1}\n" \ .format(ticket_id, datetime.utcnow()) cur.execute("UPDATE tickets " "SET expires=utcnow(), " " expires_reason='admin-intervention', " " finalised=NULL, paid=NULL, " " notes = notes || %s " "WHERE ticket_id = %s AND user_id = %s", (notes, standard_ticket["ticket_id"], user_id)) conn.commit()
def purge(postgres_settings, pairs): logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) for user_id, ticket_id in pairs: tickets.purge_unpaid(user_id, ticket_id, pg=conn) conn.commit()
def release(postgres_settings, pairs): logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) for user_id, ticket_ids in pairs: tickets.waiting_release(user_id, ticket_ids, ask_pay_within=1, pg=conn) conn.commit()
def main(crsid, postgres_settings): logger.info("Creating raven user for %s", crsid) logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_syslog_handler() logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) user = users.get_raven_user(crsid, pg=conn) if user is None: conn.commit() lookup_data = lookup.get_crsid(crsid) user = users.new_raven_user(crsid, ["current"], lookup_data, pg=conn) conn.commit()
def main(postgres_settings, filename): logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) mailed = set() with open(filename) as f: for line in f: user_id = int(line.strip()) if user_id in mailed: logger.warning("Not double-mailing %s", user_id) continue mailed.add(user_id) send_mail(user_id, pg=conn) conn.commit()
def harass(postgres_settings, filename): logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) harassed = set() with open(filename) as f: for user_id, deadline in csv.reader(f): user_id = int(user_id) if user_id in harassed: logger.warning("Not double-harassing %s", user_id) continue harassed.add(user_id) deadline = datetime.strptime(deadline, "%Y-%m-%d").date() send_update(user_id, harass=True, deadline=deadline, pg=conn) conn.commit()
def main(crsid, postgres_settings): logger.info("Creating free VIP upgrade for %s", crsid) logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_syslog_handler() logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) lookup_data = lookup.get_crsid(crsid) user = users.new_raven_user(crsid, ["current"], lookup_data, pg=conn) ticket_id, = tickets.buy("vip", False, 1, user=user, pg=conn) with conn.cursor() as cur: cur.execute("UPDATE tickets " "SET expires = NULL, expires_reason = NULL, " " price = 6900 " "WHERE ticket_id = %s", (ticket_id, )) logging.info("Free VIP upgraded ticket %s", ticket_id) conn.commit()
def main(postgres_settings): conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_syslog_handler() # logging_setup.add_smtp_handler() crsid = raw_input( "CRSid of the Cambridge student responsible for the ticket: ").strip() if not crsid: sys.exit(1) user = users.get_raven_user(crsid, pg=conn) if user is None: conn.commit() lookup_data = lookup.get_crsid(crsid) user = users.new_raven_user(crsid, ["current"], lookup_data, pg=conn) else: print("They already have an account:") print("Surname:", user["surname"]) print("Othernames:", user["othernames"]) print("College:", utils.college_name(user["college_id"], pg=conn)) print() ts = tickets.tickets(user_id=user["user_id"], pg=conn) if ts: ts.sort(key=lambda t: (t["surname"], t["othernames"], t[ "waiting_list"], not t["vip"])) print("They currently have these tickets:") for ticket in ts: print(" {0}:".format(ticket["ticket_id"]).ljust(4 + 5), ticket["othernames"], ticket["surname"], "(" + types(ticket) + ")") print() # Don't idle in transaction. conn.commit() print("To cancel, press Ctrl-C.") while True: try: num = int(raw_input("Number of tickets: ").strip()) except ValueError: continue else: if 1 <= num <= 200: break while True: ticket_type = raw_input("Standard or VIP: ").strip().lower() if ticket_type in {"vip", "standard"}: break while True: free = raw_input( "Should the tickets be free? [yes|no] ").strip().lower() if free in {"y", "yes"}: free = True break elif free in {"n", "no"}: free = False break notes = raw_input("Comment (stored against the ticket): ").strip() if notes: notes += "\n" notes = "Added using manual_add_unfinalised_ticket.py\n" + notes logger.info("Creating %s %s%s ticket(s)", num, ticket_type, " free" if free else "") ids = tickets.buy(ticket_type, False, num, user=user, quota_exempt=True, pg=conn) with conn.cursor() as cur: cur.execute( "UPDATE tickets " "SET expires = NULL, expires_reason = NULL, " " notes = %s " "WHERE ticket_id in %s", (notes, tuple(ids))) if free: cur.execute("UPDATE tickets SET price = 0 WHERE ticket_id in %s", (tuple(ids), )) conn.commit() print() print( "Done. Next step: inform the user that they should log into the website." ) print("They will see the following message:") print( " You reserved N tickets recently, but are yet to fill out the details on them." ) print(" [Resume order]") print("(there is no time limit)") print("and they can then put the names on their tickets.") print()
root = os.path.join(os.path.dirname(__file__), '..') sys.path.insert(0, root) from snowball_ticketing.tickets.payments import main from snowball_ticketing import logging_setup logging.basicConfig(level=logging.DEBUG) logging_setup.add_smtp_handler() if len(sys.argv) != 4 or \ sys.argv[1] not in ('live', 'test') or \ sys.argv[3] not in ('dry-run', 'confirm'): print("Usage:", sys.argv[0], "live|test", "filename.csv", "dry-run|confirm") else: postgres_settings = { "live": { "dbname": "ticketing" }, "test": { "dbname": "ticketing-test" } }[sys.argv[1]] dry_run = sys.argv[3] == "dry-run" if not dry_run: logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) main(sys.argv[2], dry_run, postgres_settings)
def main(postgres_settings): conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_syslog_handler() # logging_setup.add_smtp_handler() crsid = raw_input("CRSid of the Cambridge student responsible for the ticket: ").strip() if not crsid: sys.exit(1) user = users.get_raven_user(crsid, pg=conn) if user is None: conn.commit() lookup_data = lookup.get_crsid(crsid) user = users.new_raven_user(crsid, ["current"], lookup_data, pg=conn) else: print("They already have an account:") print("Surname:", user["surname"]) print("Othernames:", user["othernames"]) print("College:", utils.college_name(user["college_id"], pg=conn)) print() ts = tickets.tickets(user_id=user["user_id"], pg=conn) if ts: ts.sort(key=lambda t: (t["surname"], t["othernames"], t["waiting_list"], not t["vip"])) print("They currently have these tickets:") for ticket in ts: print(" {0}:".format(ticket["ticket_id"]).ljust(4 + 5), ticket["othernames"], ticket["surname"], "(" + types(ticket) + ")") print() # Don't idle in transaction. conn.commit() print("To cancel, press Ctrl-C.") while True: try: num = int(raw_input("Number of tickets: ").strip()) except ValueError: continue else: if 1 <= num <= 200: break while True: ticket_type = raw_input("Standard or VIP: ").strip().lower() if ticket_type in {"vip", "standard"}: break while True: free = raw_input("Should the tickets be free? [yes|no] ").strip().lower() if free in {"y", "yes"}: free = True break elif free in {"n", "no"}: free = False break notes = raw_input("Comment (stored against the ticket): ").strip() if notes: notes += "\n" notes = "Added using manual_add_unfinalised_ticket.py\n" + notes logger.info("Creating %s %s%s ticket(s)", num, ticket_type, " free" if free else "") ids = tickets.buy(ticket_type, False, num, user=user, quota_exempt=True, pg=conn) with conn.cursor() as cur: cur.execute("UPDATE tickets " "SET expires = NULL, expires_reason = NULL, " " notes = %s " "WHERE ticket_id in %s", (notes, tuple(ids))) if free: cur.execute("UPDATE tickets SET price = 0 WHERE ticket_id in %s", (tuple(ids), )) conn.commit() print() print("Done. Next step: inform the user that they should log into the website.") print("They will see the following message:") print(" You reserved N tickets recently, but are yet to fill out the details on them.") print(" [Resume order]") print("(there is no time limit)") print("and they can then put the names on their tickets.") print()
def prepare(postgres_settings, pairs): # This is a hack, to cover up a design flaw. logging_setup.add_syslog_handler() logging_setup.add_postgresql_handler(postgres_settings) logging_setup.add_smtp_handler() conn = psycopg2.connect(connection_factory=utils.PostgreSQLConnection, **postgres_settings) for user_id, ticket_id in pairs: with conn.cursor(True) as cur: cur.execute( "SELECT * FROM tickets " "WHERE ticket_id = %s AND user_id = %s", (ticket_id, user_id)) assert cur.rowcount == 1 ticket = cur.fetchone() cur.execute( "SELECT * FROM tickets " "WHERE user_id = %s AND surname = %s " "AND othernames = %s AND finalised IS NOT NULL", (user_id, ticket["surname"], ticket["othernames"])) if cur.rowcount != 2: print("Couldn't find standard ticket for", user_id, ticket_id) continue standard_ticket, b = cur.fetchall() if standard_ticket["ticket_id"] == ticket_id: standard_ticket = b if standard_ticket["waiting_list"] or standard_ticket["vip"] \ or not standard_ticket["paid"]: print("Bad standard ticket for", user_id, ticket_id) continue logger.info("Preparing waiting ticket %s to VIP upgrade %s", ticket_id, standard_ticket["ticket_id"], extra={"user_id": user_id}) notes = "Prepared to replace {0} " \ "(VIP upgrade waiting list release) on {1}\n" \ .format(standard_ticket["ticket_id"], datetime.utcnow()) cur.execute( "UPDATE tickets " "SET price = 1000, notes = notes || %s " "WHERE ticket_id = %s AND user_id = %s", (notes, ticket_id, user_id)) notes = "Replaced by VIP ticket {0} from waiting list on {1}\n" \ .format(ticket_id, datetime.utcnow()) cur.execute( "UPDATE tickets " "SET expires=utcnow(), " " expires_reason='admin-intervention', " " finalised=NULL, paid=NULL, " " notes = notes || %s " "WHERE ticket_id = %s AND user_id = %s", (notes, standard_ticket["ticket_id"], user_id)) conn.commit()
import sys import os import logging root = os.path.join(os.path.dirname(__file__), '..') sys.path.insert(0, root) from snowball_ticketing.tickets.receipt import receipts_main from snowball_ticketing import logging_setup postgres = {"database": "ticketing"} logging_setup.add_postgresql_handler(postgres) logging_setup.add_syslog_handler() logging_setup.add_smtp_handler() receipts_main(postgres)