Repository: fossasia/open-event-server

Issue:
Turn on/off all email notifications
Follow up of issue #1066 

Things to be done:-
- [x] Implement Turn ON/OFF button.



Discussion:

Resolving patch:
diff --git a/open_event/helpers/data.py b/open_event/helpers/data.py
--- a/open_event/helpers/data.py
+++ b/open_event/helpers/data.py
@@ -106,12 +106,37 @@ def create_track(form, event_id):
         record_activity('create_track', event_id=event_id, track=new_track)
         update_version(event_id, False, "tracks_ver")
 
+    @staticmethod
+    def toggle_email_notification_settings(user_id, value):
+        """
+        Settings will be toggled to database with proper User id
+        """
+        events = DataGetter.get_all_events()
+        for event in events:
+            email_notification = DataGetter.get_email_notification_settings_by_event_id(user_id, event.id)
+            if email_notification:
+                email_notification.next_event = value
+                email_notification.new_paper = value
+                email_notification.session_schedule = value
+                email_notification.session_accept_reject = value
+
+                save_to_db(email_notification, "EmailSettings Toggled")
+            else:
+                new_email_notification_setting = EmailNotification(next_event=0,
+                                                                   new_paper=0,
+                                                                   session_schedule=0,
+                                                                   session_accept_reject=0,
+                                                                   user_id=user_id,
+                                                                   event_id=event.id)
+                save_to_db(new_email_notification_setting, "EmailSetting Toggled")
+
     @staticmethod
     def add_email_notification_settings(form, user_id, event_id):
         """
-        Track will be saved to database with proper Event id
+        Settings will be saved to database with proper Event id and User id
         :param form: view data form
-        :param event_id: Track belongs to Event by event id
+        :param event_id: Settings belongs to Event by event id
+        :param user_id: Settings belongs to User by user id
         """
         email_notification_setting = DataGetter.get_email_notification_settings_by_event_id(user_id, event_id)
         if email_notification_setting:
diff --git a/open_event/views/admin/models_views/settings.py b/open_event/views/admin/models_views/settings.py
--- a/open_event/views/admin/models_views/settings.py
+++ b/open_event/views/admin/models_views/settings.py
@@ -31,3 +31,11 @@ def edit_view(self, event_id):
         event = DataGetter.get_event(event_id)
         settings = DataGetter.get_email_notification_settings_by_event_id(login.current_user.id, event_id)
         return self.render('/gentelella/admin/settings/edit.html', settings=settings, event=event)
+
+    @expose('/toggle', methods=('GET', 'POST'))
+    def toggle_view(self):
+        if request.method == 'POST':
+            value = request.form.get('value')
+            print value
+            settings = DataManager.toggle_email_notification_settings(login.current_user.id, value)
+            return "success"


Test patch: