jumpserver/apps/users/forms.py

110 lines
3.5 KiB
Python
Raw Normal View History

2016-08-14 11:18:41 +00:00
# ~*~ coding: utf-8 ~*~
2016-08-16 14:13:06 +00:00
from django import forms
from django.contrib.auth.forms import AuthenticationForm
2016-09-03 16:51:36 +00:00
from django.utils.translation import gettext_lazy as _
from captcha.fields import CaptchaField
2016-08-21 14:37:55 +00:00
from .models import User, UserGroup
2016-09-16 17:04:52 +00:00
from .hands import AssetPermission
2016-08-14 11:18:41 +00:00
class UserLoginForm(AuthenticationForm):
2016-09-03 16:51:36 +00:00
username = forms.CharField(label=_('Username'), max_length=100)
password = forms.CharField(
label=_('Password'), widget=forms.PasswordInput, max_length=100,
strip=False)
2016-09-03 06:37:01 +00:00
captcha = CaptchaField()
2016-08-25 11:29:59 +00:00
2016-09-06 07:09:00 +00:00
class UserCreateForm(forms.ModelForm):
2016-09-08 13:51:44 +00:00
2016-08-14 11:18:41 +00:00
class Meta:
model = User
fields = [
2016-08-17 14:17:16 +00:00
'username', 'name', 'email', 'groups', 'wechat',
2016-08-20 17:16:30 +00:00
'phone', 'enable_otp', 'role', 'date_expired', 'comment',
2016-08-14 11:18:41 +00:00
]
2016-08-19 16:42:50 +00:00
help_texts = {
'username': '* required',
'email': '* required',
}
widgets = {
2016-09-05 12:27:44 +00:00
'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Join user groups')}),
2016-08-19 16:42:50 +00:00
}
2016-08-16 14:13:06 +00:00
class UserUpdateForm(forms.ModelForm):
2016-08-17 14:17:16 +00:00
class Meta:
model = User
fields = [
2016-08-22 16:39:07 +00:00
'name', 'email', 'groups', 'wechat',
2016-08-20 17:16:30 +00:00
'phone', 'enable_otp', 'role', 'date_expired', 'comment',
2016-08-17 14:17:16 +00:00
]
2016-08-19 16:42:50 +00:00
help_texts = {
'username': '* required',
'email': '* required',
'groups': '* required'
}
2016-08-21 14:37:55 +00:00
widgets = {
2016-09-06 07:09:00 +00:00
'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Join user groups')}),
2016-08-21 14:37:55 +00:00
}
2016-08-18 17:39:08 +00:00
class UserGroupForm(forms.ModelForm):
2016-08-18 17:39:08 +00:00
class Meta:
model = UserGroup
fields = [
'name', 'comment',
]
2016-08-19 16:42:50 +00:00
help_texts = {
'name': '* required'
}
2016-09-08 13:51:44 +00:00
class UserInfoForm(forms.Form):
2016-09-10 05:16:58 +00:00
name = forms.CharField(max_length=20, label=_('name'))
avatar = forms.ImageField(label=_('avatar'), required=False)
wechat = forms.CharField(max_length=30, label=_('wechat'), required=False)
phone = forms.CharField(max_length=20, label=_('phone'), required=False)
enable_otp = forms.BooleanField(required=False, label=_('enable otp'))
2016-09-08 13:51:44 +00:00
class UserKeyForm(forms.Form):
2016-09-10 05:16:58 +00:00
private_key = forms.CharField(max_length=5000, widget=forms.Textarea, label=_('private key'))
def clean_private_key(self):
from users.utils import validate_ssh_pk
ssh_pk = self.cleaned_data['private_key']
checked, reason = validate_ssh_pk(ssh_pk)
if not checked:
raise forms.ValidationError(_('Not a valid ssh private key.'))
return ssh_pk
2016-09-16 17:04:52 +00:00
class UserPrivateAssetPermissionForm(forms.ModelForm):
def save(self, commit=True):
self.instance = super(UserPrivateAssetPermissionForm, self).save(commit=commit)
2016-09-17 05:04:26 +00:00
self.instance.private_for = 'U'
2016-09-16 17:04:52 +00:00
self.instance.users = [self.user]
self.instance.save()
return self.instance
class Meta:
model = AssetPermission
fields = [
'assets', 'asset_groups', 'system_users', 'private_for', 'name',
]
widgets = {
'assets': forms.SelectMultiple(attrs={'class': 'select2',
'data-placeholder': _('Select assets')}),
'asset_groups': forms.SelectMultiple(attrs={'class': 'select2',
'data-placeholder': _('Select asset groups')}),
'system_users': forms.SelectMultiple(attrs={'class': 'select2',
'data-placeholder': _('Select system users')}),
}