mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
Preferences form
This commit is contained in:
@@ -11,6 +11,12 @@ from django.forms import ModelForm
|
||||
from django.utils.html import escape, mark_safe
|
||||
|
||||
|
||||
TRUE_FALSE_CHOICES = (
|
||||
(True, 'Yes'),
|
||||
(False, 'No')
|
||||
)
|
||||
|
||||
|
||||
def _(x):
|
||||
# dummy gettext
|
||||
return x
|
||||
@@ -101,3 +107,13 @@ class SetPasswordForm(BaseSetPasswordForm):
|
||||
self.fields['new_password1'].help_text = "At least 8 characters"
|
||||
|
||||
self.fields['new_password2'].help_text = None # "Confirm password" is descriptive enough
|
||||
|
||||
|
||||
class PreferencesForm(ModelForm):
|
||||
# I haven't gotten a decent display for checkboxes in forms yet; the quickest hack around this is a ChoiceField
|
||||
send_email_alerts = forms.ChoiceField(
|
||||
label=_("Send email alerts"), choices=TRUE_FALSE_CHOICES, required=False, widget=forms.Select())
|
||||
|
||||
class Meta:
|
||||
model = UserModel
|
||||
fields = ("send_email_alerts",)
|
||||
|
||||
37
users/templates/users/preferences.html
Normal file
37
users/templates/users/preferences.html
Normal file
@@ -0,0 +1,37 @@
|
||||
{% extends "base.html" %}
|
||||
{% load static %}
|
||||
{% load tailwind_forms %}
|
||||
|
||||
{% block title %}User Preferences · {{ site_title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
|
||||
<div class="flex items-center justify-center">
|
||||
|
||||
<div class="m-4 max-w-4xl flex-auto">
|
||||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{% if messages %}
|
||||
<ul>
|
||||
{% for message in messages %}
|
||||
{# if we introduce different levels we can use{% message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %} #}
|
||||
<li class="bg-cyan-50 border-2 border-cyan-800 p-4 rounded-lg">{{ message }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
<div>
|
||||
<h1 class="text-4xl my-4 font-bold">User Preferences</h1>
|
||||
</div>
|
||||
|
||||
{% tailwind_formfield form.send_email_alerts %}
|
||||
|
||||
<button name="action" value="invite" class="font-bold text-slate-800 border-slate-500 pl-4 pr-4 pb-2 pt-2 border-2 bg-cyan-200 hover:bg-cyan-400 active:ring rounded-md">Save</button>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -5,10 +5,13 @@ from django.shortcuts import render, redirect, reverse
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.http import Http404
|
||||
from django.utils import timezone
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
from bugsink.app_settings import get_settings, CB_ANYBODY
|
||||
from bugsink.decorators import atomic_for_request_method
|
||||
|
||||
from .forms import UserCreationForm, ResendConfirmationForm, RequestPasswordResetForm, SetPasswordForm
|
||||
from .forms import UserCreationForm, ResendConfirmationForm, RequestPasswordResetForm, SetPasswordForm, PreferencesForm
|
||||
from .models import EmailVerification
|
||||
from .tasks import send_confirm_email, send_reset_email
|
||||
|
||||
@@ -150,6 +153,28 @@ def reset_password(request, token=None):
|
||||
return render(request, "users/reset_password.html", {"form": form, "next": next})
|
||||
|
||||
|
||||
@atomic_for_request_method
|
||||
# in the general case this is done by Middleware but we're under /accounts/. not security-critical because we simply
|
||||
# get a failure on request.user if this wasn't there, but still the "right thing"
|
||||
@login_required
|
||||
def preferences(request):
|
||||
user = request.user
|
||||
if request.method == 'POST':
|
||||
form = PreferencesForm(request.POST, instance=user)
|
||||
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, "Updated preferences")
|
||||
return redirect('preferences')
|
||||
|
||||
else:
|
||||
form = PreferencesForm(instance=user)
|
||||
|
||||
return render(request, 'users/preferences.html', {
|
||||
'form': form,
|
||||
})
|
||||
|
||||
|
||||
DEBUG_CONTEXTS = {
|
||||
"confirm_email": {
|
||||
"site_title": get_settings().SITE_TITLE,
|
||||
|
||||
Reference in New Issue
Block a user