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

83 lines
3.0 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
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
from orchestra.forms.widgets import SpanWidget
2015-03-23 15:36:51 +00:00
from .. import settings
2014-11-20 15:34:59 +00:00
from .options import SoftwareService, SoftwareServiceForm
2014-09-28 12:29:18 +00:00
2014-11-20 15:34:59 +00:00
class PHPListForm(SoftwareServiceForm):
2015-03-23 15:36:51 +00:00
admin_username = forms.CharField(label=_("Admin username"), required=False,
widget=SpanWidget(display='admin'))
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")
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):
2015-03-27 19:50:54 +00:00
database = forms.CharField(label=_("Database"), required=False,
2015-03-23 15:36:51 +00:00
help_text=_("Database used for this webapp."))
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)
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'
site_base_domain = settings.SAAS_PHPLIST_BASE_DOMAIN
2015-03-23 15:36:51 +00:00
def get_db_name(self):
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):
return settings.SAAS_PHPLIST_DB_NAME
def get_account(self):
return self.instance.account.get_main()
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()
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,
})
def save(self):
db_name = self.get_db_name()
db_user = self.get_db_user()
account = self.get_account()
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