def edit_settings(request): """Edit user settings""" if request.method == "POST": form = SettingsForm(request.POST) if form.is_valid(): form.save_for_user(request.user) messages.add_message(request, messages.INFO, _(u"Your settings have been saved.")) return HttpResponseRedirect(reverse("users.edit_settings")) # Invalid form return render(request, "users/edit_settings.html", {"form": form}) # Pass the current user's settings as the initial values. values = request.user.settings.values() initial = dict() for v in values: try: # Uses ast.literal_eval to convert 'False' => False etc. # TODO: Make more resilient. initial[v["name"]] = literal_eval(v["value"]) except (SyntaxError, ValueError): # Attempted to convert the string value to a Python value # but failed so leave it a string. initial[v["name"]] = v["value"] form = SettingsForm(initial=initial) return render(request, "users/edit_settings.html", {"form": form})
def edit_settings(request, template): """Edit user settings""" if request.method == 'POST': form = SettingsForm(request.POST) if form.is_valid(): form.save_for_user(request.user) messages.add_message(request, messages.INFO, _(u'Your settings have been saved.')) return HttpResponseRedirect(reverse('users.edit_settings')) # Invalid form return render(request, template, {'form': form}) # Pass the current user's settings as the initial values. values = request.user.settings.values() initial = dict() for v in values: try: # Uses ast.literal_eval to convert 'False' => False etc. # TODO: Make more resilient. initial[v['name']] = literal_eval(v['value']) except (SyntaxError, ValueError): # Attempted to convert the string value to a Python value # but failed so leave it a string. initial[v['name']] = v['value'] form = SettingsForm(initial=initial) return render(request, template, {'form': form})
def edit_settings(request): """Edit user settings""" template = "users/edit_settings.html" if request.method == "POST": form = SettingsForm(request.POST) if form.is_valid(): form.save_for_user(request.user) messages.add_message(request, messages.INFO, _("Your settings have been saved.")) return HttpResponseRedirect(reverse("users.edit_settings")) # Invalid form return render(request, template, {"form": form}) # Pass the current user's settings as the initial values. values = list(request.user.settings.values()) initial = dict() for val in values: try: # Uses ast.literal_eval to convert 'False' => False etc. # TODO: Make more resilient. initial[val["name"]] = literal_eval(val["value"]) except (SyntaxError, ValueError): # Attempted to convert the string value to a Python value # but failed so leave it a string. initial[val["name"]] = val["value"] form = SettingsForm(initial=initial) return render(request, template, {"form": form})
def get_for_user(cls, user, name): from kitsune.users.forms import SettingsForm form = SettingsForm() if name not in form.fields.keys(): raise KeyError(("'{name}' is not a field in " "user.forms.SettingsFrom()").format(name=name)) try: setting = Setting.uncached.get(user=user, name=name) except Setting.DoesNotExist: value = form.fields[name].initial or '' setting = Setting.objects.create(user=user, name=name, value=value) # Cast to the field's Python type. return form.fields[name].to_python(setting.value)
def test_non_existant_setting(self): form = SettingsForm() bad_setting = 'doesnt_exist' assert bad_setting not in form.fields.keys() with self.assertRaises(KeyError): Setting.get_for_user(self.u, bad_setting)