Example #1
0
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})
Example #2
0
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})
Example #3
0
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})