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

chore(refactor): Move all custom API errors to errors.py #7019

Merged
merged 24 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ffa1ab0
Removed duplicate exeptions in exception.py
Satyam52 May 16, 2020
eb1a214
removed duplicates from execption.py
Satyam52 May 16, 2020
1166cec
removed blank space and fixed order.py
Satyam52 May 16, 2020
87ff209
aligning syntaxes
Satyam52 May 16, 2020
4baae07
fixed some indentation and removed blank spaces
Satyam52 May 17, 2020
8403c58
replaced errors from test files
Satyam52 May 17, 2020
62ff327
moved all errors from exception.py to errors.py
Satyam52 May 18, 2020
273a4ce
added comment in exception.py
Satyam52 May 18, 2020
03e7689
removed exception.py
Satyam52 May 18, 2020
017e681
resolved merge conflicts
Satyam52 May 18, 2020
1d8eb8f
chore(polish): Reformat after safe_query refactor (#7020)
iamareebjamal May 18, 2020
460acaf
Removed duplicate exeptions in exception.py
Satyam52 May 16, 2020
2e8fdc1
removed duplicates from execption.py
Satyam52 May 16, 2020
0314a56
removed blank space and fixed order.py
Satyam52 May 16, 2020
e7cdcc3
fixed some indentation and removed blank spaces
Satyam52 May 17, 2020
1570a48
replaced errors from test files
Satyam52 May 17, 2020
476d004
moved all errors from exception.py to errors.py
Satyam52 May 18, 2020
a31ca64
added comment in exception.py
Satyam52 May 18, 2020
ef44187
removed exception.py
Satyam52 May 18, 2020
a22f904
succesfully rebased
Satyam52 May 20, 2020
1ac899a
succesfully rebased
Satyam52 May 20, 2020
68a3d4e
remove re-import
Satyam52 May 20, 2020
321cb19
changing api/attendes.py to previous state
Satyam52 May 20, 2020
3f8b202
reformat
iamareebjamal May 21, 2020
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
20 changes: 10 additions & 10 deletions app/api/access_codes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask_rest_jsonapi.exceptions import ObjectNotFound
from sqlalchemy.orm.exc import NoResultFound

Copy link
Member

Choose a reason for hiding this comment

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

Why remove this line?

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import (
ConflictException,
ForbiddenException,
UnprocessableEntity,
from app.api.helpers.errors import (
UnprocessableEntityError,
ForbiddenError,
ConflictException

Choose a reason for hiding this comment

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

Black would make changes.

)
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
Expand Down Expand Up @@ -36,7 +35,7 @@ def before_post(self, args, kwargs, data):
"""
require_relationship(['event', 'user'], data)
if not has_access('is_coorganizer', event_id=data['event']):
raise ForbiddenException({'source': ''}, "Minimum Organizer access required")
raise ForbiddenError({'source': ''}, "Minimum Organizer access required")

def before_create_object(self, data, view_kwargs):
"""
Expand Down Expand Up @@ -93,12 +92,12 @@ def query(self, view_kwargs):
if view_kwargs.get('user_id'):
user = safe_query(User, 'id', view_kwargs['user_id'], 'user_id')
if not has_access('is_user_itself', user_id=user.id):
raise ForbiddenException({'source': ''}, 'Access Forbidden')
raise ForbiddenError({'source': ''}, 'Access Forbidden')
query_ = query_.join(User).filter(User.id == user.id)
if view_kwargs.get('ticket_id'):
ticket = safe_query(Ticket, 'id', view_kwargs['ticket_id'], 'ticket_id')
if not has_access('is_coorganizer', event_id=ticket.event_id):
raise ForbiddenException({'source': ''}, 'Access Forbidden')
raise ForbiddenError({'source': ''}, 'Access Forbidden')
# access_code - ticket :: many-to-many relationship
query_ = AccessCode.query.filter(AccessCode.tickets.any(id=ticket.id))
query_
Expand All @@ -112,7 +111,7 @@ def query(self, view_kwargs):
data_layer = {
'session': db.session,
'model': AccessCode,
'methods': {'query': query,},
'methods': {'query': query, },
}


Expand Down Expand Up @@ -160,7 +159,8 @@ def before_get(self, args, kwargs):
raise ObjectNotFound({'parameter': '{id}'}, "Access Code: not found")

if not has_access('is_coorganizer', event_id=access.event_id):
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

decorators = (
api.has_permission(
Expand Down
41 changes: 20 additions & 21 deletions app/api/attendees.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
from flask_jwt_extended import current_user
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from sqlalchemy import and_, or_

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import (
ConflictException,
ForbiddenException,
UnprocessableEntity,
from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

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

Black would make changes.

)
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
Expand Down Expand Up @@ -76,11 +75,11 @@ def before_post(self, args, kwargs, data):
.first()
)
if ticket is None:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/relationships/ticket'}, "Invalid Ticket"
)
if ticket.event_id != int(data['event']):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/relationships/ticket'},
"Ticket belongs to a different Event",
)
Expand All @@ -92,19 +91,19 @@ def before_post(self, args, kwargs, data):

if 'device_name_checkin' in data and data['device_name_checkin'] is not None:
if 'is_checked_in' not in data or not data['is_checked_in']:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/device_name_checkin'},
"Attendee needs to be checked in first",
)
elif 'checkin_times' not in data or data['checkin_times'] is None:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/device_name_checkin'},
"Check in Times missing",
)
elif len(data['checkin_times'].split(",")) != len(
data['device_name_checkin'].split(",")
):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/device_name_checkin'},
"Check in Times missing for the corresponding device name",
)
Expand Down Expand Up @@ -139,19 +138,19 @@ def query(self, view_kwargs):
if not has_access('is_registrar', event_id=order.event_id) and not has_access(
'is_user_itself', user_id=order.user_id
):
raise ForbiddenException({'source': ''}, 'Access Forbidden')
raise ForbiddenError({'source': ''}, 'Access Forbidden')
query_ = query_.join(Order).filter(Order.id == order.id)

