Repository: fossasia/open-event-server

Issue:
Implement "Turn off Sponsors" button
Please implement "Turn off/on" button in sponsors button.
- [x] Standard is "Turned Off".
- [x] If sponsors are switched off, show sponsors box in gray in checklist on specific event overview page



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
@@ -975,23 +975,24 @@ def create_event(form, img_files):
                                          event_id=event.id)
                     db.session.add(room)
 
-            for index, name in enumerate(sponsor_name):
-                if not string_empty(name):
-                    sponsor = Sponsor(name=name, url=sponsor_url[index],
-                                      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],
-                            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:
-                        sponsor.logo = ""
-                    save_to_db(sponsor, "Sponsor updated")
+            if form.get('sponsors_state', u'off') == u'on':
+                for index, name in enumerate(sponsor_name):
+                    if not string_empty(name):
+                        sponsor = Sponsor(name=name, url=sponsor_url[index],
+                                          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],
+                                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:
+                            sponsor.logo = ""
+                        save_to_db(sponsor, "Sponsor updated")
 
             session_form = ""
             speaker_form = ""
@@ -1291,33 +1292,34 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         for sponsor in sponsors:
             delete_from_db(sponsor, "Sponsor Deleted")
 
-        for index, name in enumerate(sponsor_name):
-            if not string_empty(name):
-                sponsor = Sponsor(name=name, url=sponsor_url[index],
-                                  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]
+        if form.get('sponsors_state', u'off') == u'on':
+            for index, name in enumerate(sponsor_name):
+                if not string_empty(name):
+                    sponsor = Sponsor(name=name, url=sponsor_url[index],
+                                      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 = ""
                     else:
                         if name in old_sponsor_names:
                             sponsor.logo = old_sponsor_logos[index]
                         else:
                             sponsor.logo = ""
-                else:
-                    if name in old_sponsor_names:
-                        sponsor.logo = old_sponsor_logos[index]
-                    else:
-                        sponsor.logo = ""
-                print sponsor.logo
-                save_to_db(sponsor, "Sponsor updated")
+                    print sponsor.logo
+                    save_to_db(sponsor, "Sponsor updated")
 
         session_form = ""
         speaker_form = ""
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
@@ -107,7 +107,7 @@ def details_view(self, event_id):
 
         sponsors = DataGetter.get_sponsors(event_id).all()
         if not sponsors:
-            checklist["2"] = 'missing_main'
+            checklist["2"] = 'optional'
         else:
             for sponsor in sponsors:
                 if fields_not_empty(sponsor, ['name', 'description', 'url', 'level', 'logo']):


Test patch:
diff --git a/tests/views/admin/test_events.py b/tests/views/admin/test_events.py
--- a/tests/views/admin/test_events.py
+++ b/tests/views/admin/test_events.py
@@ -30,6 +30,7 @@ def test_events_create_post(self):
             url = url_for('events.create_view')
             data = POST_EVENT_DATA.copy()
             del data['copyright']
+            data['sponsors_state'] = 'on'
             data['sponsors[name]'] = ['Sponsor 1', 'Sponsor 2']
             data['sponsors[type]'] = ['Gold', 'Silver']
             data['sponsors[url]'] = ["", ""]
@@ -91,6 +92,7 @@ def test_events_edit(self):
             url = url_for('events.edit_view', event_id=event.id)
             data = POST_EVENT_DATA.copy()
             del data['copyright']
+            data['sponsors_state'] = 'on'
             data['sponsors[name]'] = ['Sponsor 1', 'Sponsor 2']
             data['sponsors[type]'] = ['Gold', 'Silver']
             data['sponsors[url]'] = ["", ""]