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:
parent
8f5812ff29
commit
92c6b39e12
3 changed files with 24 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
Changelog
|
||||
=========
|
||||
|
||||
* :feature:`-` automatic creation of mailbox names from user names
|
||||
* :bug:`-` fix broken mailbox admin
|
||||
|
||||
* :release:`0.2.3 <2014-12-26>`
|
||||
|
|
|
@ -67,6 +67,8 @@ class MailboxCreationForm(forms.ModelForm):
|
|||
|
||||
"""
|
||||
mailbox = super(MailboxCreationForm, self).save(commit=False)
|
||||
mailbox.username = Mailbox.objects.get_next_mailbox_name(
|
||||
mailbox.osuser)
|
||||
mailbox.set_password(self.cleaned_data['password1'])
|
||||
if commit:
|
||||
mailbox.save()
|
||||
|
@ -118,7 +120,7 @@ class MailboxAdmin(ActivationChangeMixin, admin.ModelAdmin):
|
|||
add_fieldsets = (
|
||||
(None, {
|
||||
'classes': ('wide',),
|
||||
'fields': ('osuser', 'username', 'password1', 'password2')}),
|
||||
'fields': ('osuser', 'password1', 'password2')}),
|
||||
)
|
||||
search_fields = ('username',)
|
||||
ordering = ('username',)
|
||||
|
|
|
@ -20,12 +20,32 @@ class ActivateAbleMixin(models.Model):
|
|||
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
|
||||
class Mailbox(ActivateAbleMixin, TimeStampedModel, models.Model):
|
||||
osuser = models.ForeignKey(OsUser)
|
||||
username = models.CharField(max_length=128, unique=True)
|
||||
password = models.CharField(max_length=255)
|
||||
|
||||
objects = MailboxManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Mailbox')
|
||||
verbose_name_plural = _('Mailboxes')
|
||||
|
|
Loading…
Reference in a new issue