Repository: fossasia/open-event-server

Issue:
Exception (AttributeError) when accepting or rejecting a session
AttributeError: 'NoneType' object has no attribute 'user_control_status'

The last lines of the traceback make clear what went wrong. 

```
DataManager.session_accept_reject(session, event_id, 'accepted')
File "/home/aviaryan/github/open-event-orga-server/app/helpers/data.py", line 498, in session_accept_reject
trigger_session_state_change_notifications(session, event_id)
File "/home/aviaryan/github/open-event-orga-server/app/helpers/notification_email_triggers.py", line 37, in trigger_session_state_change_notifications
admin_msg_setting.user_control_status == 1) or not admin_msg_setting or admin_msg_setting.user_control_status == 0:
AttributeError: 'NoneType' object has no attribute 'user_control_status'
```

The issue is in `helpers.notification_email_triggers` file. The error happens when admin_msg_setting is None. I noticed that if conditions are like - 

``` python
if (email_notification_setting and email_notification_setting.new_paper == 1
                and admin_msg_setting.user_control_status == 1) or not admin_msg_setting or admin_msg_setting.user_control_status == 0:
```

The ordering of `or` conditions is faulty. Keeping `not admin_msg_setting` as first part, this issue will be solved. 



Discussion:

Resolving patch:
diff --git a/app/helpers/notification_email_triggers.py b/app/helpers/notification_email_triggers.py
--- a/app/helpers/notification_email_triggers.py
+++ b/app/helpers/notification_email_triggers.py
@@ -18,8 +18,10 @@ def trigger_new_session_notifications(session_id, event_id=None, event=None):
     organizers = DataGetter.get_user_event_roles_by_role_name(event.id, 'organizer')
     for organizer in organizers:
         email_notification_setting = DataGetter.get_email_notification_settings_by_event_id(organizer.user.id, event_id)
-        if (email_notification_setting and email_notification_setting.new_paper == 1
-                and admin_msg_setting.user_control_status == 1) or not admin_msg_setting or admin_msg_setting.user_control_status == 0:
+        if not admin_msg_setting or \
+                (email_notification_setting and email_notification_setting.new_paper == 1 and
+                    admin_msg_setting.user_control_status == 1) or \
+                admin_msg_setting.user_control_status == 0:
             send_new_session_organizer(organizer.user.email, event.name, link)
         # Send notification
         print " notif Sending"
@@ -33,8 +35,10 @@ def trigger_session_state_change_notifications(session, event_id, state=None):
     admin_msg_setting = DataGetter.get_message_setting_by_action(SESSION_ACCEPT_REJECT)
     for speaker in session.speakers:
         email_notification_setting = DataGetter.get_email_notification_settings_by_event_id(speaker.user_id, event_id)
-        if (email_notification_setting and email_notification_setting.session_accept_reject == 1 and
-                admin_msg_setting.user_control_status == 1) or not admin_msg_setting or admin_msg_setting.user_control_status == 0:
+        if not admin_msg_setting or \
+                (email_notification_setting and email_notification_setting.session_accept_reject == 1 and
+                    admin_msg_setting.user_control_status == 1) or \
+                admin_msg_setting.user_control_status == 0:
             send_session_accept_reject(speaker.email, session.title, state, link)
             # Send notification
         send_notif_session_accept_reject(speaker.user, session.title, state, link)
@@ -45,8 +49,10 @@ def trigger_session_schedule_change_notifications(session, event_id):
     admin_msg_setting = DataGetter.get_message_setting_by_action(SESSION_SCHEDULE)
     for speaker in session.speakers:
         email_notification_setting = DataGetter.get_email_notification_settings_by_event_id(speaker.user_id, event_id)
-        if (email_notification_setting and email_notification_setting.session_schedule == 1 and
-                admin_msg_setting.user_control_status == 1) or not admin_msg_setting or admin_msg_setting.user_control_status == 0:
+        if not admin_msg_setting or \
+                (email_notification_setting and email_notification_setting.session_schedule == 1 and
+                    admin_msg_setting.user_control_status == 1) or \
+                admin_msg_setting.user_control_status == 0:
             send_schedule_change(speaker.email, session.title, link)
             # Send notification
         send_notif_session_schedule(speaker.user, session.title, link)


Test patch: