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
|
2015-09-22 10:24:04 +00:00
|
|
|
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 _
|
|
|
|
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.databases.models import Database, DatabaseUser
|
2015-09-22 10:24:04 +00:00
|
|
|
from orchestra.contrib.mailboxes.models import Mailbox
|
2015-04-27 12:24:17 +00:00
|
|
|
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 SoftwareService
|
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,
|
2015-09-22 10:24:04 +00:00
|
|
|
widget=SpanWidget(display='admin'))
|
2015-09-22 10:47:23 +00:00
|
|
|
database = forms.CharField(label=_("Database"), required=False,
|
|
|
|
help_text=_("Database used for this instance."),
|
|
|
|
widget=SpanWidget(display=settings.SAAS_PHPLIST_DB_NAME.replace(
|
|
|
|
'%(', '<').replace(')s', '>')))
|
|
|
|
mailbox = forms.CharField(label=_("Bounces mailbox"), required=False,
|
|
|
|
help_text=_("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)
|
2015-03-25 15:45:04 +00:00
|
|
|
self.fields['name'].label = _("Site name")
|
|
|
|
base_domain = self.plugin.site_base_domain
|
|
|
|
help_text = _("Admin URL http://<site_name>.{}/admin/").format(base_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)
|
2015-03-25 15:45:04 +00:00
|
|
|
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)
|
2015-03-25 15:45:04 +00:00
|
|
|
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))
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields['database'].widget = SpanWidget(original=db.name, display=db_link)
|
2015-09-22 10:24:04 +00:00
|
|
|
# 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(SoftwareService):
|
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-03-25 15:45:04 +00:00
|
|
|
site_base_domain = settings.SAAS_PHPLIST_BASE_DOMAIN
|
2015-03-23 15:36:51 +00:00
|
|
|
|
|
|
|
def get_db_name(self):
|
2015-09-22 10:24:04 +00:00
|
|
|
context = {
|
|
|
|
'name': self.instance.name,
|
|
|
|
'site_name': self.instance.name,
|
|
|
|
}
|
|
|
|
return settings.SAAS_PHPLIST_DB_NAME % context
|
2015-03-25 15:45:04 +00:00
|
|
|
db_name = 'phplist_mu_%s' % self.instance.name
|
2015-03-23 15:36:51 +00:00
|
|
|
# Limit for mysql database names
|
|
|
|
return db_name[:65]
|
|
|
|
|
|
|
|
def get_db_user(self):
|
2015-07-20 12:51:30 +00:00
|
|
|
return settings.SAAS_PHPLIST_DB_USER
|
2015-03-23 15:36:51 +00:00
|
|
|
|
2015-09-22 10:24:04 +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-25 15:45:04 +00:00
|
|
|
def get_account(self):
|
2015-09-04 10:22:14 +00:00
|
|
|
account_model = self.instance._meta.get_field_by_name('account')[0]
|
2015-09-16 12:15:05 +00:00
|
|
|
return account_model.rel.to.objects.get_main()
|
2015-03-25 15:45:04 +00:00
|
|
|
|
2015-03-23 15:36:51 +00:00
|
|
|
def validate(self):
|
|
|
|
super(PHPListService, self).validate()
|
|
|
|
create = not self.instance.pk
|
|
|
|
if create:
|
2015-09-22 10:24:04 +00:00
|
|
|
account = self.get_account()
|
|
|
|
# Validated Database
|
2015-07-09 13:04:26 +00:00
|
|
|
db_user = self.get_db_user()
|
|
|
|
try:
|
|
|
|
DatabaseUser.objects.get(username=db_user)
|
|
|
|
except DatabaseUser.DoesNotExist:
|
2015-09-22 10:24:04 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_("Global database user for PHPList '%(db_user)s' does not exists.") % {
|
|
|
|
'db_user': db_user
|
|
|
|
}
|
|
|
|
)
|
2015-03-25 15:45:04 +00:00
|
|
|
db = Database(name=self.get_db_name(), account=account)
|
2015-03-23 15:36:51 +00:00
|
|
|
try:
|
|
|
|
db.full_clean()
|
|
|
|
except ValidationError as e:
|
|
|
|
raise ValidationError({
|
|
|
|
'name': e.messages,
|
|
|
|
})
|
2015-09-22 10:24:04 +00:00
|
|
|
# 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):
|
2015-09-22 10:24:04 +00:00
|
|
|
account = self.get_account()
|
|
|
|
# Database
|
2015-03-23 15:36:51 +00:00
|
|
|
db_name = self.get_db_name()
|
|
|
|
db_user = self.get_db_user()
|
2015-03-27 19:50:54 +00:00
|
|
|
db, db_created = account.databases.get_or_create(name=db_name, type=Database.MYSQL)
|
2015-03-23 15:36:51 +00:00
|
|
|
user = DatabaseUser.objects.get(username=db_user)
|
|
|
|
db.users.add(user)
|
2015-03-27 19:50:54 +00:00
|
|
|
self.instance.database_id = db.pk
|
2015-09-22 10:24:04 +00:00
|
|
|
# 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):
|
|
|
|
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))
|
|
|
|
for mailbox in account.mailboxes.filter(qs):
|
|
|
|
mailbox.delete()
|