if view_kwargs.get('ticket_id'):
ticket = safe_query(Ticket, 'id', view_kwargs['ticket_id'], 'ticket_id')
# if not has_access('is_registrar', event_id=ticket.event_id):
# raise ForbiddenException({'source': ''}, 'Access Forbidden')
# raise ForbiddenError({'source': ''}, 'Access Forbidden')
query_ = query_.join(Ticket).filter(Ticket.id == ticket.id)

if view_kwargs.get('user_id'):
user = safe_query(User, 'id', view_kwargs['user_id'], 'user_id')
if not has_access('is_user_itself', user_id=user.id):
raise ForbiddenException({'source': ''}, 'Access Forbidden')
raise ForbiddenError({'source': ''}, 'Access Forbidden')
query_ = query_.join(User, User.email == TicketHolder.email).filter(
User.id == user.id
)
Expand Down Expand Up @@ -188,7 +187,7 @@ def before_get_object(self, view_kwargs):
user_id=current_user.id,
event_id=attendee.event_id,
):
raise ForbiddenException(
raise ForbiddenError(
{'source': 'User'}, 'You are not authorized to access this.'
)

Expand All @@ -200,7 +199,7 @@ def before_delete_object(self, obj, kwargs):
:return:
"""
if not has_access('is_registrar', event_id=obj.event_id):
raise ForbiddenException(
raise ForbiddenError(
{'source': 'User'}, 'You are not authorized to access this.'
)

Expand All @@ -213,7 +212,7 @@ def before_update_object(self, obj, data, kwargs):
:return:
"""
# if not has_access('is_registrar', event_id=obj.event_id):
# raise ForbiddenException({'source': 'User'}, 'You are not authorized to access this.')
# raise ForbiddenError({'source': 'User'}, 'You are not authorized to access this.')

Choose a reason for hiding this comment

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

line too long (100 > 90 characters)


if 'ticket' in data:
ticket = (
Expand All @@ -222,20 +221,20 @@ def before_update_object(self, obj, data, kwargs):
.first()
)
if ticket is None:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/relationships/ticket'}, "Invalid Ticket"
)

if 'device_name_checkin' in data:
if 'checkin_times' not in data or data['checkin_times'] is None:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/device_name_checkin'},
"Check in Times missing",
)

if 'is_checked_in' in data and data['is_checked_in']:
if 'checkin_times' not in data or data['checkin_times'] is None:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/checkin_times'},
"Check in time missing while trying to check in attendee",
)
Expand All @@ -249,7 +248,7 @@ def before_update_object(self, obj, data, kwargs):
elif obj.checkin_times and data[
'checkin_times'
] in obj.checkin_times.split(","):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/checkin_times'},
"Check in time already present",
)
Expand All @@ -266,7 +265,7 @@ def before_update_object(self, obj, data, kwargs):
if len(data['checkin_times'].split(",")) != len(
data['device_name_checkin'].split(",")
):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'pointer': '/data/attributes/device_name_checkin'},
"Check in Time missing for the corresponding device name",
)
Expand Down
7 changes: 3 additions & 4 deletions app/api/data_layers/ChargesLayer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from flask_rest_jsonapi.data_layers.base import BaseDataLayer
from flask_rest_jsonapi.exceptions import ObjectNotFound

from app.api.helpers.exceptions import ConflictException, UnprocessableEntity
from app.api.helpers.errors import UnprocessableEntityError, ConflictException
from app.api.helpers.ticketing import TicketingManager
from app.models.order import Order

