add inline editing for mail address targets

- move active flag into managemails.models.ActivateAbleMixin
- refactor Mailbox and Mailaddress to use ActivateAbleMixin
- implement managemails.models.Mailbox.__str__
- add managemails.admin.ActivationChangeMixin to provide activate and
  deactivate actions for model admin classes
- add activate and deactivate actions for managemails.admin.MailboxAdmin
- add managemails.admin.MailAddressAdmin and inlines for MailAddressMailbox and
  MailAddressForward management on the MailAddress management admin page
This commit is contained in:
Jan Dittberner 2014-05-24 14:58:54 +02:00
parent b044fe2dd9
commit ee46a61e6d
2 changed files with 52 additions and 7 deletions

View file

@ -5,9 +5,11 @@ from django.forms.util import flatatt
from django.utils.translation import ugettext as _
from .models import (
MailAddress,
MailAddressForward,
MailAddressMailbox,
MailDomain,
Mailbox,
MailAddress
)
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
@ -89,7 +91,18 @@ class MailboxChangeForm(forms.ModelForm):
return self.initial['password']
class MailboxAdmin(admin.ModelAdmin):
class ActivationChangeMixin(object):
def activate(self, request, queryset):
queryset.update(active=True)
def deactivate(self, request, queryset):
queryset.update(active=False)
activate.short_description = _('Activate')
deactivate.short_description = _('Deactivate')
class MailboxAdmin(ActivationChangeMixin, admin.ModelAdmin):
"""
Custom admin page for mailboxes.
@ -97,6 +110,8 @@ class MailboxAdmin(admin.ModelAdmin):
form = MailboxChangeForm
add_form = MailboxCreationForm
actions = ['activate', 'deactivate']
list_display = ('username', 'active')
list_filter = ('active',)
fieldsets = (
@ -134,6 +149,23 @@ class MailboxAdmin(admin.ModelAdmin):
return super(MailboxAdmin, self).get_form(request, obj, **defaults)
class MailAddressMailboxInline(admin.TabularInline):
model = MailAddressMailbox
class MailAddressForwardInline(admin.TabularInline):
model = MailAddressForward
class MailAddressAdmin(ActivationChangeMixin, admin.ModelAdmin):
actions = ['activate', 'deactivate']
list_display = ('__str__', 'active')
list_filter = ('active',)
inlines = [MailAddressMailboxInline, MailAddressForwardInline]
admin.site.register(MailDomain)
admin.site.register(Mailbox, MailboxAdmin)
admin.site.register(MailAddress)
admin.site.register(MailAddress, MailAddressAdmin)

View file

@ -16,13 +16,24 @@ class MailDomain(models.Model):
return self.domain
class Mailbox(models.Model):
class ActivateAbleMixin(models.Model):
"""
Mixin for model classes that can be active or inactive.
"""
active = models.BooleanField(default=True)
class Meta:
abstract = True
@python_2_unicode_compatible
class Mailbox(ActivateAbleMixin, models.Model):
username = models.CharField(max_length=128, unique=True)
password = models.CharField(max_length=255)
home = models.CharField(max_length=255)
uid = models.PositiveSmallIntegerField()
gid = models.PositiveSmallIntegerField()
active = models.BooleanField(default=True)
class Meta:
verbose_name = _('Mailbox')
@ -31,12 +42,14 @@ class Mailbox(models.Model):
def set_password(self, password):
self.password = sha512_crypt.encrypt(password)
def __str__(self):
return self.username
@python_2_unicode_compatible
class MailAddress(models.Model):
class MailAddress(ActivateAbleMixin, models.Model):
localpart = models.CharField(max_length=128)
domain = models.ForeignKey(MailDomain)
active = models.BooleanField(default=True)
class Meta:
unique_together = ('localpart', 'domain')