2014-09-26 21:24:23 +00:00
|
|
|
from django import forms
|
2014-10-06 14:57:02 +00:00
|
|
|
from django.contrib.auth import forms as auth_forms
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-10-06 14:57:02 +00:00
|
|
|
|
2015-03-25 15:45:04 +00:00
|
|
|
from orchestra.utils.python import random_ascii
|
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
from ..core.validators import validate_password
|
2014-09-26 21:24:23 +00:00
|
|
|
|
2015-04-26 13:53:00 +00:00
|
|
|
from .fields import SpanField
|
|
|
|
from .widgets import SpanWidget
|
|
|
|
|
2014-09-26 21:24:23 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form that creates a user, with no privileges, from the given username and
|
|
|
|
password.
|
|
|
|
"""
|
|
|
|
error_messages = {
|
|
|
|
'password_mismatch': _("The two password fields didn't match."),
|
2014-10-20 15:51:24 +00:00
|
|
|
'duplicate_username': _("A user with that username already exists."),
|
2014-10-06 14:57:02 +00:00
|
|
|
}
|
|
|
|
password1 = forms.CharField(label=_("Password"),
|
2015-09-30 13:22:17 +00:00
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
|
|
|
|
validators=[validate_password])
|
2014-10-06 14:57:02 +00:00
|
|
|
password2 = forms.CharField(label=_("Password confirmation"),
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
help_text=_("Enter the same password as above, for verification."))
|
|
|
|
|
2015-03-25 15:45:04 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(UserCreationForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['password1'].help_text = _("Suggestion: %s") % random_ascii(10)
|
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def clean_password2(self):
|
2014-10-24 10:16:46 +00:00
|
|
|
password1 = self.cleaned_data.get('password1')
|
|
|
|
password2 = self.cleaned_data.get('password2')
|
2014-10-06 14:57:02 +00:00
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
|
|
|
return password2
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
# Since model.clean() will check this, this is redundant,
|
|
|
|
# but it sets a nicer error message than the ORM and avoids conflicts with contrib.auth
|
|
|
|
username = self.cleaned_data["username"]
|
|
|
|
try:
|
|
|
|
self._meta.model._default_manager.get(username=username)
|
|
|
|
except self._meta.model.DoesNotExist:
|
|
|
|
return username
|
|
|
|
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
user = super(UserCreationForm, self).save(commit=False)
|
2015-02-25 13:24:11 +00:00
|
|
|
user.set_password(self.cleaned_data['password1'])
|
2014-10-06 14:57:02 +00:00
|
|
|
if commit:
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class UserChangeForm(forms.ModelForm):
|
|
|
|
password = auth_forms.ReadOnlyPasswordHashField(label=_("Password"),
|
|
|
|
help_text=_("Raw passwords are not stored, so there is no way to see "
|
2015-08-05 22:58:35 +00:00
|
|
|
"this user's password, but you can change it by "
|
2016-05-11 12:56:10 +00:00
|
|
|
"using <a href='../password/'>this form</a>. "
|
|
|
|
"<a onclick='return showAddAnotherPopup(this);' href='../hash/'>Show hash</a>."))
|
2014-10-06 14:57:02 +00:00
|
|
|
|
|
|
|
def clean_password(self):
|
|
|
|
# Regardless of what the user provides, return the initial value.
|
|
|
|
# This is done here, rather than on the field, because the
|
|
|
|
# field does not have access to the initial value
|
|
|
|
return self.initial["password"]
|
2015-04-26 13:53:00 +00:00
|
|
|
|
|
|
|
|
2015-08-05 22:58:35 +00:00
|
|
|
class NonStoredUserChangeForm(forms.ModelForm):
|
|
|
|
password = forms.CharField(label=_("Password"), required=False,
|
|
|
|
widget=SpanWidget(display='<strong>Unknown password</strong>'),
|
|
|
|
help_text=_("This service's password is not stored, so there is no way to see it, "
|
2016-04-30 15:10:39 +00:00
|
|
|
"but you can change it using <a href=\"../password/\">this form</a>."))
|
2015-08-05 22:58:35 +00:00
|
|
|
|
|
|
|
|
2015-04-26 13:53:00 +00:00
|
|
|
class ReadOnlyFormMixin(object):
|
|
|
|
"""
|
|
|
|
Mixin class for ModelForm or Form that provides support for SpanField on readonly fields
|
|
|
|
Meta:
|
2016-02-11 14:24:09 +00:00
|
|
|
readonly_fields = (ro_field1, ro_field2)
|
2015-04-26 13:53:00 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ReadOnlyFormMixin, self).__init__(*args, **kwargs)
|
|
|
|
for name in self.Meta.readonly_fields:
|
|
|
|
field = self.fields[name]
|
|
|
|
if not isinstance(field, SpanField):
|
2015-04-27 12:24:17 +00:00
|
|
|
if not isinstance(field.widget, SpanWidget):
|
|
|
|
field.widget = SpanWidget()
|
|
|
|
original = self.initial.get(name)
|
2015-04-26 13:53:00 +00:00
|
|
|
if hasattr(self, 'instance'):
|
2015-04-27 12:24:17 +00:00
|
|
|
original = getattr(self.instance, name, original)
|
|
|
|
field.widget.original = original
|