Repository: fossasia/open-event-server

Issue:
Use storage keys from the storage module rather than hard-coding
Storage keys for various type of media are defined in storage.py under UPLOAD_PATHS variable.
The codebase should refer to that variable for upload keys and not hard-code them when calling the upload function. 



Discussion:

Resolving patch:
diff --git a/app/helpers/data.py b/app/helpers/data.py
--- a/app/helpers/data.py
+++ b/app/helpers/data.py
@@ -21,7 +21,7 @@
 from app.helpers.notification_email_triggers import trigger_new_session_notifications, \
     trigger_session_state_change_notifications
 from app.helpers.oauth import OAuth, FbOAuth, InstagramOAuth
-from app.helpers.storage import upload, UploadedFile
+from app.helpers.storage import upload, UploadedFile, UPLOAD_PATHS
 from app.models.notifications import Notification
 from ..helpers import helpers as Helper
 from ..helpers.data_getter import DataGetter
@@ -343,23 +343,35 @@ def add_session_to_event(request, event_id, state=None):
         speaker_modified = False
         session_modified = False
         if slide_file != "":
-            slide_url = upload(slide_file,
-                               'events/%d/session/%d/slide' % (int(event_id), int(new_session.id)))
+            slide_url = upload(
+                slide_file,
+                UPLOAD_PATHS['sessions']['slides'].format(
+                    event_id=int(event_id), id=int(new_session.id)
+                ))
             new_session.slides = slide_url
             session_modified = True
         if audio_file != "":
-            audio_url = upload(audio_file,
-                               'events/%d/session/%d/audio' % (int(event_id), int(new_session.id)))
+            audio_url = upload(
+                audio_file,
+                UPLOAD_PATHS['sessions']['audio'].format(
+                    event_id=int(event_id), id=int(new_session.id)
+                ))
             new_session.audio = audio_url
             session_modified = True
         if video_file != "":
-            video_url = upload(video_file,
-                               'events/%d/session/%d/video' % (int(event_id), int(new_session.id)))
+            video_url = upload(
+                video_file,
+                UPLOAD_PATHS['sessions']['video'].format(
+                    event_id=int(event_id), id=int(new_session.id)
+                ))
             new_session.video = video_url
             session_modified = True
         if speaker_img_file != "":
-            speaker_img = upload(speaker_img_file,
-                                 'events/%d/speaker/%d/photo' % (int(event_id), int(speaker.id)))
+            speaker_img = upload(
+                speaker_img_file,
+                UPLOAD_PATHS['speakers']['photo'].format(
+                    event_id=int(event_id), id=int(speaker.id)
+                ))
             speaker.photo = speaker_img
             speaker_modified = True
 
@@ -411,8 +423,11 @@ def add_speaker_to_event(request, event_id, user=login.current_user):
             record_activity('create_speaker', speaker=speaker, event_id=event_id)
         speaker_img = ""
         if speaker_img_file != "":
-            speaker_img = upload(speaker_img_file,
-                                 'events/%d/speaker/%d/photo' % (int(event_id), int(speaker.id)))
+            speaker_img = upload(
+                speaker_img_file,
+                UPLOAD_PATHS['speakers']['photo'].format(
+                    event_id=int(event_id), id=int(speaker.id)
+                ))
             speaker.photo = speaker_img
             save_to_db(speaker, "Speaker photo saved")
             record_activity('update_speaker', speaker=speaker, event_id=event_id)
@@ -473,17 +488,26 @@ def edit_session(request, session):
         form_state = form.get('state', 'draft')
 
         if slide_file != "":
-            slide_url = upload(slide_file,
-                               'events/%d/session/%d/slide' % (int(event_id), int(session.id)))
+            slide_url = upload(
+                slide_file,
+                UPLOAD_PATHS['sessions']['slides'].format(
+                    event_id=int(event_id), id=int(session.id)
+                ))
             session.slides = slide_url
 
         if audio_file != "":
-            audio_url = upload(audio_file,
-                               'events/%d/session/%d/audio' % (int(event_id), int(session.id)))
+            audio_url = upload(
+                audio_file,
+                UPLOAD_PATHS['sessions']['audio'].format(
+                    event_id=int(event_id), id=int(session.id)
+                ))
             session.audio = audio_url
         if video_file != "":
-            video_url = upload(video_file,
-                               'events/%d/session/%d/video' % (int(event_id), int(session.id)))
+            video_url = upload(
+                video_file,
+                UPLOAD_PATHS['sessions']['video'].format(
+                    event_id=int(event_id), id=int(session.id)
+                ))
             session.video = video_url
 
         if form_state == 'pending' and session.state != 'pending' and session.state != 'accepted' and session.state != 'rejected':
@@ -898,7 +922,11 @@ def create_event(form, img_files):
                 fh.write(background_image.split(",")[1].decode('base64'))
                 fh.close()
                 background_file = UploadedFile(file_path, filename)
-                background_url = upload(background_file, 'events/%d/background_image' % (int(event.id)))
+                background_url = upload(
+                    background_file,
+                    UPLOAD_PATHS['event']['background_url'].format(
+                        event_id=int(event.id)
+                    ))
                 event.background_url = background_url
 
             logo = form['logo']
@@ -909,7 +937,11 @@ def create_event(form, img_files):
                 fh.write(logo.split(",")[1].decode('base64'))
                 fh.close()
                 logo_file = UploadedFile(file_path, filename)
-                logo = upload(logo_file, 'events/%d/logo' % (int(event.id)))
+                logo = upload(
+                    logo_file,
+                    UPLOAD_PATHS['event']['logo'].format(
+                        event_id=int(event.id)
+                    ))
                 event.logo = logo
 
             for index, name in enumerate(session_type_names):
@@ -945,8 +977,11 @@ def create_event(form, img_files):
                                       event_id=event.id)
                     save_to_db(sponsor, "Sponsor created")
                     if len(img_files) != 0:
-                        img_url = upload(img_files[index],
-                                         'events/%d/sponsor/%d/image' % (int(event.id), int(sponsor.id)))
+                        img_url = upload(
+                            img_files[index],
+                            UPLOAD_PATHS['sponsors']['logo'].format(
+                                event_id=int(event.id), id=int(sponsor.id)
+                            ))
                         sponsor_logo_url.append(img_url)
                         sponsor.logo = sponsor_logo_url[index]
                     else:
@@ -1104,7 +1139,11 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
             fh.write(background_image.split(",")[1].decode('base64'))
             fh.close()
             background_file = UploadedFile(file_path, filename)
-            background_url = upload(background_file, 'events/%d/background_image' % (int(event.id)))
+            background_url = upload(
+                background_file,
+                UPLOAD_PATHS['event']['background_url'].format(
+                    event_id=int(event.id)
+                ))
             event.background_url = background_url
 
         logo = form['logo']
@@ -1115,7 +1154,11 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
             fh.write(logo.split(",")[1].decode('base64'))
             fh.close()
             logo_file = UploadedFile(file_path, filename)
-            logo = upload(logo_file, 'events/%d/logo' % (int(event.id)))
+            logo = upload(
+                logo_file,
+                UPLOAD_PATHS['event']['logo'].format(
+                    event_id=int(event.id)
+                ))
             event.logo = logo
 
         state = form.get('state', None)
@@ -1242,8 +1285,11 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
                 save_to_db(sponsor, "Sponsor created")
                 if len(img_files) != 0:
                     if img_files[index]:
-                        img_url = upload(img_files[index],
-                                         'events/%d/sponsor/%d/image' % (int(event.id), int(sponsor.id)))
+                        img_url = upload(
+                            img_files[index],
+                            UPLOAD_PATHS['sponsors']['logo'].format(
+                                event_id=int(event.id), id=int(sponsor.id)
+                            ))
                         sponsor_logo_url.append(img_url)
                         sponsor.logo = sponsor_logo_url[index]
                     else:
diff --git a/app/helpers/storage.py b/app/helpers/storage.py
--- a/app/helpers/storage.py
+++ b/app/helpers/storage.py
@@ -29,6 +29,9 @@
     },
     'tracks': {
         'track_image_url': 'events/{event_id}/tracks/{id}/track_image'
+    },
+    'user': {
+        'avatar': 'users/{user_id}/avatar'
     }
 }
 
diff --git a/app/views/admin/models_views/profile.py b/app/views/admin/models_views/profile.py
--- a/app/views/admin/models_views/profile.py
+++ b/app/views/admin/models_views/profile.py
@@ -6,7 +6,7 @@
 from app.views.admin.models_views.events import is_verified_user
 from ....helpers.data import DataManager, get_facebook_auth, get_instagram_auth
 from ....helpers.data_getter import DataGetter
-from app.helpers.storage import upload
+from app.helpers.storage import upload, UPLOAD_PATHS
 from app.helpers.oauth import OAuth, FbOAuth, InstagramOAuth
 
 
@@ -40,7 +40,11 @@ def edit_view(self, user_id=None):
             url = ""
             if 'avatar' in request.files and request.files['avatar'].filename != "":
                 avatar_img = request.files['avatar']
-                url = upload(avatar_img, 'users/%d/avatar' % int(user_id))
+                url = upload(
+                    avatar_img,
+                    UPLOAD_PATHS['user']['avatar'].format(
+                        user_id=int(user_id)
+                    ))
             profile = DataManager.update_user(request.form, int(user_id), url)
             if admin:
                 return redirect(url_for('sadmin_users.details_view', user_id=user_id))


Test patch: