django-orchestra/orchestra/contrib/saas/services/phplist.py

120 lines
4.8 KiB
Python
Raw Normal View History

2014-09-28 12:29:18 +00:00
from django import forms
2015-04-04 17:44:07 +00:00
from django.core.exceptions import ValidationError
2015-03-27 19:50:54 +00:00
from django.core.urlresolvers import reverse
from django.db.models import Q
2015-03-23 15:36:51 +00:00
from django.utils.safestring import mark_safe
2014-09-28 12:29:18 +00:00
from django.utils.translation import ugettext_lazy as _
from orchestra.contrib.mailboxes.models import Mailbox
from orchestra.forms.widgets import SpanWidget
2015-03-23 15:36:51 +00:00
from .. import settings
2015-09-21 10:28:49 +00:00
from ..forms import SaaSPasswordForm
from .options import DBSoftwareService
2014-09-28 12:29:18 +00:00
2015-09-21 10:28:49 +00:00
class PHPListForm(SaaSPasswordForm):
2015-03-23 15:36:51 +00:00
admin_username = forms.CharField(label=_("Admin username"), required=False,
widget=SpanWidget(display='admin'))
database = forms.CharField(label=_("Database"), required=False,
2015-09-23 12:22:32 +00:00
help_text=_("Database dedicated to this phpList instance."),
widget=SpanWidget(display=settings.SAAS_PHPLIST_DB_NAME.replace(
'%(', '<').replace(')s', '>')))
mailbox = forms.CharField(label=_("Bounces mailbox"), required=False,
2015-09-23 12:22:32 +00:00
help_text=_("Dedicated mailbox used for reciving bounces."),
widget=SpanWidget(display=settings.SAAS_PHPLIST_BOUNCES_MAILBOX_NAME.replace(
'%(', '<').replace(')s', '>')))
2015-03-23 15:36:51 +00:00
def __init__(self, *args, **kwargs):
super(PHPListForm, self).__init__(*args, **kwargs)
self.fields['name'].label = _("Site name")
2015-09-29 12:35:22 +00:00
context = {
'site_name': '<site_name>',
'name': '<site_name>',
}
domain = self.plugin.site_domain % context
help_text = _("Admin URL http://{}/admin/").format(domain)
self.fields['site_url'].help_text = help_text
2015-03-23 15:36:51 +00:00
class PHPListChangeForm(PHPListForm):
def __init__(self, *args, **kwargs):
super(PHPListChangeForm, self).__init__(*args, **kwargs)
site_domain = self.instance.get_site_domain()
admin_url = "http://%s/admin/" % site_domain
2015-03-23 15:36:51 +00:00
help_text = _("Admin URL <a href={0}>{0}</a>").format(admin_url)
self.fields['site_url'].help_text = help_text
2015-03-27 19:50:54 +00:00
# DB link
db = self.instance.database
db_url = reverse('admin:databases_database_change', args=(db.pk,))
db_link = mark_safe('<a href="%s">%s</a>' % (db_url, db.name))
self.fields['database'].widget = SpanWidget(original=db.name, display=db_link)
# Mailbox link
mailbox_id = self.instance.data.get('mailbox_id')
if mailbox_id:
try:
mailbox = Mailbox.objects.get(id=mailbox_id)
except Mailbox.DoesNotExist:
pass
else:
mailbox_url = reverse('admin:mailboxes_mailbox_change', args=(mailbox.pk,))
mailbox_link = mark_safe('<a href="%s">%s</a>' % (mailbox_url, mailbox.name))
self.fields['mailbox'].widget = SpanWidget(
original=mailbox.name, display=mailbox_link)
2014-09-28 12:29:18 +00:00
class PHPListService(DBSoftwareService):
2015-03-23 15:36:51 +00:00
name = 'phplist'
2014-09-28 12:29:18 +00:00
verbose_name = "phpList"
form = PHPListForm
2015-03-23 15:36:51 +00:00
change_form = PHPListChangeForm
2015-03-04 21:06:16 +00:00
icon = 'orchestra/icons/apps/Phplist.png'
2015-09-29 12:35:22 +00:00
site_domain = settings.SAAS_PHPLIST_DOMAIN
allow_custom_url = settings.SAAS_PHPLIST_ALLOW_CUSTOM_URL
db_name = settings.SAAS_PHPLIST_DB_NAME
db_user = settings.SAAS_PHPLIST_DB_USER
2015-03-23 15:36:51 +00:00
def get_mailbox_name(self):
context = {
'name': self.instance.name,
'site_name': self.instance.name,
}
return settings.SAAS_PHPLIST_BOUNCES_MAILBOX_NAME % context
2015-03-23 15:36:51 +00:00
def validate(self):
super(PHPListService, self).validate()
create = not self.instance.pk
if create:
account = self.get_account()
# Validate mailbox
mailbox = Mailbox(name=self.get_mailbox_name(), account=account)
try:
mailbox.full_clean()
except ValidationError as e:
raise ValidationError({
'name': e.messages,
})
2015-03-23 15:36:51 +00:00
def save(self):
super(PHPListService, self).save()
account = self.get_account()
# Mailbox
mailbox_name = self.get_mailbox_name()
mailbox, mb_created = account.mailboxes.get_or_create(name=mailbox_name)
if mb_created:
mailbox.set_password(settings.SAAS_PHPLIST_BOUNCES_MAILBOX_PASSWORD)
mailbox.save(update_fields=('password',))
self.instance.data.update({
'mailbox_id': mailbox.pk,
'mailbox_name': mailbox_name,
})
def delete(self):
super(PHPListService, self).save()
account = self.get_account()
# delete Mailbox (database will be deleted by ORM's cascade behaviour
mailbox_name = self.instance.data.get('mailbox_name') or self.get_mailbox_name()
mailbox_id = self.instance.data.get('mailbox_id')
qs = Q(Q(name=mailbox_name) | Q(id=mailbox_id))
2015-10-02 09:31:32 +00:00
account.mailboxes.filter(qs).delete()