Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:ticket pdf associated with attendee id #7400

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 14 additions & 53 deletions app/api/custom/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,36 @@
ticket_blueprint = Blueprint('ticket_blueprint', __name__, url_prefix='/v1/tickets')


@ticket_blueprint.route('/<string:order_identifier>')
@order_blueprint.route('/<string:order_identifier>/tickets-pdf')
@ticket_blueprint.route('/<int:attendee_id>')
@order_blueprint.route('/<int:attendee_id>/tickets-pdf')
@jwt_required
def ticket_attendee_authorized(order_identifier):
def ticket_attendee_authorized(attendee_id):
if current_user:
try:
order = Order.query.filter_by(identifier=order_identifier).first()
order = Order.query.filter_by(user_id=attendee_id).first()
Copy link
Member

Choose a reason for hiding this comment

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

Not really. As I already told you, Order.user_id != Attendee.id

Attendee == TicketHolder

Attendee != Order.user

You need to understand what is Ticket Buyer and what is an attendee or a ticket holder. Then understand why the issue was raised and what is the actual problem. We discussed this in meeting as well. And then think if the changes actually solve the problem

Copy link
Contributor Author

@codedsun codedsun Nov 3, 2020

Choose a reason for hiding this comment

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

this is my fault, I missed to change this. Will update the PR. Does the rest of the PR seems OK or not? @iamareebjamal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ticket Buyer is the person who is ordering the ticket
Ticket Holder = Can be ticket buyer or some other person

Issue was raised because on frontend download tickets, a random ticket was downloaded - to solve this we will have to save ticket attendee wise.

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why you removed the code. Why was it necessary. How is it compatible with previous behaviour?

Copy link
Contributor Author

@codedsun codedsun Nov 3, 2020

Choose a reason for hiding this comment

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

I removed the code as the ticket pdf is now created and saved attendee wise so the ticket path needs to be changed for every ticket holder. The same common code is now moved to create pdf function.

Previously the ticket pdf path seems to be constant using order_identifier but now it needs to be ticketholder wise @iamareebjamal

Copy link
Member

Choose a reason for hiding this comment

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

It still wasn't constant. It changed with order. Now it changes with ticket holder. I understand that. But you removed the code of sending the attachments and other things. We need that. It should be adapted to recent changes

Copy link
Contributor Author

@codedsun codedsun Nov 3, 2020

Choose a reason for hiding this comment

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

I have a question, shall the invoice path and ticket path then be sent as attatchement to the function as earlier? If yes then I have to loop for the ticket path from everywhere where the function is used or should I add a parameter attachment in function and append that function parameter value to the invoice, ticket_path, ?

Copy link
Contributor Author

@codedsun codedsun Nov 3, 2020

Choose a reason for hiding this comment

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

Also, I think that send_Email function should not have this code as it's a generic function? What are your views

for holder in order.ticket_holders:

@iamareebjamal

