From 2d8025efd61e481b71b14934ee9b044325c326e2 Mon Sep 17 00:00:00 2001 From: Hieu Lam - TMA <135117837+lthanhhieu@users.noreply.github.com> Date: Mon, 25 Mar 2024 13:30:36 +0700 Subject: [PATCH] fix-9127: Add the unique ticket code into the downlad CSV file (#9133) * fix-9127: Add the unique ticket code into the downlad CSV file * fix-9127: Add the unique ticket code into the downlad CSV file * fix-9127: Add the unique ticket code into the downlad CSV file --- app/api/helpers/csv_jobs_util.py | 12 ++++++--- app/api/schema/attendees.py | 1 + app/models/ticket_holder.py | 19 ++++++++++++- app/templates/pdf/attendees_pdf.html | 6 ++++- .../rev-2024-03-23-08:47:20-e7e952b58504_.py | 27 +++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 migrations/versions/rev-2024-03-23-08:47:20-e7e952b58504_.py diff --git a/app/api/helpers/csv_jobs_util.py b/app/api/helpers/csv_jobs_util.py index 18b5b2f711..edf9b3f88e 100644 --- a/app/api/helpers/csv_jobs_util.py +++ b/app/api/helpers/csv_jobs_util.py @@ -82,7 +82,7 @@ def export_orders_csv(orders): return rows -def get_order_ticket_data(order, ticket): +def get_order_ticket_data(attendee, order, ticket): """Get order ticket data""" data = {} if not order: @@ -104,6 +104,12 @@ def get_order_ticket_data(order, ticket): 'City': '', 'Zipcode': '', } + ticketId = '' + if order.identifier: + if not attendee.identifier: + ticketId = order.identifier + '-' + attendee.get_new_identifier() + else: + ticketId = order.identifier + '-' + attendee.identifier data = { 'Order#': str(order.get_invoice_number()), @@ -113,7 +119,7 @@ def get_order_ticket_data(order, ticket): 'Status': str(order.status) if order.status else '-', 'Payment Type': str(order.paid_via) if order.paid_via else '', 'Payment Mode': str(order.payment_mode) if order.payment_mode else '', - 'Ticket ID': str(order.identifier) if order.identifier else '', + 'Ticket ID': ticketId, } if ticket: @@ -162,7 +168,7 @@ def get_ticket_data(ticket): def get_attendee_data(attendee, custom_forms, attendee_form_dict): """Get attendee data from attendee object""" - order_ticket_data = get_order_ticket_data(attendee.order, attendee.ticket) + order_ticket_data = get_order_ticket_data(attendee, attendee.order, attendee.ticket) data = { **order_ticket_data, 'Email': '', diff --git a/app/api/schema/attendees.py b/app/api/schema/attendees.py index 8637d4dba6..f48e1d8f44 100644 --- a/app/api/schema/attendees.py +++ b/app/api/schema/attendees.py @@ -85,6 +85,7 @@ def validate_json(self, data, original_data): is_discount_applied = fields.Boolean(allow_none=True) is_access_code_applied = fields.Boolean(allow_none=True) tag_id = fields.Int(allow_none=True) + identifier = fields.Str(dump_only=True) event = Relationship( self_view='v1.attendee_event', self_view_kwargs={'id': ''}, diff --git a/app/models/ticket_holder.py b/app/models/ticket_holder.py index 1d8f1371a3..d9fa9251cb 100644 --- a/app/models/ticket_holder.py +++ b/app/models/ticket_holder.py @@ -1,4 +1,6 @@ import base64 +import binascii +import os from dataclasses import dataclass from datetime import datetime from io import BytesIO @@ -11,6 +13,11 @@ from app.models.base import SoftDeletionModel +def get_new_id(): + """Generate a new id for a ticket holder.""" + return str(binascii.b2a_hex(os.urandom(3)), 'utf-8') + + @dataclass(init=False, unsafe_hash=True) class TicketHolder(SoftDeletionModel): __tablename__ = "ticket_holders" @@ -90,6 +97,7 @@ class TicketHolder(SoftDeletionModel): is_access_code_applied: bool = db.Column(db.Boolean, default=False) tag_id: int = db.Column(db.Integer, db.ForeignKey('tags.id', ondelete='CASCADE')) tag = db.relationship('Tag', backref='ticket_holders') + identifier = db.Column(db.String, default=get_new_id) @property def name(self): @@ -108,7 +116,11 @@ def qr_code(self): box_size=10, border=0, ) - qr.add_data(self.order.identifier) + identifier = self.identifier + if not self.identifier: + identifier = get_new_id() + + qr.add_data(self.order.identifier + "-" + identifier) qr.make(fit=True) img = qr.make_image() @@ -143,3 +155,8 @@ def pdf_url_path(self) -> str: + self.order.identifier + '.pdf' ) + + @staticmethod + def get_new_identifier(): + """Generate a new identifier for the ticket holder.""" + return str(binascii.b2a_hex(os.urandom(3)), 'utf-8') diff --git a/app/templates/pdf/attendees_pdf.html b/app/templates/pdf/attendees_pdf.html index 71eb0160d2..cc3047eef9 100644 --- a/app/templates/pdf/attendees_pdf.html +++ b/app/templates/pdf/attendees_pdf.html @@ -128,7 +128,11 @@

{{ holders[0].event.name }} - {{ ("Attendees List {% else %} {{ "Free Ticket" }}
{% endif %} - {{ holder.order.identifier }}
+ {% if holder.identifier %} + {{ holder.order.identifier }} - {{ holder.identifier }}
+ {% else %} + {{ holder.order.identifier }} - {{ holder.get_new_identifier()}}
+ {% endif %} {{ holder.ticket.name }}
{% if holder.is_checked_in %} {{ ("Checked In") }}
diff --git a/migrations/versions/rev-2024-03-23-08:47:20-e7e952b58504_.py b/migrations/versions/rev-2024-03-23-08:47:20-e7e952b58504_.py new file mode 100644 index 0000000000..18504c1787 --- /dev/null +++ b/migrations/versions/rev-2024-03-23-08:47:20-e7e952b58504_.py @@ -0,0 +1,27 @@ +"""empty message + +Revision ID: e7e952b58504 +Revises: 414c776ae509 +Create Date: 2024-03-23 08:47:20.614874 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'e7e952b58504' +down_revision = '414c776ae509' + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('ticket_holders', sa.Column('identifier', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('ticket_holders', 'identifier') + # ### end Alembic commands ###