Repository: fossasia/open-event-server

Issue:
Dashboard: Add info on API endpoints for event
We have the web app and Android app that can generate apps from the JSON files and should be able to do it through the API. We also want the volunteer system to get the data through the APIs.

Please 
- [x] provide API endpoints and 
- [ ] all required info on the specific event dashboard of events at `/events/ID/`
- [x] Name the tab "API" (not API Endpoints)



Discussion:
@mariobehling Can you give some more description on this.

Example: I am a developer and have an app to get my event data through an API. How can I get it instead of using export? What data do I need? What is the link?

@mariobehling 

> We also want the volunteer system to get the data through the APIs.

What exactly is a volunteer system? And what kind of APIs would it need?

@mariobehling I am taking this up.

I think this is already being possible. Something like this 
https://open-event.herokuapp.com/api/v2/events/4
https://open-event.herokuapp.com/api/v2/events/4/speakers
https://open-event.herokuapp.com/api/v2/events/4/microlocations
etc

I am already implementing that in the webapp generator https://github.com/fossasia/open-event-webapp/pull/409

That is what I was also saying. This should already be possible using `/api/v2` . @aayusharora and @the-dagger also agree I think.

@SaptakS @rafalkowalski 
One request

can we get `/api/v2/events/{id}/event` endpoint too ? 
that should work exactly the same as `/api/v2/events/{id}` only

@championswimmer What should this endpoint provide data about?

Same as events/id

events/2 == events/2/event

That is possible?

On Mon 4 Jul, 2016 23:13 Saptak Sengupta, notifications@github.com wrote:

> @championswimmer https://github.com/championswimmer What should this
> endpoint provide data about?
> 
> —
> You are receiving this because you were mentioned.
> 
> Reply to this email directly, view it on GitHub
> https://github.com/fossasia/open-event-orga-server/issues/1191#issuecomment-230333105,
> or mute the thread
> https://github.com/notifications/unsubscribe/ABQ_yvlwVRXx-SIipPQc3B7PNbZ10uKhks5qSUYwgaJpZM4I9j15
> .

@championswimmer Yes it can be done. But why is it needed if both the paths provide the same data (nothing wrong with it)?

@championswimmer @mariobehling can this issue be closed?


Resolving patch:
diff --git a/open_event/views/admin/admin.py b/open_event/views/admin/admin.py
--- a/open_event/views/admin/admin.py
+++ b/open_event/views/admin/admin.py
@@ -6,6 +6,7 @@
 from open_event.models.user import User
 from open_event.views.admin.models_views.events import EventsView
 from open_event.views.admin.models_views.my_sessions import MySessionView
+from open_event.views.admin.models_views.event_api import EventApiView
 from open_event.views.admin.models_views.roles import RoleView
 from open_event.views.admin.models_views.profile import ProfileView
 from open_event.views.admin.models_views.settings import SettingsView
@@ -54,6 +55,7 @@ def _add_views(self):
         self.admin.add_view(SponsorsView(name='Sponsors', url='/events/<event_id>/sponsors', endpoint="event_sponsors"))
         self.admin.add_view(SessionsView(name='Sessions', url='/events/<event_id>/sessions', endpoint="event_sessions"))
         self.admin.add_view(SchedulerView(name='Scheduler', url='/events/<event_id>/scheduler', endpoint="event_scheduler"))
+        self.admin.add_view(EventApiView(name='Api', url='/events/<event_id>/api', endpoint="event_api"))
         self.admin.add_view(RoleView(name='Role', url='/events/<event_id>/roles', endpoint="event_roles"))
         self.admin.add_view(ProfileView(name='Profile', url='/profile', endpoint="profile"))
         self.admin.add_view(InviteView(name='Invite', url='/events/<event_id>/invite', endpoint="event_invites"))
diff --git a/open_event/views/admin/models_views/event_api.py b/open_event/views/admin/models_views/event_api.py
new file mode 100644
--- /dev/null
+++ b/open_event/views/admin/models_views/event_api.py
@@ -0,0 +1,114 @@
+import json
+import urllib2
+import re
+
+import flask_login
+from flask import flash, redirect, url_for, request
+from flask.ext.restplus import abort
+from flask_admin import BaseView, expose
+
+from open_event.helpers.data import DataManager
+from open_event.views.admin.models_views.events import is_verified_user
+from ....helpers.data_getter import DataGetter
+
+
+class EventApiView(BaseView):
+
+    @expose('/')
+    @flask_login.login_required
+    def display_api_view(self, event_id):
+        response = urllib2.urlopen('http://open-event-dev.herokuapp.com/api/v2/swagger.json')
+        event = DataGetter.get_event(event_id)
+        data = json.load(response)
+        events_data = {}
+        paths = []
+        ctr = 0
+        for path in data['paths']:
+            if re.match(r"\/events\/{event_id}(\/\w*)*", path):
+                path_modified = path.replace('{event_id}', str(event_id))
+                paths.append(path_modified)
+                events_data[path_modified] = data['paths'][path]
+                events_data[path_modified]['id'] = ctr
+                ctr += 1
+                for method in data['paths'][path]:
+                    if method == "get":
+                        try:
+                            definition = data['paths'][path][method]['responses']['200']['schema']['items']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['200']['schema']['items']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['200']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['200']['schema']['ref_def'] = data['definitions'][definition]
+                            for prop in data['definitions'][definition]['properties']:
+                                if '$ref' in data['definitions'][definition]['properties'][prop]:
+                                    def_ = data['definitions'][definition]['properties'][prop]['$ref'].replace('#/definitions/', '')
+                                    events_data[path_modified][method]['responses']['200']['schema']['ref_def']['properties'][prop]['ref_def'] = data['definitions'][def_]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['400']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['400']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['401']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['401']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['404']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['404']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                    elif method == "post" or method == "put" or method == "delete":
+                        try:
+                            definition = data['paths'][path][method]['parameters'][0]['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['parameters'][0]['schema']['ref_def'] = data['definitions'][definition]
+                            for prop in data['definitions'][definition]['properties']:
+                                if '$ref' in data['definitions'][definition]['properties'][prop]:
+                                    def_ = data['definitions'][definition]['properties'][prop]['$ref'].replace('#/definitions/', '')
+                                    events_data[path_modified][method]['parameters'][0]['schema']['ref_def']['properties'][prop]['ref_def'] = data['definitions'][def_]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['200']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['200']['schema']['ref_def'] = data['definitions'][definition]
+                            for prop in data['definitions'][definition]['properties']:
+                                if '$ref' in data['definitions'][definition]['properties'][prop]:
+                                    def_ = data['definitions'][definition]['properties'][prop]['$ref'].replace('#/definitions/', '')
+                                    events_data[path_modified][method]['responses']['200']['schema']['ref_def']['properties'][prop]['ref_def'] = data['definitions'][def_]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['400']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['400']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['401']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['401']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+                        try:
+                            definition = data['paths'][path][method]['responses']['404']['schema']['$ref'].replace('#/definitions/', '')
+                            events_data[path_modified][method]['responses']['404']['schema']['ref_def'] = data['definitions'][definition]
+                        except:
+                            pass
+
+        paths.sort()
+
+        if not is_verified_user():
+            flash("Your account is unverified. "
+                  "Please verify by clicking on the confirmation link that has been emailed to you.")
+        return self.render('/gentelella/admin/event/api/index.html',
+                           event=event, data=events_data, paths=paths)


Test patch: