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

91 lines
3.4 KiB
Python
Raw Normal View History

2016-03-04 10:04:29 +00:00
import logging
2014-10-24 10:16:46 +00:00
from collections import OrderedDict
2014-05-08 16:59:35 +00:00
from django import forms
2015-04-04 17:44:07 +00:00
from django.core.exceptions import ValidationError
2015-05-01 17:23:22 +00:00
from django.apps import apps
2014-05-08 16:59:35 +00:00
from django.utils.translation import ugettext_lazy as _
2014-10-06 14:57:02 +00:00
from orchestra.forms import UserCreationForm
2014-05-08 16:59:35 +00:00
2014-10-20 15:51:24 +00:00
from . import settings
from .models import Account
2014-05-08 16:59:35 +00:00
2014-10-06 14:57:02 +00:00
2016-03-04 10:04:29 +00:00
logger = logging.getLogger(__name__)
2014-10-20 15:51:24 +00:00
def create_account_creation_form():
2014-10-24 10:16:46 +00:00
fields = OrderedDict(**{
'enable_systemuser': forms.BooleanField(initial=True, required=False,
label=_("Enable systemuser"),
help_text=_("Designates whether to creates an enabled or disabled related system user. "
"Notice that a related system user will be always created."))
})
2016-03-04 10:04:29 +00:00
create_related = []
for model, key, kwargs, help_text in settings.ACCOUNTS_CREATE_RELATED:
try:
model = apps.get_model(model)
except LookupError:
logger.error("%s not installed." % model)
else:
field_name = 'create_%s' % model._meta.model_name
label = _("Create %s") % model._meta.verbose_name
fields[field_name] = forms.BooleanField(
initial=True, required=False, label=label, help_text=help_text)
create_related.append((model, key, kwargs, help_text))
2016-04-27 08:35:13 +00:00
def clean(self, create_related=create_related):
2014-10-20 15:51:24 +00:00
""" unique usernames between accounts and system users """
cleaned_data = UserCreationForm.clean(self)
try:
account = Account(
username=cleaned_data['username'],
password=cleaned_data['password1']
)
except KeyError:
# Previous validation error
return
2014-11-05 20:22:01 +00:00
errors = {}
systemuser_model = Account.main_systemuser.field.related_model
2014-10-23 15:38:46 +00:00
if systemuser_model.objects.filter(username=account.username).exists():
2014-11-05 20:22:01 +00:00
errors['username'] = _("A system user with this name already exists.")
2016-03-04 10:04:29 +00:00
for model, key, related_kwargs, __ in create_related:
2014-10-20 15:51:24 +00:00
kwargs = {
key: eval(related_kwargs[key], {'account': account})
}
if model.objects.filter(**kwargs).exists():
verbose_name = model._meta.verbose_name
2014-11-05 20:22:01 +00:00
field_name = 'create_%s' % model._meta.model_name
2015-04-04 17:44:07 +00:00
errors[field_name] = ValidationError(
2014-11-05 20:22:01 +00:00
_("A %(type)s with this name already exists."),
params={'type': verbose_name})
if errors:
raise ValidationError(errors)
2014-10-24 10:16:46 +00:00
def save_model(self, account):
2015-07-15 10:35:21 +00:00
enable_systemuser=self.cleaned_data['enable_systemuser']
account.save(active_systemuser=enable_systemuser)
2014-10-20 15:51:24 +00:00
def save_related(self, account):
for model, key, related_kwargs, __ in settings.ACCOUNTS_CREATE_RELATED:
2015-05-01 17:23:22 +00:00
model = apps.get_model(model)
2014-10-20 15:51:24 +00:00
field_name = 'create_%s' % model._meta.model_name
if self.cleaned_data[field_name]:
2014-10-23 15:38:46 +00:00
kwargs = {
2015-04-02 16:14:55 +00:00
key: eval(value, {'account': account}) for key, value in related_kwargs.items()
2014-10-23 15:38:46 +00:00
}
model.objects.create(account=account, **kwargs)
2014-10-20 15:51:24 +00:00
fields.update({
2015-04-02 16:14:55 +00:00
'create_related_fields': list(fields.keys()),
2014-10-20 15:51:24 +00:00
'clean': clean,
2014-10-24 10:16:46 +00:00
'save_model': save_model,
2014-10-20 15:51:24 +00:00
'save_related': save_related,
})
2014-10-20 15:51:24 +00:00
return type('AccountCreationForm', (UserCreationForm,), fields)
AccountCreationForm = create_account_creation_form()