Skip to content

Commit

Permalink
Add ticket_type and order_id to the Attendee model
Browse files Browse the repository at this point in the history
Signed-off-by: Nabarun Pal <pal.nabarun95@gmail.com>
  • Loading branch information
palnabarun committed Sep 27, 2020
1 parent 90b83a7 commit e738f77
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
21 changes: 17 additions & 4 deletions badges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -60,8 +62,16 @@ def __repr__(self) -> str:
return "<Attendee {}>".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()

Expand Down Expand Up @@ -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
43 changes: 43 additions & 0 deletions badges/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import random

from badges.models import Attendee

Expand Down Expand Up @@ -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"],
)
Expand All @@ -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)

3 comments on commit e738f77

@SaptakS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@palnabarun you didn't add the migration file related to the model changes.

@palnabarun
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SaptakS -- Aha! 😞

Pushing those.

@palnabarun
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch.

Please sign in to comment.