Don't post checkbox data directly to /json/notify_settings/change.

They have weird properties like not sending anything for unchecked
boxes, which makes it hard to wrap a client-agnostic API around.

(imported from commit fef73a57a55b218b55dab6be3453dd6eac73c789)
This commit is contained in:
Jessica McKellar 2014-02-12 11:22:56 -05:00
parent 7829e809c7
commit e0bd15669a
4 changed files with 57 additions and 33 deletions

View File

@ -1131,10 +1131,28 @@ $(function () {
.text(response).stop(true).fadeTo(0,1);
}
$("form.notify-settings").expectOne().ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: update_notification_settings_success,
error: update_notification_settings_error
function post_notify_settings_changes(notification_changes, success_func,
error_func) {
return channel.post({
url: "/json/notify_settings/change",
data: notification_changes,
success: success_func,
error: error_func
});
}
$("#change_notification_settings").on("click", function (e) {
var updated_settings = {};
_.each(["enable_stream_desktop_notifications", "enable_stream_sounds",
"enable_desktop_notifications", "enable_sounds",
"enable_offline_email_notifications",
"enable_offline_push_notifications", "enable_digest_emails"],
function (setting) {
updated_settings[setting] = $("#" + setting).is(":checked");
});
post_notify_settings_changes(updated_settings,
update_notification_settings_success,
update_notification_settings_error);
});
if (feature_flags.show_autoscroll_forever_option) {

View File

@ -88,8 +88,7 @@
</div>
</div>
<form action="/json/notify_settings/change" method="post"
class="form-horizontal notify-settings">{% csrf_token %}
<div>
<div id="notification-settings" class="settings-section">
<div class="settings-section-title"><i class="icon-vector-warning-sign settings-section-icon"></i>Notifications</div>
<div class="alert" id="notify-settings-status"></div>
@ -197,14 +196,16 @@
<div class="control-group">
<div class="controls notification-submission">
<input type="submit" name="change_settings" value="Save changes" class="btn btn-big btn-primary" />
<input type="submit" id="change_notification_settings"
name="change_notification_settings" value="Save changes"
class="btn btn-big btn-primary" />
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<div id="bot-settings" class="settings-section">

View File

@ -528,7 +528,8 @@ class ChangeSettingsTest(AuthedTestCase):
def test_notify_settings(self):
# This is basically a don't-explode test.
self.login("hamlet@zulip.com")
json_result = self.client.post("/json/notify_settings/change", {})
json_result = self.client.post("/json/notify_settings/change",
{"enable_desktop_notifications": ujson.dumps(False)})
self.assert_json_success(json_result)
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
enable_desktop_notifications, False)

View File

@ -1475,56 +1475,60 @@ def json_change_settings(request, user_profile,
@authenticated_json_post_view
@has_request_variables
def json_change_notify_settings(request, user_profile,
# Notifications default to False because
# browsers POST nothing for an unchecked
# checkbox.
enable_stream_desktop_notifications=REQ(converter=lambda x: x == "on",
default=False),
enable_stream_sounds=REQ(converter=lambda x: x == "on",
default=False),
enable_desktop_notifications=REQ(converter=lambda x: x == "on",
default=False),
enable_sounds=REQ(converter=lambda x: x == "on",
default=False),
enable_offline_email_notifications=REQ(converter=lambda x: x == "on",
default=False),
enable_offline_push_notifications=REQ(converter=lambda x: x == "on",
default=False),
enable_digest_emails=REQ(converter=lambda x: x == "on",
default=False)):
enable_stream_desktop_notifications=REQ(validator=check_bool,
default=None),
enable_stream_sounds=REQ(validator=check_bool,
default=None),
enable_desktop_notifications=REQ(validator=check_bool,
default=None),
enable_sounds=REQ(validator=check_bool,
default=None),
enable_offline_email_notifications=REQ(validator=check_bool,
default=None),
enable_offline_push_notifications=REQ(validator=check_bool,
default=None),
enable_digest_emails=REQ(validator=check_bool,
default=None)):
result = {}
# Stream notification settings.
if user_profile.enable_stream_desktop_notifications != enable_stream_desktop_notifications:
if enable_stream_desktop_notifications is not None and \
user_profile.enable_stream_desktop_notifications != enable_stream_desktop_notifications:
do_change_enable_stream_desktop_notifications(
user_profile, enable_stream_desktop_notifications)
result['enable_stream_desktop_notifications'] = enable_stream_desktop_notifications
if user_profile.enable_stream_sounds != enable_stream_sounds:
if enable_stream_sounds is not None and \
user_profile.enable_stream_sounds != enable_stream_sounds:
do_change_enable_stream_sounds(user_profile, enable_stream_sounds)
result['enable_stream_sounds'] = enable_stream_sounds
# PM and @-mention settings.
if user_profile.enable_desktop_notifications != enable_desktop_notifications:
if enable_desktop_notifications is not None and \
user_profile.enable_desktop_notifications != enable_desktop_notifications:
do_change_enable_desktop_notifications(user_profile, enable_desktop_notifications)
result['enable_desktop_notifications'] = enable_desktop_notifications
if user_profile.enable_sounds != enable_sounds:
if enable_sounds is not None and \
user_profile.enable_sounds != enable_sounds:
do_change_enable_sounds(user_profile, enable_sounds)
result['enable_sounds'] = enable_sounds
if user_profile.enable_offline_email_notifications != enable_offline_email_notifications:
if enable_offline_email_notifications is not None and \
user_profile.enable_offline_email_notifications != enable_offline_email_notifications:
do_change_enable_offline_email_notifications(user_profile, enable_offline_email_notifications)
result['enable_offline_email_notifications'] = enable_offline_email_notifications
if user_profile.enable_offline_push_notifications != enable_offline_push_notifications:
if enable_offline_push_notifications is not None and \
user_profile.enable_offline_push_notifications != enable_offline_push_notifications:
do_change_enable_offline_push_notifications(user_profile, enable_offline_push_notifications)
result['enable_offline_push_notifications'] = enable_offline_push_notifications
if user_profile.enable_digest_emails != enable_digest_emails:
if enable_digest_emails is not None and \
user_profile.enable_digest_emails != enable_digest_emails:
do_change_enable_digest_emails(user_profile, enable_digest_emails)
result['enable_digest_emails'] = enable_digest_emails