Repository: fossasia/open-event-server

Issue:
Upload of sponsorship images apparently not working
Added sponsorship images do not show up in the public page or on the dashboard.

Please ensure:
* [x] Images are uploaded

Resolving this issue, please also:
* [ ] Implement a preview in the upload section (step 2 of wizard) similar to the image upload in wizard step 1

![screenshot from 2016-11-12 12 20 47](https://cloud.githubusercontent.com/assets/1583873/20235656/dad98918-a8d2-11e6-8306-b5099569dbb9.png)


Discussion:
I will look into this.

@niranjan94 the preview system hasn't been added. I will add that and close this issue. 


Resolving patch:
diff --git a/app/helpers/data.py b/app/helpers/data.py
--- a/app/helpers/data.py
+++ b/app/helpers/data.py
@@ -1440,7 +1440,7 @@ def create_event(form, img_files):
             sponsor_url = form.getlist('sponsors[url]')
             sponsor_level = form.getlist('sponsors[level]')
             sponsor_description = form.getlist('sponsors[description]')
-            sponsor_logo_url = []
+            sponsor_logo = form.getlist('sponsors[logo]')
 
             if 'pay_by_stripe' in form:
                 if form.get('stripe_added', u'no') == u'yes':
@@ -1461,14 +1461,17 @@ def create_event(form, img_files):
                                           level=sponsor_level[index], description=sponsor_description[index],
                                           event_id=event.id)
                         save_to_db(sponsor, "Sponsor created")
-                        if len(img_files) != 0:
-                            img_url = upload(
-                                img_files[index],
+                        if sponsor_logo[index] != "":
+                            filename = '{}.png'.format(time.time())
+                            filepath = '{}/static/{}'.format(path.realpath('.'),
+                                sponsor_logo[index][len('/serve_static/'):])
+                            logo_file = UploadedFile(filepath, filename)
+                            logo = upload(
+                                logo_file,
                                 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]
+                            sponsor.logo = logo
                         else:
                             sponsor.logo = ""
                         save_to_db(sponsor, "Sponsor updated")
@@ -1884,7 +1887,7 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         social_link_id = form.getlist('social[id]')
 
         sponsor_name = form.getlist('sponsors[name]')
-        sponsor_logo_url = []
+        sponsor_logo = form.getlist('sponsors[logo]')
         sponsor_url = form.getlist('sponsors[url]')
         sponsor_type = form.getlist('sponsors[type]')
         sponsor_level = form.getlist('sponsors[level]')
@@ -2054,20 +2057,17 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
                                       level=sponsor_level[index], description=sponsor_description[index],
                                       event_id=event.id, sponsor_type=sponsor_type[index])
                     save_to_db(sponsor, "Sponsor created")
-                    if len(img_files) != 0:
-                        if img_files[index]:
-                            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:
-                            if name in old_sponsor_names:
-                                sponsor.logo = old_sponsor_logos[index]
-                            else:
-                                sponsor.logo = ""
+                    if sponsor_logo[index] != "":
+                        filename = '{}.png'.format(time.time())
+                        filepath = '{}/static/{}'.format(path.realpath('.'),
+                            sponsor_logo[index][len('/serve_static/'):])
+                        logo_file = UploadedFile(filepath, filename)
+                        logo = upload(
+                            logo_file,
+                            UPLOAD_PATHS['sponsors']['logo'].format(
+                                event_id=int(event.id), id=int(sponsor.id)
+                            ))
+                        sponsor.logo = logo
                     else:
                         if name in old_sponsor_names:
                             sponsor.logo = old_sponsor_logos[index]
diff --git a/app/views/admin/models_views/events.py b/app/views/admin/models_views/events.py
--- a/app/views/admin/models_views/events.py
+++ b/app/views/admin/models_views/events.py
@@ -117,6 +117,20 @@ def create_event_logo_upload(self):
             else:
                 return jsonify({'status': 'no logo'})
 
+    @expose('/create/files/sponsor_logo', methods=('POST',))
+    def create_sponsor_logo_upload(self):
+        if request.method == 'POST':
+            logo_image = request.form['logo']
+            if logo_image:
+                logo_file = uploaded_file(file_content=logo_image)
+                logo = upload_local(
+                    logo_file,
+                    UPLOAD_PATHS['temp']['event'].format(uuid=uuid4())
+                )
+                return jsonify({'status': 'ok', 'logo': logo})
+            else:
+                return jsonify({'status': 'no logo'})
+
     @expose('/create/', methods=('GET', 'POST'))
     def create_view(self, ):
         if request.method == 'POST':
@@ -299,6 +313,29 @@ def logo_upload(self, event_id):
             save_to_db(event)
             return jsonify({'status': 'ok'})
 
+    @expose('/<int:event_id>/sponsor/<int:sponsor_id>/editfiles/sponsor_logo', methods=('POST', 'DELETE'))
+    def sponsor_logo_upload(self, event_id, sponsor_id):
+        if request.method == 'POST':
+            logo_image = request.form['logo']
+            if logo_image:
+                logo_file = uploaded_file(file_content=logo_image)
+                logo = upload(
+                    logo_file,
+                    UPLOAD_PATHS['sponsors']['logo'].format(
+                        event_id=event_id, id=sponsor_id
+                    ))
+                sponsor = DataGetter.get_sponsor(sponsor_id)
+                sponsor.logo = logo
+                save_to_db(sponsor)
+                return jsonify({'status': 'ok', 'logo': logo})
+            else:
+                return jsonify({'status': 'no logo'})
+        elif request.method == 'DELETE':
+            sponsor = DataGetter.get_sponsor(sponsor_id)
+            sponsor.logo = ''
+            save_to_db(sponsor)
+            return jsonify({'status': 'ok'})
+
     @expose('/<event_id>/edit/', methods=('GET', 'POST'))
     @can_access
     def edit_view(self, event_id):


Test patch:
diff --git a/tests/unittests/views/admin/test_events.py b/tests/unittests/views/admin/test_events.py
--- a/tests/unittests/views/admin/test_events.py
+++ b/tests/unittests/views/admin/test_events.py
@@ -45,6 +45,7 @@ def test_events_create_post(self):
             data['sponsors[type]'] = ['Gold', 'Silver']
             data['sponsors[url]'] = ["", ""]
             data['sponsors[description]'] = ["", ""]
+            data['sponsors[logo]'] = ["", ""]
             data['sponsors[level]'] = ["", ""]
             data['start_date'] = '07/04/2016'
             data['start_time'] = '19:00'
@@ -163,6 +164,7 @@ def test_events_edit(self):
             data['sponsors[name]'] = ['Sponsor 1', 'Sponsor 2']
             data['sponsors[type]'] = ['Gold', 'Silver']
             data['sponsors[url]'] = ["", ""]
+            data['sponsors[logo]'] = ["", ""]
             data['sponsors[description]'] = ["", ""]
             data['sponsors[level]'] = ["", ""]
             data['name'] = 'EditTestName'