Skip to content
Merged
29 changes: 24 additions & 5 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_jwt import current_identity as current_user, _jwt_required, 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,28 @@ class UserFavouriteEventDetail(ResourceDetail):
"""
User Favourite Events detail by id
"""
@jwt_required()
Copy link
Member

Choose a reason for hiding this comment

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

paranthesis is redundant

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 It shows error when not using paranthesis.
Screenshot from 2019-06-22 14-20-29

Copy link
Member

Choose a reason for hiding this comment

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

OK

def before_get_object(self, view_kwargs):

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': '/data/relationships/event'}, "Object: not found")
else:
if user_favourite_event 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_get_object': before_get_object,
}}


class UserFavouriteEventRelationship(ResourceRelationship):
Expand Down
5 changes: 3 additions & 2 deletions app/factories/user_favourite_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from app.factories.user import UserFactory
from app.factories.event import EventFactoryBasic
from app.models.user_favourite_event import db, UserFavouriteEvent
from app.models.user import User


class UserFavouriteEventFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = UserFavouriteEvent
sqlalchemy_session = db.session

user = factory.RelatedFactory(UserFactory)
event = factory.RelatedFactory(EventFactoryBasic)
user = factory.LazyAttribute(lambda a: User.query.first())
event = factory.SubFactory(EventFactoryBasic)
4 changes: 2 additions & 2 deletions docs/api/api_blueprint.apib
Original file line number Diff line number Diff line change
Expand Up @@ -25616,9 +25616,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
4 changes: 2 additions & 2 deletions tests/hook_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4358,8 +4358,8 @@ def favourite_events_list_post(transaction):
:return:
"""
with stash['app'].app_context():
user_fav_event = UserFavouriteEventFactory()
db.session.add(user_fav_event)
event = EventFactoryBasic()
db.session.add(event)
db.session.commit()


Expand Down