Add a user settings form

This commit is contained in:
Raphael Michel
2015-05-22 09:15:38 +02:00
parent 2d83049181
commit 10373838f1
6 changed files with 154 additions and 9 deletions

View File

@@ -48,7 +48,7 @@
</li>
</ul>
<ul class="nav navbar-top-links navbar-right">
<li><a href="#"><i class="fa fa-user"></i> {{ request.user.get_full_name }}</a></li>
<li><a href="{% url 'control:user.settings' %}"><i class="fa fa-user"></i> {{ request.user.get_full_name }}</a></li>
<li><a href="{% url 'control:auth.logout' %}" title="{% trans "Log out" %}"><i class="fa fa-sign-out"></i><span class="visible-xs-inline">{% trans "Log out" %}</span></a></li>
</ul>
<div class="navbar-default sidebar" role="navigation">

View File

@@ -0,0 +1,30 @@
{% extends "pretixcontrol/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% block title %}{% trans "Account settings" %}{% endblock %}
{% block content %}
<h1>{% trans "Account settings" %}</h1>
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
{% bootstrap_form_errors form %}
<fieldset>
<legend>{% trans "General settings" %}</legend>
{% bootstrap_field form.givenname layout='horizontal' %}
{% bootstrap_field form.familyname layout='horizontal' %}
{% bootstrap_field form.locale layout='horizontal' %}
{% bootstrap_field form.timezone layout='horizontal' %}
</fieldset>
<fieldset>
<legend>{% trans "Login settings" %}</legend>
{% bootstrap_field form.old_pw layout='horizontal' %}
{% bootstrap_field form.email layout='horizontal' %}
{% bootstrap_field form.new_pw layout='horizontal' %}
{% bootstrap_field form.new_pw_repeat layout='horizontal' %}
</fieldset>
<div class="form-group submit-group">
<button type="submit" class="btn btn-primary btn-save">
{% trans "Save" %}
</button>
</div>
</form>
{% endblock %}

View File

@@ -1,12 +1,13 @@
from django.conf.urls import url, include
from pretix.control.views import main, event, item, auth, orders
from pretix.control.views import main, event, item, auth, orders, user
urlpatterns = [
url(r'^logout$', auth.logout, name='auth.logout'),
url(r'^login$', auth.login, name='auth.login'),
url(r'^$', main.index, name='index'),
url(r'^settings$', user.UserSettings.as_view(), name='user.settings'),
url(r'^events/$', main.EventList.as_view(), name='events'),
url(r'^event/(?P<organizer>[^/]+)/(?P<event>[^/]+)/', include([
url(r'^$', event.index, name='event.index'),

View File

@@ -0,0 +1,118 @@
from django import forms
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.hashers import check_password
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.views.generic import UpdateView
from django.utils.translation import ugettext_lazy as _
from pytz import common_timezones
from pretix.base.models import User
class UserSettingsForm(forms.ModelForm):
error_messages = {
'duplicate_identifier': _("There already is an account associated with this e-mail address. "
"Please choose a different one."),
'pw_current': _("Please enter your current password if you want to change your e-mail "
"address or password."),
'pw_current_wrong': _("The current password you entered was not correct."),
'pw_mismatch': _("Please enter the same password twice"),
}
old_pw = forms.CharField(max_length=255,
required=False,
label=_("Your current password"),
widget=forms.PasswordInput())
new_pw = forms.CharField(max_length=255,
required=False,
label=_("New password"),
widget=forms.PasswordInput())
new_pw_repeat = forms.CharField(max_length=255,
required=False,
label=_("Repeat new password"),
widget=forms.PasswordInput())
timezone = forms.ChoiceField(
choices=((a, a) for a in common_timezones),
label=_("Default timezone"),
)
class Meta:
model = User
fields = [
'givenname', 'familyname', 'locale', 'timezone', 'email'
]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
def clean_old_pw(self):
old_pw = self.cleaned_data.get('old_pw')
if old_pw and not check_password(old_pw, self.user.password):
raise forms.ValidationError(
self.error_messages['pw_current_wrong'],
code='pw_current_wrong',
)
return old_pw
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(
Q(identifier=email)
& ~Q(pk=self.instance.pk)
).exists():
raise forms.ValidationError(
self.error_messages['duplicate_identifier'],
code='duplicate_identifier',
)
return email
def clean(self):
password1 = self.cleaned_data.get('new_pw')
password2 = self.cleaned_data.get('new_pw_repeat')
old_pw = self.cleaned_data.get('old_pw')
email = self.cleaned_data.get('email')
if (password1 or email != self.user.email) and not old_pw:
raise forms.ValidationError(
self.error_messages['pw_current'],
code='pw_current',
)
if password1 and password1 != password2:
raise forms.ValidationError(
self.error_messages['pw_mismatch'],
code='pw_mismatch',
)
if password1:
self.instance.set_password(password1)
self.instance.identifier = self.instance.email
return self.cleaned_data
class UserSettings(UpdateView):
model = User
form_class = UserSettingsForm
template_name = 'pretixcontrol/user/settings.html'
def get_object(self, queryset=None):
return self.request.user
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
messages.success(self.request, _('Your changes have been saved.'))
sup = super().form_valid(form)
update_session_auth_hash(self.request, self.request.user)
return sup
def get_success_url(self):
return reverse('control:user.settings')