django-orchestra-test/orchestra/contrib/systemusers/forms.py

123 lines
4.9 KiB
Python
Raw Normal View History

import textwrap
from django import forms
2015-05-11 14:05:39 +00:00
from django.core.exceptions import ValidationError
from django.utils.translation import ngettext, ugettext_lazy as _
from orchestra.forms import UserCreationForm, UserChangeForm
from . import settings
from .models import SystemUser
2015-05-12 12:38:40 +00:00
from .validators import validate_home, validate_path_exists
2015-03-04 21:06:16 +00:00
class SystemUserFormMixin(object):
MOCK_USERNAME = '<username>'
def __init__(self, *args, **kwargs):
super(SystemUserFormMixin, self).__init__(*args, **kwargs)
duplicate = lambda n: (n, n)
if self.instance.pk:
username = self.instance.username
choices=(
duplicate(self.account.main_systemuser.get_base_home()),
duplicate(self.instance.get_base_home()),
)
else:
username = self.MOCK_USERNAME
choices=(
duplicate(self.account.main_systemuser.get_base_home()),
duplicate(SystemUser(username=username).get_base_home()),
)
self.fields['home'].widget = forms.Select(choices=choices)
if self.instance.pk and (self.instance.is_main or self.instance.has_shell):
# hidde home option for shell users
self.fields['home'].widget = forms.HiddenInput()
self.fields['directory'].widget = forms.HiddenInput()
elif self.instance.pk and (self.instance.get_base_home() == self.instance.home):
self.fields['directory'].widget = forms.HiddenInput()
else:
self.fields['directory'].widget = forms.TextInput(attrs={'size':'70'})
if not self.instance.pk or not self.instance.is_main:
# Some javascript for hidde home/directory inputs when convinient
self.fields['shell'].widget.attrs = {
'onChange': textwrap.dedent("""\
field = $(".field-home, .field-directory");
input = $("#id_home, #id_directory");
if ($.inArray(this.value, %s) < 0) {
field.addClass("hidden");
} else {
field.removeClass("hidden");
input.removeAttr("type");
};""" % str(list(settings.SYSTEMUSERS_DISABLED_SHELLS)))
}
self.fields['home'].widget.attrs = {
'onChange': textwrap.dedent("""\
field = $(".field-box.field-directory");
input = $("#id_directory");
if (this.value.search("%s") > 0) {
field.addClass("hidden");
} else {
field.removeClass("hidden");
input.removeAttr("type");
};""" % username)
}
def clean(self):
2015-04-09 14:32:10 +00:00
super(SystemUserFormMixin, self).clean()
2015-05-12 12:38:40 +00:00
cleaned_data = self.cleaned_data
home = cleaned_data.get('home')
if home and self.MOCK_USERNAME in home:
2015-05-12 12:38:40 +00:00
username = cleaned_data.get('username', '')
cleaned_data['home'] = home.replace(self.MOCK_USERNAME, username)
validate_home(self.instance, cleaned_data, self.account)
return cleaned_data
class SystemUserCreationForm(SystemUserFormMixin, UserCreationForm):
pass
class SystemUserChangeForm(SystemUserFormMixin, UserChangeForm):
pass
2015-05-11 14:05:39 +00:00
class PermissionForm(forms.Form):
set_action = forms.ChoiceField(label=_("Action"), initial='grant',
choices=(
('grant', _("Grant")),
('revoke', _("Revoke"))
))
base_home = forms.ChoiceField(label=_("Set permissions to"), choices=(),
help_text=_("User will be granted/revoked access to this directory."))
home_extension = forms.CharField(label=_("Home extension"), required=False, initial='',
widget=forms.TextInput(attrs={'size':'70'}), help_text=_("Relative to chosen home."))
2015-05-11 14:05:39 +00:00
permissions = forms.ChoiceField(label=_("Permissions"), initial='read-write',
choices=(
('read-write', _("Read and write")),
('read-only', _("Read only")),
('write-only', _("Write only"))
))
def __init__(self, *args, **kwargs):
2015-05-11 14:05:39 +00:00
self.instance = args[0]
super_args = []
if len(args) > 1:
super_args.append(args[1])
2015-05-11 14:05:39 +00:00
super(PermissionForm, self).__init__(*super_args, **kwargs)
related_users = type(self.instance).objects.filter(account=self.instance.account_id)
self.fields['base_home'].choices = (
(user.get_base_home(), user.get_base_home()) for user in related_users
)
2015-05-11 14:05:39 +00:00
def clean(self):
cleaned_data = super(PermissionForm, self).clean()
2015-05-12 12:38:40 +00:00
path = os.path.join(cleaned_data['base_home'], cleaned_data['home_extension'])
try:
validate_path_exists(self.instance, path)
except ValidationError as err:
2015-05-11 14:05:39 +00:00
raise ValidationError({
2015-05-12 12:38:40 +00:00
'home_extension': err,
2015-05-11 14:05:39 +00:00
})
return cleaned_data