mirror of
https://github.com/pretix/pretix.git
synced 2026-09-15 16:34:42 +00:00
UserSettingsform: Refactor validation
This commit is contained in:
@@ -51,6 +51,7 @@ class UserSettingsForm(forms.ModelForm):
|
|||||||
self.error_messages['pw_current_wrong'],
|
self.error_messages['pw_current_wrong'],
|
||||||
code='pw_current_wrong',
|
code='pw_current_wrong',
|
||||||
)
|
)
|
||||||
|
|
||||||
return old_pw
|
return old_pw
|
||||||
|
|
||||||
def clean_email(self):
|
def clean_email(self):
|
||||||
@@ -62,27 +63,28 @@ class UserSettingsForm(forms.ModelForm):
|
|||||||
)
|
)
|
||||||
return email
|
return email
|
||||||
|
|
||||||
def clean(self):
|
def clean_new_pw_repeat(self):
|
||||||
password1 = self.cleaned_data.get('new_pw')
|
password1 = self.cleaned_data.get('new_pw')
|
||||||
password2 = self.cleaned_data.get('new_pw_repeat')
|
password2 = self.cleaned_data.get('new_pw_repeat')
|
||||||
old_pw = self.cleaned_data.get('old_pw')
|
if password1 and password1 != password2:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
self.error_messages['pw_mismatch'],
|
||||||
|
code='pw_mismatch'
|
||||||
|
)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
password1 = self.cleaned_data.get('new_pw')
|
||||||
email = self.cleaned_data.get('email')
|
email = self.cleaned_data.get('email')
|
||||||
|
old_pw = self.cleaned_data.get('old_pw')
|
||||||
|
|
||||||
if (password1 or email != self.user.email) and not old_pw:
|
if (password1 or email != self.user.email) and not old_pw:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
self.error_messages['pw_current'],
|
self.error_messages['pw_current'],
|
||||||
code='pw_current',
|
code='pw_current'
|
||||||
)
|
|
||||||
|
|
||||||
if password1 and password1 != password2:
|
|
||||||
raise forms.ValidationError(
|
|
||||||
self.error_messages['pw_mismatch'],
|
|
||||||
code='pw_mismatch',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if password1:
|
if password1:
|
||||||
self.instance.set_password(password1)
|
self.instance.set_password(password1)
|
||||||
|
|
||||||
self.instance.identifier = email
|
self.instance.identifier = email
|
||||||
|
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ class UserSettings(UpdateView):
|
|||||||
kwargs['user'] = self.request.user
|
kwargs['user'] = self.request.user
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
def form_invalid(self, form):
|
||||||
|
messages.error(self.request, _('Your changes could not be saved. See below for details.'))
|
||||||
|
return super().form_invalid(form)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
messages.success(self.request, _('Your changes have been saved.'))
|
messages.success(self.request, _('Your changes have been saved.'))
|
||||||
sup = super().form_valid(form)
|
sup = super().form_valid(form)
|
||||||
|
|||||||
Reference in New Issue
Block a user