Repository: fossasia/open-event-server

Issue:
Delete User Favorite By EventId
**Is your feature request related to a problem? Please describe.**
Current Implementation of user-favorite-events allows deletion of events by using the id given by the server.
**Describe the solution you'd like**
There should be an option to delete the event using eventId rather the user-favorite-event id 



Discussion:
@mrsaicharan1 Can you take a look 
Yeah! Will solve this.

On Wed, 1 May 2019 at 6:58 PM, Pulkit Aggarwal <notifications@github.com>
wrote:

> @mrsaicharan1 <https://github.com/mrsaicharan1> Can you take a look
>
> —
> You are receiving this because you were mentioned.
>
>
> Reply to this email directly, view it on GitHub
> <https://github.com/fossasia/open-event-server/issues/5857#issuecomment-488282036>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AGAHUW5U7YGXZNS2PO2FZYTPTGLIPANCNFSM4HJTPUFA>
> .
>

@mrsaicharan1 any update on this ?
@aggarwalpulkit596 Sorry for the delay on this. Will get up to speed with this today.
DELETE methods are automatically integrated when JSON API endpoints are created. What is the query you're having a problem with right now? You can directly delete the event using event_id. 
http://127.0.0.1:5000/#events-event-details-delete
We're talking about deleting favorite, not the event
Any Update @mrsaicharan1  on this 
This is a major roadblock for the app we need this ASAP @iamareebjamal correct?
There are some priority issues which are blocking the system from
functioning properly right now. I've been involved in the discussions and
the necessary things required for them.
Will try to do this as soon as I'm finished with those. You can keep a tab
on the issue tracker and the gitter channels as to what we've been talking
about :) .However, in the meantime, anyone else is welcome to take this.

On Sun, 19 May 2019 at 4:03 PM, Pulkit Aggarwal <notifications@github.com>
wrote:

> Any Update @mrsaicharan1 <https://github.com/mrsaicharan1> on this
>
> —
> You are receiving this because you were mentioned.
>
>
> Reply to this email directly, view it on GitHub
> <https://github.com/fossasia/open-event-server/issues/5857?email_source=notifications&email_token=AGAHUW5JP6X4GYPLJNMZT5DPWEUHPA5CNFSM4HJTPUFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVW7KTY#issuecomment-493745487>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AGAHUWYU5D7LINLCRUSZYJTPWEUHPANCNFSM4HJTPUFA>
> .
>

@prateekj117 Can you take this up?
@iamareebjamal Sure. On it. 👍
So one solution here is to pass body in the Request, but @iamareebjamal told that Delete method doesn't work with body. So one solution is to make a new endpoint. Or is there any other solution? @uds5501 @mrsaicharan1 @shreyanshdwivedi 
You'll need to make a new API endpoint using flask_reset_json_api to create
and update this. The delete function will be integrated automatically.

On Mon, 20 May 2019 at 1:00 PM, Prateek Jain <notifications@github.com>
wrote:

> So one solution here is to pass body in the Request, but @iamareebjamal
> <https://github.com/iamareebjamal> told that Delete method doesn't work
> with body. So one solution is to make a new endpoint. Or is there any other
> solution? @uds5501 <https://github.com/uds5501> @mrsaicharan1
> <https://github.com/mrsaicharan1> @shreyanshdwivedi
> <https://github.com/shreyanshdwivedi>
>
> —
> You are receiving this because you were mentioned.
>
>
> Reply to this email directly, view it on GitHub
> <https://github.com/fossasia/open-event-server/issues/5857?email_source=notifications&email_token=AGAHUWYWWUFMUZAVMMJSBTLPWLRNJA5CNFSM4HJTPUFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVZTR4A#issuecomment-494090480>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AGAHUWZTASTVCMKNZ4W4X6TPWLRNJANCNFSM4HJTPUFA>
> .
>

There'll be no need of create as it won't be different from the previous endpoint and delete function won't be integrated automatically since you're not deleting the resource by its ID. you are deleting it by event's ID
@iamareebjamal Without creating a new endpoint, how would we know if the client passed an id or an event_id to delete the favorite.
You can create a new endpoint, but there'll be no create or update and delete will not be integrated automatically. You'll have to write custom delete function
@iamareebjamal So a new endpoint with a custom delete function, so do you have something else in mind?
No

Resolving patch:
diff --git a/app/api/user_favourite_events.py b/app/api/user_favourite_events.py
--- a/app/api/user_favourite_events.py
+++ b/app/api/user_favourite_events.py
@@ -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
 
 
@@ -79,11 +81,28 @@ class UserFavouriteEventDetail(ResourceDetail):
     """
     User Favourite Events detail by id
     """
+    @jwt_required()
+    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):
diff --git a/app/factories/user_favourite_events.py b/app/factories/user_favourite_events.py
--- a/app/factories/user_favourite_events.py
+++ b/app/factories/user_favourite_events.py
@@ -3,6 +3,7 @@
 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):
@@ -10,5 +11,5 @@ 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)


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -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()