Expand Down Expand Up @@ -51,7 +50,7 @@ def create_object(self, data, view_kwargs):
# charge through stripe
if order.payment_mode == 'stripe':
if not data.get('stripe'):
raise UnprocessableEntity({'source': ''}, "stripe token is missing")
raise UnprocessableEntityError({'source': ''}, "stripe token is missing")
if not order.event.can_pay_by_stripe:
raise ConflictException(
{'': ''}, "This event doesn't accept payments by Stripe"
Expand All @@ -66,7 +65,7 @@ def create_object(self, data, view_kwargs):
# charge through paypal
elif order.payment_mode == 'paypal':
if (not data.get('paypal_payer_id')) or (not data.get('paypal_payment_id')):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'source': ''}, "paypal_payer_id or paypal_payment_id or both missing"
)
if not order.event.can_pay_by_paypal:
Expand Down
2 changes: 0 additions & 2 deletions app/api/data_layers/EventCopyLayer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from datetime import date

from flask_rest_jsonapi.data_layers.base import BaseDataLayer
from sqlalchemy.orm import make_transient

from app.api.helpers.db import safe_query, save_to_db
from app.api.helpers.files import create_save_resized_image
from app.models import db
Expand Down
46 changes: 26 additions & 20 deletions app/api/discount_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from sqlalchemy.orm.exc import NoResultFound

from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import (
from app.api.helpers.errors import (

Choose a reason for hiding this comment

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

Black would make changes.

ForbiddenError,
UnprocessableEntityError,
ConflictException,
ForbiddenException,
MethodNotAllowed,
UnprocessableEntity,
MethodNotAllowed

Choose a reason for hiding this comment

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

Black would make changes.

)
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
Expand Down Expand Up @@ -59,13 +59,13 @@ def before_post(self, args, kwargs, data):
if data['used_for'] == 'ticket':
require_relationship(['event'], data)
if not has_access('is_coorganizer', event_id=data['event']):
raise ForbiddenException({'source': ''}, 'You are not authorized')
raise ForbiddenError({'source': ''}, 'You are not authorized')
elif (
data['used_for'] == 'event'
and not has_access('is_admin')
and 'events' in data
):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission or check your relationship"
)

Expand All @@ -83,11 +83,11 @@ def before_create_object(self, data, view_kwargs):
.one()
)
except NoResultFound:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'event_id': event}, "Event does not exist"
)
if event_now.discount_code_id:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'event_id': event},
"A Discount Code already exists for the provided Event ID",
)
Expand All @@ -104,7 +104,7 @@ def before_get(self, args, kwargs):
if has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "You are not authorized")
raise UnprocessableEntityError({'source': ''}, "You are not authorized")

decorators = (jwt_required,)
schema = DiscountCodeSchemaTicket
Expand Down Expand Up @@ -136,7 +136,7 @@ def query(self, view_kwargs):
user = safe_query(User, 'id', view_kwargs['user_id'], 'user_id')
query_ = query_.join(User).filter(User.id == user.id)
else:
raise ForbiddenException({'source': ''}, 'You are not authorized')
raise ForbiddenError({'source': ''}, 'You are not authorized')

if view_kwargs.get('event_identifier'):
event = safe_query(
Expand All @@ -150,7 +150,7 @@ def query(self, view_kwargs):
self.schema = DiscountCodeSchemaTicket
query_ = query_.filter_by(event_id=view_kwargs['event_id'])
else:
raise ForbiddenException(
raise ForbiddenError(
{'source': ''}, 'Event organizer access required'
)

Expand Down Expand Up @@ -217,7 +217,7 @@ def before_get(self, args, kwargs):
else:
kwargs['id'] = None
else:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'source': ''},
"Please verify your permission. You must have coorganizer "
"privileges to view ticket discount code details",
Expand All @@ -230,7 +230,7 @@ def before_get(self, args, kwargs):
else:
kwargs['id'] = None
else:
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'source': ''},
"Please verify your permission. You must be admin to view event discount code details",
)
Expand Down Expand Up @@ -308,7 +308,8 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event':
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

def before_get_object(self, view_kwargs):
"""
Expand Down Expand Up @@ -372,10 +373,11 @@ def before_get_object(self, view_kwargs):
elif discount.used_for == 'event':
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

elif not view_kwargs.get('id') and not has_access('is_admin'):
raise UnprocessableEntity(
raise UnprocessableEntityError(
{'source': ''},
"Please verify your permission. You must be admin to view event\
discount code details",
Expand Down Expand Up @@ -409,7 +411,8 @@ def before_update_object(self, discount, data, view_kwargs):
self.schema = DiscountCodeSchemaEvent
self.resource.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

def before_delete_object(self, discount, view_kwargs):
"""
Expand All @@ -426,7 +429,8 @@ def before_delete_object(self, discount, view_kwargs):
elif discount.used_for == 'event' and has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

# decorators = (jwt_required,)
schema = DiscountCodeSchemaTicket
Expand Down Expand Up @@ -465,7 +469,8 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event' and has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

methods = ['GET', 'PATCH']
decorators = (jwt_required,)
Expand Down Expand Up @@ -498,7 +503,8 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event' and has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError(
{'source': ''}, "Please verify your permission")

schema = DiscountCodeSchemaEvent
data_layer = {'session': db.session, 'model': DiscountCode}
Loading