Skip to content
Merged
51 changes: 47 additions & 4 deletions app/api/user_favourite_events.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask import request, current_app as app
from flask_jwt import current_identity as current_user, _jwt_required
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask_rest_jsonapi.exceptions import ObjectNotFound
from sqlalchemy.orm.exc import NoResultFound

from app.models.user import User
from app.api.helpers.db import safe_query
from app.api.helpers.permission_manager import has_access
from app.api.helpers.exceptions import ForbiddenException, ConflictException
from app.api.helpers.permission_manager import has_access
from app.api.helpers.utilities import require_relationship
from app.api.schema.user_favourite_events import UserFavouriteEventSchema
from app.models import db
from app.models.user import User
from app.models.user_favourite_event import UserFavouriteEvent


Expand Down Expand Up @@ -79,11 +81,52 @@ class UserFavouriteEventDetail(ResourceDetail):
"""
User Favourite Events detail by id
"""
def before_get_object(self, view_kwargs):
if 'Authorization' in request.headers:
_jwt_required(app.config['JWT_DEFAULT_REALM'])
else:
raise ForbiddenException({'source': ''}, 'Only Authorized Users can view an event')

if view_kwargs.get('id') is not None:
try:
user_favourite_event = UserFavouriteEvent.query.filter_by(
user=current_user, event_id=view_kwargs['id']).first()
except NoResultFound:
raise ObjectNotFound({'source': ''}, "Object: not found")
Copy link
Member

Choose a reason for hiding this comment

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

Set proper source

else:
if user_favourite_event:
if user_favourite_event.event_id is not None:
view_kwargs['id'] = user_favourite_event.id
else:
view_kwargs['id'] = None

def before_delete_object(self, view_kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

This is unneeded. Remove

if 'Authorization' in request.headers:
_jwt_required(app.config['JWT_DEFAULT_REALM'])
else:
raise ForbiddenException({'source': ''}, 'Only Authorized Users can delete an event')

if view_kwargs.get('id') is not None:
try:
user_favourite_event = UserFavouriteEvent.query.filter_by(
user=current_user, event_id=view_kwargs['id']).first()
except NoResultFound:
raise ObjectNotFound({'source': ''}, "Object: not found")
else:
if user_favourite_event:
if user_favourite_event.event_id is not None:
view_kwargs['id'] = user_favourite_event.id
else:
view_kwargs['id'] = None

methods = ['GET', 'DELETE']
schema = UserFavouriteEventSchema
data_layer = {'session': db.session,
'model': UserFavouriteEvent}
'model': UserFavouriteEvent,
'methods': {
'before_delete_object': before_delete_object,
'before_get_object': before_get_object,
}}


class UserFavouriteEventRelationship(ResourceRelationship):
Expand Down
4 changes: 2 additions & 2 deletions docs/api/api_blueprint.apib
Original file line number Diff line number Diff line change
Expand Up @@ -25594,9 +25594,9 @@ This Group's APIs can be used for adding a particular event to the favourite lis
}
}

## Favourite Events Detail [/v1/user-favourite-events/{user_favourite_event_id}]
## Favourite Events Detail [/v1/user-favourite-events/{event_id}]
+ Parameters
+ user_favourite_event_id: 1 (integer) - ID of the Favourite Event
+ event_id: 1 (integer) - ID of the Event in the form of an integer
Copy link
Member

Choose a reason for hiding this comment

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

Changing name of parameters won't fix anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

@iamareebjamal But it should be changed, right?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, your version is more explicit, but if you changed it in hopes that it'll fix the error, then it won't


### Get Details [GET]

Expand Down