From b4670610eec126ce8db66162e47df87a73288061 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Thu, 4 Jun 2015 14:15:17 +0000 Subject: [PATCH] Improvements on database form validation --- orchestra/contrib/accounts/admin.py | 2 ++ orchestra/contrib/databases/forms.py | 20 +++++++++++++------- orchestra/contrib/miscellaneous/admin.py | 5 +++-- orchestra/contrib/orchestration/admin.py | 15 ++------------- orchestra/contrib/webapps/types/php.py | 10 ++++------ 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/orchestra/contrib/accounts/admin.py b/orchestra/contrib/accounts/admin.py index df6ddb59..2470e25b 100644 --- a/orchestra/contrib/accounts/admin.py +++ b/orchestra/contrib/accounts/admin.py @@ -193,6 +193,8 @@ class AccountAdminMixin(object): if obj and not obj.account.is_active: help_text += "
This user's account is dissabled" field.help_text = _(help_text) + # Not available in POST + form.initial_account = self.get_changeform_initial_data(request).get('account') return form def get_fields(self, request, obj=None): diff --git a/orchestra/contrib/databases/forms.py b/orchestra/contrib/databases/forms.py index a3440e2d..78000f0b 100644 --- a/orchestra/contrib/databases/forms.py +++ b/orchestra/contrib/databases/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.auth.forms import ReadOnlyPasswordHashField +from django.core.exceptions import ValidationError from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ @@ -25,7 +26,7 @@ class DatabaseUserCreationForm(forms.ModelForm): password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: msg = _("The two password fields didn't match.") - raise forms.ValidationError(msg) + raise ValidationError(msg) return password2 @@ -45,29 +46,34 @@ class DatabaseCreationForm(DatabaseUserCreationForm): def __init__(self, *args, **kwargs): super(DatabaseCreationForm, self).__init__(*args, **kwargs) - account_id = self.initial.get('account', None) + account_id = self.initial.get('account', self.initial_account) if account_id: qs = self.fields['user'].queryset.filter(account=account_id) choices = [ (u.pk, "%s (%s)" % (u, u.get_type_display())) for u in qs ] self.fields['user'].queryset = qs self.fields['user'].choices = [(None, '--------'),] + choices + def clean_username(self): + username = self.cleaned_data.get('username') + if DatabaseUser.objects.filter(username=username).exists(): + raise ValidationError("Provided username already exists.") + def clean_password2(self): username = self.cleaned_data.get('username') password1 = self.cleaned_data.get('password1') password2 = self.cleaned_data.get('password2') if username and not (password1 and password2): - raise forms.ValidationError(_("Missing password")) + raise ValidationError(_("Missing password")) if password1 and password2 and password1 != password2: msg = _("The two password fields didn't match.") - raise forms.ValidationError(msg) + raise ValidationError(msg) return password2 def clean_user(self): user = self.cleaned_data.get('user') if user and user.type != self.cleaned_data.get('type'): msg = _("Database type and user type doesn't match") - raise forms.ValidationError(msg) + raise ValidationError(msg) return user def clean(self): @@ -75,9 +81,9 @@ class DatabaseCreationForm(DatabaseUserCreationForm): if 'user' in cleaned_data and 'username' in cleaned_data: msg = _("Use existing user or create a new one?") if cleaned_data['user'] and self.cleaned_data['username']: - raise forms.ValidationError(msg) + raise ValidationError(msg) elif not (cleaned_data['username'] or cleaned_data['user']): - raise forms.ValidationError(msg) + raise ValidationError(msg) return cleaned_data diff --git a/orchestra/contrib/miscellaneous/admin.py b/orchestra/contrib/miscellaneous/admin.py index 2d3f4d6b..75a50e10 100644 --- a/orchestra/contrib/miscellaneous/admin.py +++ b/orchestra/contrib/miscellaneous/admin.py @@ -94,8 +94,8 @@ class MiscellaneousAdmin(AccountAdminMixin, SelectPluginAdminMixin, admin.ModelA def clean_identifier(self, service=service): identifier = self.cleaned_data['identifier'] validator_path = settings.MISCELLANEOUS_IDENTIFIER_VALIDATORS.get(service.name, None) - validator = import_class(validator_path) - if validator: + if validator_path: + validator = import_class(validator_path) validator(identifier) return identifier @@ -117,5 +117,6 @@ class MiscellaneousAdmin(AccountAdminMixin, SelectPluginAdminMixin, admin.ModelA setattr(obj, self.plugin_field, plugin.model.objects.get(**kwargs)) obj.save() + admin.site.register(MiscService, MiscServiceAdmin) admin.site.register(Miscellaneous, MiscellaneousAdmin) diff --git a/orchestra/contrib/orchestration/admin.py b/orchestra/contrib/orchestration/admin.py index 9a85e91a..f1cf7608 100644 --- a/orchestra/contrib/orchestration/admin.py +++ b/orchestra/contrib/orchestration/admin.py @@ -8,6 +8,7 @@ from orchestra.admin.utils import admin_link, admin_date, admin_colored, display from . import settings, helpers from .backends import ServiceBackend +from .forms import RouteForm from .models import Server, Route, BackendLog, BackendOperation from .widgets import RouteBackendSelect @@ -24,19 +25,6 @@ STATE_COLORS = { } -from django import forms -from orchestra.forms.widgets import SpanWidget -from orchestra.forms.widgets import paddingCheckboxSelectMultiple -class RouteForm(forms.ModelForm): - def __init__(self, *args, **kwargs): - super(RouteForm, self).__init__(*args, **kwargs) - if self.instance: - self.fields['backend'].widget = SpanWidget() - self.fields['backend'].required = False - self.fields['async_actions'].widget = paddingCheckboxSelectMultiple(45) - self.fields['async_actions'].choices = ((action, action) for action in self.instance.backend_class.actions) - - class RouteAdmin(ExtendedModelAdmin): list_display = ( 'backend', 'host', 'match', 'display_model', 'display_actions', 'async', 'is_active' @@ -131,6 +119,7 @@ class BackendLogAdmin(admin.ModelAdmin): ) list_display_links = ('id', 'backend') list_filter = ('state', 'backend') + date_hierarchy = 'created_at' inlines = (BackendOperationInline,) fields = ( 'backend', 'server_link', 'state', 'mono_script', 'mono_stdout', diff --git a/orchestra/contrib/webapps/types/php.py b/orchestra/contrib/webapps/types/php.py index 444edda6..81595d07 100644 --- a/orchestra/contrib/webapps/types/php.py +++ b/orchestra/contrib/webapps/types/php.py @@ -60,9 +60,7 @@ class PHPApp(AppType): return self.instance.data.get('php_version', '') def get_php_init_vars(self, merge=settings.WEBAPPS_MERGE_PHP_WEBAPPS): - """ - process php options for inclusion on php.ini - """ + """ Prepares PHP options for inclusion on php.ini """ init_vars = OrderedDict() options = self.instance.get_options(merge=merge) php_version_number = float(self.get_php_version_number()) @@ -75,7 +73,7 @@ class PHPApp(AppType): # Filter non-deprecated PHP options if opt.group == opt.PHP and (opt.deprecated or 999) > php_version_number: init_vars[name] = value - # Enable functions + # Disable functions if self.PHP_DISABLED_FUNCTIONS: enable_functions = init_vars.pop('enable_functions', '') if enable_functions or self.is_fpm: @@ -87,7 +85,7 @@ class PHPApp(AppType): if function not in enable_functions: disable_functions.append(function) init_vars['disable_functions'] = ','.join(disable_functions) - # process timeout + # Process timeout if timeout: # Give a little slack here timeout = str(int(timeout)-2) @@ -97,7 +95,7 @@ class PHPApp(AppType): context = self.get_directive_context() error_log_path = os.path.normpath(self.PHP_ERROR_LOG_PATH % context) init_vars['error_log'] = error_log_path - # auto update max_post_size + # Auto update max_post_size if 'upload_max_filesize' in init_vars: upload_max_filesize = init_vars['upload_max_filesize'] post_max_size = init_vars.get('post_max_size', '0')