implement automatic creation of mailbox names

- create mailbox name when saving new mailboxes in admin
- add MailboxManager that implement get_next_mailbox_name as manager for
  Mailbox
This commit is contained in:
Jan Dittberner 2014-12-27 00:24:05 +01:00
parent 8f5812ff29
commit 92c6b39e12
3 changed files with 24 additions and 1 deletions

View file

@ -1,6 +1,7 @@
Changelog Changelog
========= =========
* :feature:`-` automatic creation of mailbox names from user names
* :bug:`-` fix broken mailbox admin * :bug:`-` fix broken mailbox admin
* :release:`0.2.3 <2014-12-26>` * :release:`0.2.3 <2014-12-26>`

View file

@ -67,6 +67,8 @@ class MailboxCreationForm(forms.ModelForm):
""" """
mailbox = super(MailboxCreationForm, self).save(commit=False) mailbox = super(MailboxCreationForm, self).save(commit=False)
mailbox.username = Mailbox.objects.get_next_mailbox_name(
mailbox.osuser)
mailbox.set_password(self.cleaned_data['password1']) mailbox.set_password(self.cleaned_data['password1'])
if commit: if commit:
mailbox.save() mailbox.save()
@ -118,7 +120,7 @@ class MailboxAdmin(ActivationChangeMixin, admin.ModelAdmin):
add_fieldsets = ( add_fieldsets = (
(None, { (None, {
'classes': ('wide',), 'classes': ('wide',),
'fields': ('osuser', 'username', 'password1', 'password2')}), 'fields': ('osuser', 'password1', 'password2')}),
) )
search_fields = ('username',) search_fields = ('username',)
ordering = ('username',) ordering = ('username',)

View file

@ -20,12 +20,32 @@ class ActivateAbleMixin(models.Model):
abstract = True abstract = True
class MailboxManager(models.Manager):
def get_next_mailbox_name(self, osuser):
count = 1
mailboxformat = "{0}p{1:02d}"
mailboxname = mailboxformat.format(osuser.username, count)
for box in self.values('username').filter(osuser=osuser).order_by(
'username'
):
if box['username'] == mailboxname:
count += 1
mailboxname = mailboxformat.format(osuser.username, count)
else:
break
return mailboxname
@python_2_unicode_compatible @python_2_unicode_compatible
class Mailbox(ActivateAbleMixin, TimeStampedModel, models.Model): class Mailbox(ActivateAbleMixin, TimeStampedModel, models.Model):
osuser = models.ForeignKey(OsUser) osuser = models.ForeignKey(OsUser)
username = models.CharField(max_length=128, unique=True) username = models.CharField(max_length=128, unique=True)
password = models.CharField(max_length=255) password = models.CharField(max_length=255)
objects = MailboxManager()
class Meta: class Meta:
verbose_name = _('Mailbox') verbose_name = _('Mailbox')
verbose_name_plural = _('Mailboxes') verbose_name_plural = _('Mailboxes')