Skip to content

Commit

Permalink
chore(refactor): Refactor code for custom attendee route (#6752)
Browse files Browse the repository at this point in the history
  • Loading branch information
codedsun authored and iamareebjamal committed Jan 21, 2020
1 parent 10a914b commit 494608c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
37 changes: 0 additions & 37 deletions app/api/attendees.py
@@ -1,10 +1,7 @@
import datetime

from flask import Blueprint, request, jsonify, abort, make_response
from flask_jwt_extended import current_user
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask_rest_jsonapi.exceptions import ObjectNotFound
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import or_, and_

from app.api.bootstrap import api
Expand All @@ -14,7 +11,6 @@
ForbiddenException,
UnprocessableEntity,
)
from app.api.helpers.mail import send_email_to_attendees
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
Expand All @@ -28,8 +24,6 @@

from app.settings import get_settings

attendee_misc_routes = Blueprint('attendee_misc', __name__, url_prefix='/v1')


def get_sold_and_reserved_tickets_count(event_id):
order_expiry_time = get_settings()['order_expiry_time']
Expand Down Expand Up @@ -275,34 +269,3 @@ class AttendeeRelationshipOptional(ResourceRelationship):
schema = AttendeeSchema
data_layer = {'session': db.session,
'model': TicketHolder}


@attendee_misc_routes.route('/attendees/send-receipt', methods=['POST'])
@jwt_required
def send_receipt():
"""
Send receipts to attendees related to the provided order.
:return:
"""
order_identifier = request.json.get('order-identifier')
if order_identifier:
try:
order = db.session.query(Order).filter_by(identifier=order_identifier).one()
except NoResultFound:
raise ObjectNotFound({'parameter': '{identifier}'}, "Order not found")

if (order.user_id != current_user.id) and (not has_access('is_registrar', event_id=order.event_id)):
abort(
make_response(jsonify(error="You need to be the event organizer or order buyer to send receipts."), 403)
)
elif order.status != 'completed':
abort(
make_response(jsonify(error="Cannot send receipt for an incomplete order"), 409)
)
else:
send_email_to_attendees(order, current_user.id)
return jsonify(message="receipt sent to attendees")
else:
abort(
make_response(jsonify(error="Order identifier missing"), 422)
)
45 changes: 45 additions & 0 deletions app/api/custom/attendees.py
@@ -0,0 +1,45 @@
from flask import Blueprint, request, jsonify, abort, make_response
from flask_jwt_extended import current_user
from sqlalchemy.orm.exc import NoResultFound

from app.api.helpers.mail import send_email_to_attendees
from app.api.helpers.permissions import jwt_required
from app.api.helpers.errors import (
UnprocessableEntityError,
NotFoundError,
ForbiddenError,
)
from app.api.helpers.permission_manager import has_access

from app.models.order import Order
from app.models import db

attendee_blueprint = Blueprint('attendee_blueprint', __name__, url_prefix='/v1')


@attendee_blueprint.route('/attendees/send-receipt', methods=['POST'])
@jwt_required
def send_receipt():
"""
Send receipts to attendees related to the provided order.
:return:
"""
order_identifier = request.json.get('order-identifier')
if order_identifier:
try:
order = db.session.query(Order).filter_by(identifier=order_identifier).one()
except NoResultFound:
return NotFoundError({'parameter': '{order_identifier}'}, "Order not found").respond()

if (order.user_id != current_user.id) and (not has_access('is_registrar', event_id=order.event_id)):
return ForbiddenError({'source': ''},
'You need to be the event organizer or order buyer to send receipts.').respond()
elif order.status != 'completed':
abort(
make_response(jsonify(error="Cannot send receipt for an incomplete order"), 409)
)
else:
send_email_to_attendees(order, current_user.id)
return jsonify(message="receipt sent to attendees")
else:
return UnprocessableEntityError({'source': ''}, 'Order identifier missing').respond()
4 changes: 2 additions & 2 deletions app/instance.py
Expand Up @@ -138,7 +138,7 @@ def create_app():
with app.app_context():
from app.api.admin_statistics_api.events import event_statistics
from app.api.auth import auth_routes
from app.api.attendees import attendee_misc_routes
from app.api.custom.attendees import attendee_blueprint
from app.api.bootstrap import api_v1
from app.api.celery_tasks import celery_routes
from app.api.event_copy import event_copy
Expand Down Expand Up @@ -166,7 +166,7 @@ def create_app():
app.register_blueprint(auth_routes)
app.register_blueprint(event_statistics)
app.register_blueprint(user_misc_routes)
app.register_blueprint(attendee_misc_routes)
app.register_blueprint(attendee_blueprint)
app.register_blueprint(order_misc_routes)
app.register_blueprint(role_invites_misc_routes)
app.register_blueprint(authorised_blueprint)
Expand Down

0 comments on commit 494608c

Please sign in to comment.