from django import forms from django.core.urlresolvers import reverse from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from orchestra.forms.widgets import ReadOnlyWidget from .models import Mailbox from ..forms import RoleAdminBaseForm class MailRoleAdminForm(RoleAdminBaseForm): class Meta(RoleAdminBaseForm.Meta): model = Mailbox def __init__(self, *args, **kwargs): super(MailRoleAdminForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance') if instance: widget = ReadOnlyWidget(self.addresses(instance)) self.fields['addresses'] = forms.CharField(widget=widget, label=_("Addresses")) # def addresses(self, mailbox): # account = mailbox.user.account # addresses = account.addresses.filter(destination__contains=mailbox.user.username) # add_url = reverse('admin:mail_address_add') # add_url += '?account=%d&destination=%s' % (account.pk, mailbox.user.username) # img = 'Add Another' # onclick = 'onclick="return showAddAnotherPopup(this);"' # add_link = '%s Add address' % (add_url, onclick, img) # value = '%s

' % add_link # for pk, name, domain in addresses.values_list('pk', 'name', 'domain__name'): # url = reverse('admin:mail_address_change', args=(pk,)) # name = '%s@%s' % (name, domain) # value += '
  • %s
  • ' % (url, name) # value = '' % value # return mark_safe('
    %s
    ' % value) def addresses(self, mailbox): account = mailbox.user.account add_url = reverse('admin:mail_address_add') add_url += '?account=%d&mailboxes=%s' % (account.pk, mailbox.pk) img = 'Add Another' onclick = 'onclick="return showAddAnotherPopup(this);"' add_link = '%s Add address' % (add_url, onclick, img) value = '%s

    ' % add_link for pk, name, domain in mailbox.addresses.values_list('pk', 'name', 'domain__name'): url = reverse('admin:mail_address_change', args=(pk,)) name = '%s@%s' % (name, domain) value += '
  • %s
  • ' % (url, name) value = '' % value return mark_safe('
    %s
    ' % value)