-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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: New Create Order API #7047
Merged
iamareebjamal
merged 4 commits into
fossasia:development
from
iamareebjamal:order-create
Jun 5, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import json | ||
from datetime import datetime | ||
|
||
import pytz | ||
from flask import Blueprint, jsonify, make_response, request | ||
from flask import Blueprint, app, jsonify, make_response, request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'flask.app' imported but unused |
||
from flask_jwt_extended import current_user, jwt_required | ||
from sqlalchemy.orm.exc import NoResultFound | ||
|
||
from app.api.auth import return_file | ||
from app.api.custom.schema.order_amount import OrderAmountInputSchema | ||
from app.api.helpers.db import get_count, safe_query | ||
from app.api.helpers.errors import ForbiddenError, NotFoundError, UnprocessableEntityError | ||
from app.api.helpers.files import make_frontend_url | ||
|
@@ -18,7 +20,7 @@ | |
from app.api.helpers.order import calculate_order_amount, create_pdf_tickets_for_holder | ||
from app.api.helpers.permission_manager import has_access | ||
from app.api.helpers.storage import UPLOAD_PATHS, generate_hash | ||
from app.api.custom.schema.order_amount import OrderAmountInputSchema | ||
from app.api.orders import validate_attendees | ||
from app.api.schema.attendees import AttendeeSchema | ||
from app.api.schema.orders import OrderSchema | ||
from app.extensions.limiter import limiter | ||
|
@@ -117,100 +119,55 @@ def resend_emails(): | |
@order_blueprint.route('/calculate-amount', methods=['POST']) | ||
def calculate_amount(): | ||
data, errors = OrderAmountInputSchema().load(request.get_json()) | ||
if errors: | ||
return make_response(jsonify(errors), 422) | ||
return jsonify(calculate_order_amount(data['tickets'], data.get('discount_code'))) | ||
|
||
|
||
@order_blueprint.route('/create-order', methods=['POST']) | ||
@jwt_required | ||
def create_order(): | ||
data = request.get_json() | ||
tickets, discount_code = data['tickets'], data['discount-code'] | ||
attendee = data['attendee'] | ||
for attribute in attendee: | ||
attendee[attribute.replace('-', '_')] = attendee.pop(attribute) | ||
schema = AttendeeSchema() | ||
json_api_attendee = {"data": {"attributes": data['attendee'], "type": "attendee"}} | ||
result = schema.load(json_api_attendee) | ||
if result.errors: | ||
return make_response(jsonify(result.errors), 422) | ||
ticket_ids = {int(ticket['id']) for ticket in tickets} | ||
quantity = {int(ticket['id']): ticket['quantity'] for ticket in tickets} | ||
ticket_list = ( | ||
db.session.query(Ticket) | ||
.filter(Ticket.id.in_(ticket_ids)) | ||
.filter_by(event_id=data['event_id'], deleted_at=None) | ||
.all() | ||
data, errors = OrderAmountInputSchema().load(request.get_json()) | ||
if errors: | ||
return make_response(jsonify(errors), 422) | ||
|
||
tickets_dict = data['tickets'] | ||
order_amount = calculate_order_amount(tickets_dict, data.get('discount_code')) | ||
ticket_ids = {ticket['id'] for ticket in tickets_dict} | ||
ticket_map = {int(ticket['id']): ticket for ticket in tickets_dict} | ||
tickets = ( | ||
Ticket.query.filter_by(deleted_at=None).filter(Ticket.id.in_(ticket_ids)).all() | ||
) | ||
ticket_ids_found = {ticket_information.id for ticket_information in ticket_list} | ||
tickets_not_found = ticket_ids - ticket_ids_found | ||
if tickets_not_found: | ||
return make_response( | ||
jsonify( | ||
status='Order Unsuccessful', | ||
error='Tickets with id {} were not found in Event {}.'.format( | ||
tickets_not_found, data['event_id'] | ||
), | ||
), | ||
404, | ||
|
||
if not tickets: | ||
raise UnprocessableEntityError( | ||
{'source': 'tickets'}, "Tickets missing in Order request", | ||
) | ||
for ticket_info in ticket_list: | ||
if ( | ||
ticket_info.quantity | ||
- get_count( | ||
db.session.query(TicketHolder.id).filter_by( | ||
ticket_id=int(ticket_info.id), deleted_at=None | ||
) | ||
) | ||
) < quantity[ticket_info.id]: | ||
return make_response( | ||
jsonify(status='Order Unsuccessful', error='Ticket already sold out.'), | ||
409, | ||
) | ||
attendee_list = [] | ||
for ticket in tickets: | ||
for _ in range(ticket['quantity']): | ||
attendee = TicketHolder( | ||
**result[0], event_id=int(data['event_id']), ticket_id=int(ticket['id']) | ||
) | ||
db.session.add(attendee) | ||
attendee_list.append(attendee) | ||
# created_at not getting filled | ||
ticket_pricing = calculate_order_amount(tickets, discount_code) | ||
if not has_access('is_coorganizer', event_id=data['event_id']): | ||
data['status'] = 'initializing' | ||
# create on site attendees | ||
# check if order already exists for this attendee. | ||
# check for free tickets and verified user | ||
|
||
event = tickets[0].event | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local variable 'event' is assigned to but never used |
||
|
||
try: | ||
attendees = [] | ||
for ticket in tickets: | ||
for _ in range(ticket_map[ticket.id]['quantity']): | ||
ticket.raise_if_unavailable() | ||
attendees.append(TicketHolder(firstname='', lastname='', ticket=ticket)) | ||
db.session.commit() | ||
except Exception as e: | ||
db.session.rollback() | ||
raise e | ||
|
||
validate_attendees({attendee.id for attendee in attendees}) | ||
order = Order( | ||
amount=ticket_pricing['total_amount'], | ||
user_id=current_user.id, | ||
event_id=int(data['event_id']), | ||
status=data['status'], | ||
amount=order_amount['total'], | ||
event=event, | ||
discount_code_id=data.get('discount_code'), | ||
ticket_holders=attendees, | ||
) | ||
db.session.add(order) | ||
db.session.commit() | ||
db.session.refresh(order) | ||
order_tickets = {} | ||
for holder in attendee_list: | ||
holder.order_id = order.id | ||
db.session.add(holder) | ||
if not order_tickets.get(holder.ticket_id): | ||
order_tickets[holder.ticket_id] = 1 | ||
else: | ||
order_tickets[holder.ticket_id] += 1 | ||
order.populate_and_save() | ||
|
||
for ticket in order_tickets: | ||
od = OrderTicket( | ||
order_id=order.id, ticket_id=ticket, quantity=order_tickets[ticket] | ||
) | ||
db.session.add(od) | ||
|
||
order.quantity = order.tickets_count | ||
db.session.add(order) | ||
db.session.commit() | ||
db.session.refresh(order) | ||
order_schema = OrderSchema() | ||
return order_schema.dump(order) | ||
return OrderSchema().dumps(order) | ||
|
||
|
||
@order_blueprint.route('/complete-order/<order_id>', methods=['PATCH']) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'json' imported but unused