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 6 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: 11 additions & 9 deletions app/api/access_codes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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,
ConflictException
Copy link
Member

Choose a reason for hiding this comment

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

Why is this left?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't find conflictException in other file(error.py).

Copy link
Member

Choose a reason for hiding this comment

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

Then move it there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so I should remove all exceptions from exception.py and move to error.py.there are only two left ConflictException and MethodNotAllowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iamareebjamal Shall I move all exception from exception.py to error.py ???

Copy link
Member

Choose a reason for hiding this comment

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

Yup

Copy link
Member

Choose a reason for hiding this comment

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

Yup

)
from app.api.helpers.errors import (
UnprocessableEntityError,
ForbiddenError
)
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
Expand Down Expand Up @@ -36,7 +37,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 +94,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 +113,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 +161,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: 21 additions & 20 deletions app/api/attendees.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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,
ConflictException
)
from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError
)
from app.api.helpers.permission_manager import has_access
from app.api.helpers.permissions import jwt_required
Expand Down Expand Up @@ -76,11 +77,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 +93,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 +140,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 +189,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 +201,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 +214,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 +223,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 +250,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 +267,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
8 changes: 4 additions & 4 deletions app/api/data_layers/ChargesLayer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.exceptions import ConflictException
from app.api.helpers.errors import UnprocessableEntityError
from app.api.helpers.ticketing import TicketingManager
from app.models.order import Order

Expand Down Expand Up @@ -51,7 +51,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 +66,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
Loading