From e738f7742c133c78b89869af1a0f8721380c89d2 Mon Sep 17 00:00:00 2001 From: Nabarun Pal Date: Mon, 28 Sep 2020 00:27:15 +0530 Subject: [PATCH] Add ticket_type and order_id to the Attendee model Signed-off-by: Nabarun Pal --- badges/models.py | 21 +++++++++++++++++---- badges/utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/badges/models.py b/badges/models.py index 38c3da6..eb8a684 100644 --- a/badges/models.py +++ b/badges/models.py @@ -35,7 +35,9 @@ class Attendee(db.Model): username = db.Column(TEXT, index=True) twitter_id = db.Column(TEXT) about = db.Column(TEXT) + ticket_type = db.Column(TEXT) type = db.Column(TEXT, default="attendee") + order_id = db.Column(TEXT, nullable=False) @property def id(self) -> str: @@ -60,8 +62,16 @@ def __repr__(self) -> str: return "".format(self.uuid) @classmethod - def create(cls, booking_id: int, email: str, fullname: str) -> Attendee: - a = Attendee(booking_id=booking_id, email=email, fullname=fullname) + def create( + cls, booking_id: int, order_id: int, email: str, fullname: str, ticket_type: str + ) -> Attendee: + a = Attendee( + booking_id=booking_id, + order_id=order_id, + ticket_type=ticket_type, + email=email, + fullname=fullname, + ) db.session.add(a) db.session.commit() @@ -99,9 +109,12 @@ def find_by_booking_id(cls, booking_id: str) -> Union[Attendee, None]: return cls.query.filter_by(booking_id=booking_id).first() @classmethod - def verify(cls, booking_id: int, token: str) -> bool: + def verify(cls, booking_id: int, *, order_id: int = 0, token: str = "") -> bool: a = cls.find_by_booking_id(booking_id=booking_id) - if a and str(a.token) == token: + if a and token and str(a.token) == token: + return True + + if a and order_id or a.order_id == order_id: return True return False diff --git a/badges/utils.py b/badges/utils.py index a35b41f..83c258e 100644 --- a/badges/utils.py +++ b/badges/utils.py @@ -1,4 +1,5 @@ import csv +import random from badges.models import Attendee @@ -39,6 +40,8 @@ def bulk_insert_attendees(csv_path: str): for row in rows: Attendee.create( booking_id=row["booking_id"], + order_id=row["order_id"], + ticket_type=row.get("ticket_type", ""), email=row["email"], fullname=row["fullname"], ) @@ -57,3 +60,43 @@ def bulk_export_attendee_tokens(csv_path: str): "token": attendee.token, } ) + + +def bulk_insert_volunteer_spec(import_path: str, export_path: str): + no_bookings = [] + booking_ids = [] + with open(import_path) as f: + rows = csv.DictReader(f, delimiter=",") + + for row in rows: + a = Attendee.find_by_email(row["email"]) + if not a: + no_bookings.append(row) + continue + + Attendee.create( + booking_id=row["booking_id"], + receipt_id=random.randint(1000, 9999), + email=row["email"], + fullname=row["fullname"], + ) + + booking_ids.append(row["booking_id"]) + + with open(export_path, "w") as f: + writer = csv.DictWriter(f, fieldnames=["booking_id", "email", "receipt_id"]) + + writer.writeheader() + for attendee in Attendee.query.filter(Attendee.booking_id.in_(booking_ids)): + writer.writerow( + { + "booking_id": attendee.booking_id, + "email": attendee.email, + "receipt_id": attendee.receipt_id, + } + ) + + if no_bookings: + print("The following volunteers have no bookings") + for v in no_bookings: + print(v)