Repository: fossasia/open-event-server

Issue:
HTTP 500 for getting order's user relationship
**Describe the bug**
HTTP 500 is raised when a GET request is made for the user relationship of an order - `v1/orders/<identifier>/relationship/user`

**To Reproduce**
Steps to reproduce the behavior:
Make a get request for the above mentioned endpoint.

**Expected behavior**
Order's user relationship should be displayed.

**Stacktrace**
```
INFO:werkzeug:127.0.0.1 - - [14/Jul/2018 23:35:18] "GET /v1/orders/097b7fc5-6155-4646-92f8-c53d148ed25f/relationships/user HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/binaryboy/Desktop/open-event-server/app/__init__.py", line 66, in __call__
    return self.app(environ, start_response)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/binaryboy/Desktop/open-event-server/app/api/helpers/permissions.py", line 45, in decorator
    return fn(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 67, in dispatch_request
    response = method(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 365, in get
    kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 241, in get_relationship
    obj = self.get_object(view_kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 70, in get_object
    filter_value = view_kwargs[url_field]
KeyError: 'id'
```

Working on fixing this error.


Discussion:

Resolving patch:
diff --git a/app/api/orders.py b/app/api/orders.py
--- a/app/api/orders.py
+++ b/app/api/orders.py
@@ -320,6 +320,22 @@ class OrderRelationship(ResourceRelationship):
     """
     Order relationship
     """
+
+    def before_get(self, args, kwargs):
+        """
+        before get method to get the resource id for fetching details
+        :param view_kwargs:
+        :return:
+        """
+        if kwargs.get('order_identifier'):
+            order = safe_query(db, Order, 'identifier', kwargs['order_identifier'], 'order_identifier')
+            kwargs['id'] = order.id
+        elif kwargs.get('id'):
+            order = safe_query(db, Order, 'id', kwargs['id'], 'id')
+
+        if not has_access('is_coorganizer', event_id=order.event_id, user_id=order.user_id):
+            return ForbiddenException({'source': ''}, 'You can only access your orders or your event\'s orders')
+
     decorators = (jwt_required,)
     schema = OrderSchema
     data_layer = {'session': db.session,


Test patch: