def save_settings(): errors = defaultdict(list) # Validate the time format. time_format = flask.request.form['time_format'] if time_format is None: errors[_TIME_FORMAT_ERROR].append('missing') elif time_format not in db._SETTINGS_TIME_FORMATS: errors[_TIME_FORMAT_ERROR].append('invalid') # Validate the country code. country = flask.request.form.get('country', None) if not country: country = None if country: if (len(country) != 2) or (country not in _COUNTRY_CODE_TO_TIME_ZONE_MAP): errors[_COUNTRY_ERROR].append('invalid') # Validate the time zone. time_zone = flask.request.form.get('time_zone', None) if not time_zone: time_zone = None if time_zone: if not country: # Cannot have a time zone without a country specified. errors[_COUNTRY_ERROR].append('missing') else: # Validate the time zone in the selected country. country_time_zones = (time_zone.zone for time_zone in _COUNTRY_CODE_TO_TIME_ZONE_MAP[country].itervalues()) if time_zone not in country_time_zones: errors[_TIME_ZONE_ERROR].append('invalid') # Update the client settings if there are no errors. if not errors: # Save the settings in the database. db.save_settings(flask.g.client_id, time_format, country, time_zone) # Store the settings in the session. client = flask.session['client'] client['time_format'] = time_format if time_zone: client['time_zone'] = time_zone else: client.pop('time_zone', None) # Changes to mutable data are not picked up automatically. flask.session.modified = True saved = not errors return _render_settings(time_format, country, time_zone, errors, saved)
def save_settings(): require_login() db.save_settings(flask.g.user_id, flask.request.get_json()) return flask.jsonify(ok=True)