except NoResultFound:
raise NotFoundError(
{'source': ''}, 'This ticket is not associated with any order'
)
raise NotFoundError({'source': ''}, 'No Ticket found for attendee')
if (
has_access(
'is_coorganizer_or_user_itself',
event_id=order.event_id,
user_id=order.user_id,
user_id=attendee_id,
)
or order.is_attendee(current_user)
):
key = UPLOAD_PATHS['pdf']['tickets_all'].format(identifier=order_identifier)
key = UPLOAD_PATHS['pdf']['tickets_all'].format(
identifier=order.identifier, attendee_identifier=attendee_id
)
file_path = (
'../generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ order.identifier
Copy link
Member

Choose a reason for hiding this comment

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

How is this attendee? This is an order

+ '.pdf'
)
try:
return return_file('ticket', file_path, order_identifier)
return return_file('ticket', file_path, order.identifier)
except FileNotFoundError:
create_pdf_tickets_for_holder(order)
return return_file('ticket', file_path, order_identifier)
return return_file('ticket', file_path, order.identifier)
else:
raise ForbiddenError({'source': ''}, 'Unauthorized Access')
else:
Expand All @@ -85,27 +85,9 @@ def resend_emails():
order = safe_query(Order, 'identifier', order_identifier, 'identifier')
if has_access('is_coorganizer', event_id=order.event_id):
if order.status == 'completed' or order.status == 'placed':
# fetch tickets attachment
order_identifier = order.identifier
key = UPLOAD_PATHS['pdf']['tickets_all'].format(identifier=order_identifier)
ticket_path = (
'generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)
key = UPLOAD_PATHS['pdf']['order'].format(identifier=order_identifier)
invoice_path = (
'generated/invoices/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

# send email.
send_email_to_attendees(
order=order,
purchaser_id=current_user.id,
attachments=[ticket_path, invoice_path],
)
send_email_to_attendees(order=order, purchaser_id=current_user.id)
return jsonify(
status=True,
message="Verification emails for order : {} has been sent successfully".format(
Expand Down Expand Up @@ -327,28 +309,7 @@ def complete_order(order_id):
if (order.status == 'completed' or order.status == 'placed') and (
order.deleted_at is None
):
order_identifier = order.identifier

key = UPLOAD_PATHS['pdf']['tickets_all'].format(identifier=order_identifier)
ticket_path = (
'generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

key = UPLOAD_PATHS['pdf']['order'].format(identifier=order_identifier)
invoice_path = (
'generated/invoices/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

# send email and notifications.
send_email_to_attendees(
order=order,
purchaser_id=current_user.id,
attachments=[ticket_path, invoice_path],
)
send_email_to_attendees(order=order, purchaser_id=current_user.id)

send_notif_to_attendees(order, current_user.id)
order_url = make_frontend_url(path=f'/orders/{order.identifier}')
Expand Down
25 changes: 21 additions & 4 deletions app/api/helpers/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.api.helpers.db import save_to_db
from app.api.helpers.files import make_frontend_url
from app.api.helpers.log import record_activity
from app.api.helpers.storage import UPLOAD_PATHS, generate_hash
from app.api.helpers.system_mails import MAILS
from app.api.helpers.utilities import get_serializer, str_generator, string_empty
from app.models.mail import (
Expand Down Expand Up @@ -336,13 +337,29 @@ def send_email_change_user_email(user, email):
send_email_with_action(email, USER_CHANGE_EMAIL, email=email, new_email=user.email)


def send_email_to_attendees(order, purchaser_id, attachments=None):
if not current_app.config['ATTACH_ORDER_PDF']:
attachments = None

def send_email_to_attendees(order, purchaser_id):
frontend_url = get_settings()['frontend_url']
order_view_url = frontend_url + '/orders/' + order.identifier + '/view'
key = UPLOAD_PATHS['pdf']['order'].format(identifier=order.identifier)
invoice_path = (
'generated/invoices/{}/{}/'.format(key, generate_hash(key))
+ order.identifier
+ '.pdf'
)
for holder in order.ticket_holders:
key = UPLOAD_PATHS['pdf']['tickets_all'].format(
identifier=order.identifier, attendee_identifier=holder.id
)
ticket_path = (
'generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order.identifier
+ '.pdf'
)
if not current_app.config['ATTACH_ORDER_PDF']:
attachments = None
else:
attachments = [ticket_path, invoice_path]

if holder.user and holder.user.id == purchaser_id:
# Ticket holder is the purchaser
send_email(
Expand Down
19 changes: 10 additions & 9 deletions app/api/helpers/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ def create_pdf_tickets_for_holder(order):
:param order: The order for which to create tickets for.
"""
if order.status == 'completed' or order.status == 'placed':
pdf = create_save_pdf(
render_template('pdf/ticket_purchaser.html', order=order),
UPLOAD_PATHS['pdf']['tickets_all'],
dir_path='/static/uploads/pdf/tickets/',
identifier=order.identifier,
upload_dir='generated/tickets/',
)

order.tickets_pdf_url = pdf

for holder in order.ticket_holders:
if (not holder.user) or holder.user.id != order.user_id:
Expand All @@ -88,10 +79,20 @@ def create_pdf_tickets_for_holder(order):
UPLOAD_PATHS['pdf']['tickets_all'],
dir_path='/static/uploads/pdf/tickets/',
identifier=order.identifier,
extra_identifiers={'attendee_identifier': holder.id},
upload_dir='generated/tickets/',
)
else:
# holder is the order buyer.
pdf = create_save_pdf(
render_template('pdf/ticket_purchaser.html', order=order),
UPLOAD_PATHS['pdf']['tickets_all'],
dir_path='/static/uploads/pdf/tickets/',
identifier=order.identifier,
extra_identifiers={'attendee_identifier': holder.id},
upload_dir='generated/tickets/',
)
order.tickets_pdf_url = pdf
pdf = order.tickets_pdf_url
holder.pdf_url = pdf
save_to_db(holder)
Expand Down
2 changes: 1 addition & 1 deletion app/api/helpers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
'pdf': {
'ticket_attendee': 'attendees/tickets/pdf/{identifier}',
'order': 'orders/invoices/pdf/{identifier}',
'tickets_all': 'orders/tickets/pdf/{identifier}',
'tickets_all': 'orders/tickets/pdf/{identifier}/{attendee_identifier}',
'event_invoice': 'events/organizer/invoices/pdf/{event_identifier}/{identifier}',
},
}
Expand Down
46 changes: 2 additions & 44 deletions app/api/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
from app.api.helpers.query import event_query
from app.api.helpers.storage import UPLOAD_PATHS, generate_hash
from app.api.helpers.ticketing import validate_discount_code, validate_ticket_holders
from app.api.helpers.utilities import dasherize, require_relationship
from app.api.schema.attendees import AttendeeSchema
Expand Down Expand Up @@ -118,29 +117,9 @@ def on_order_completed(order):
# send e-mail and notifications if the order status is completed
if not (order.status == 'completed' or order.status == 'placed'):
return
# fetch tickets attachment
order_identifier = order.identifier

key = UPLOAD_PATHS['pdf']['tickets_all'].format(identifier=order_identifier)
ticket_path = (
'generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

key = UPLOAD_PATHS['pdf']['order'].format(identifier=order_identifier)
invoice_path = (
'generated/invoices/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

# send email and notifications.
send_email_to_attendees(
order=order,
purchaser_id=current_user.id,
attachments=[ticket_path, invoice_path],
)
send_email_to_attendees(order=order, purchaser_id=current_user.id)

send_notif_to_attendees(order, current_user.id)

Expand Down Expand Up @@ -522,29 +501,8 @@ def after_update_object(self, order, data, view_kwargs):
elif (
order.status == 'completed' or order.status == 'placed'
) and order.deleted_at is None:
# Send email to attendees with invoices and tickets attached
order_identifier = order.identifier

key = UPLOAD_PATHS['pdf']['tickets_all'].format(identifier=order_identifier)
ticket_path = (
'generated/tickets/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

key = UPLOAD_PATHS['pdf']['order'].format(identifier=order_identifier)
invoice_path = (
'generated/invoices/{}/{}/'.format(key, generate_hash(key))
+ order_identifier
+ '.pdf'
)

# send email and notifications.
send_email_to_attendees(
order=order,
purchaser_id=current_user.id,
attachments=[ticket_path, invoice_path],
)
send_email_to_attendees(order=order, purchaser_id=current_user.id)

send_notif_to_attendees(order, current_user.id)

Expand Down