Prevent deleting the same domain multiple times on bulk deletes

This commit is contained in:
Marc Aymerich 2015-12-21 11:34:07 +00:00
parent 06bfa2c767
commit 3cdeb6010d
2 changed files with 19 additions and 14 deletions

View File

@ -5,7 +5,7 @@ from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from orchestra.core.validators import validate_password from orchestra.core import validators
from .models import DatabaseUser, Database from .models import DatabaseUser, Database
@ -13,7 +13,7 @@ from .models import DatabaseUser, Database
class DatabaseUserCreationForm(forms.ModelForm): class DatabaseUserCreationForm(forms.ModelForm):
password1 = forms.CharField(label=_("Password"), required=False, password1 = forms.CharField(label=_("Password"), required=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}), widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
validators=[validate_password]) validators=[validators.validate_password])
password2 = forms.CharField(label=_("Password confirmation"), required=False, password2 = forms.CharField(label=_("Password confirmation"), required=False,
widget=forms.PasswordInput, widget=forms.PasswordInput,
help_text=_("Enter the same password as above, for verification.")) help_text=_("Enter the same password as above, for verification."))
@ -32,12 +32,12 @@ class DatabaseUserCreationForm(forms.ModelForm):
class DatabaseCreationForm(DatabaseUserCreationForm): class DatabaseCreationForm(DatabaseUserCreationForm):
username = forms.RegexField(label=_("Username"), max_length=30, username = forms.CharField(label=_("Username"), max_length=16,
required=False, regex=r'^[\w.@+-]+$', required=False, validators=[validators.validate_name],
help_text=_("Required. 30 characters or fewer. Letters, digits and " help_text=_("Required. 16 characters or fewer. Letters, digits and "
"@/./+/-/_ only."), "@/./+/-/_ only."),
error_messages={ error_messages={
'invalid': _("This value may contain only letters, numbers and " 'invalid': _("This value may contain 16 characters or fewer, only letters, numbers and "
"@/./+/-/_ characters.")}) "@/./+/-/_ characters.")})
user = forms.ModelChoiceField(required=False, queryset=DatabaseUser.objects) user = forms.ModelChoiceField(required=False, queryset=DatabaseUser.objects)

View File

@ -264,13 +264,18 @@ class PostfixAddressVirtualDomainBackend(ServiceController):
def exclude_virtual_alias_domain(self, context): def exclude_virtual_alias_domain(self, context):
domain = context['domain'] domain = context['domain']
if self.is_last_domain(domain): if self.is_last_domain(domain):
self.append(textwrap.dedent(""" # Prevent deleting the same domain multiple times on bulk deletes
# Delete %(domain)s virtual domain if not hasattr(self, '_excluded_domains'):
if grep '^%(domain)s\s*$' %(virtual_alias_domains)s > /dev/null; then self._excluded_domains = set()
sed -i '/^%(domain)s\s*/d' %(virtual_alias_domains)s if domain.name not in self._excluded_domains:
UPDATED_VIRTUAL_ALIAS_DOMAINS=1 self._excluded_domains.add(domain.name)
fi""") % context self.append(textwrap.dedent("""
) # Delete %(domain)s virtual domain
if grep '^%(domain)s\s*$' %(virtual_alias_domains)s > /dev/null; then
sed -i '/^%(domain)s\s*/d' %(virtual_alias_domains)s
UPDATED_VIRTUAL_ALIAS_DOMAINS=1
fi""") % context
)
def save(self, address): def save(self, address):
context = self.get_context(address) context = self.get_context(address)
@ -378,7 +383,7 @@ class PostfixAddressBackend(PostfixAddressVirtualDomainBackend):
self.update_virtual_alias_maps(address, context) self.update_virtual_alias_maps(address, context)
def delete(self, address): def delete(self, address):
context = super().save(address) context = super().delete(address)
self.exclude_virtual_alias_maps(context) self.exclude_virtual_alias_maps(context)
def commit(self): def commit(self):