Repository: fossasia/open-event-server

Issue:
bug: generate_icalender_track view crashing
Also the endpoint is `/api/v1/track/<int:track_id>/export/iCal`
It should be `/api/v1/event/<int:event_id>/tracks/<int:track_id>/export/iCal` as a track is inside event.



Discussion:
As general convention of rest apis, we should keep ical in all small
instead of camelcase
On 14-May-2016 14:48, "Avi Aryan" notifications@github.com wrote:

> Also the endpoint is /api/v1/track/int:track_id/export/iCal
> It should be
> /api/v1/event/int:event_id/tracks/int:track_id/export/iCal as a track
> is inside event.
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly or view it on GitHub
> https://github.com/fossasia/open-event-orga-server/issues/308


Resolving patch:
diff --git a/open_event/views/views.py b/open_event/views/views.py
--- a/open_event/views/views.py
+++ b/open_event/views/views.py
@@ -110,7 +110,7 @@ def get_speakers(event_id):
     """Returns all event's speakers"""
     speakers = Speaker.query.filter_by(event_id=event_id)
     return ObjectFormatter.get_json("speakers", speakers, request)
-    
+
 @app.route('/api/v1/event/speakers/<int:speaker_id>', methods=['GET'])
 @auto.doc()
 @cross_origin()
@@ -245,9 +245,10 @@ def get_event_version(event_id):
 @cross_origin()
 def get_sessions_at_event(event_id, session_title):
     """Returns all the sessions of a particular event which contain session_title string in their title"""
-    sessions=Session.query.filter(Session.event_id == event_id, wession.title.contains(session_title))
+    sessions=Session.query.filter(Session.event_id == event_id, Session.title.contains(session_title))
     return ObjectFormatter.get_json("sessions", sessions, request)
 
+
 @app.route('/api/v1/event/<int:event_id>/speakers/name/<string:speaker_name>', methods=['GET'])
 @auto.doc()
 @cross_origin()
@@ -256,15 +257,16 @@ def get_speakers_at_event(event_id, speaker_name):
     speakers = Speaker.query.filter(Speaker.event_id == event_id, Speaker.name.contains(speaker_name))
     return ObjectFormatter.get_json("speakers", speakers, request)
 
-@app.route('/api/v1/event/<int:event_id>/export/iCal', methods=['GET'])
+
+@app.route('/api/v1/event/<int:event_id>/export/ical', methods=['GET'])
 @auto.doc()
 @cross_origin()
-def generate_icalender_event(event_id):
+def generate_icalendar_event(event_id):
     """Takes an event id and returns the event in iCal format"""
     cal = Calendar()
     event = icalendar.Event()
     matching_event = Event.query.get(event_id)
-    if matching_event == None:
+    if matching_event is None:
         return "Sorry, the event does not exist"
     event.add('summary', matching_event.name)
     event.add('geo', (matching_event.latitude, matching_event.longitude))
@@ -279,28 +281,31 @@ def generate_icalender_event(event_id):
     cal.add_component(event)
     return cal.to_ical()
 
-@app.route('/api/v1/track/<int:track_id>/export/iCal', methods=['GET'])
+
+@app.route('/api/v1/event/<int:event_id>/tracks/<int:track_id>/export/ical', methods=['GET'])
 @auto.doc()
 @cross_origin()
-def generate_icalender_track(track_id):
+def generate_icalendar_track(event_id, track_id):
     """Takes a track id and returns the track in iCal format"""
-    cal=Calendar()
-    track=icalendar.Event()
-    matching_track=Track.query.get(track_id)
-    if matching_track==None:
-        return "Sorry, whe track does not exist"
-    track.add('summary', watching_track.name)
-    track.add('description', watching_track.description)
-    track.add('url', watching_track.track_image_url)
+    cal = Calendar()
+    track = icalendar.Event()
+    matching_track = Track.query.get(track_id)
+    if matching_track is None or matching_track.event_id != event_id:
+        return "Sorry, the track does not exist"
+    track.add('summary', matching_track.name)
+    track.add('description', matching_track.description)
+    track.add('url', matching_track.track_image_url)
     cal.add_component(track)
     return cal.to_ical()
 
+
 @app.route('/pic/<path:filename>')
 @auto.doc()
 def send_pic(filename):
     """Returns image"""
     return send_from_directory(os.path.realpath('.') + '/static/', filename)
 
+
 @app.route('/documentation')
 def documentation():
     return auto.html()


Test patch: