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

50 lines
1.9 KiB
Python
Raw Normal View History

2015-03-23 15:36:51 +00:00
import re
import requests
from django.utils.translation import ugettext_lazy as _
2015-04-05 10:46:24 +00:00
from orchestra.contrib.orchestration import ServiceController
2015-03-23 15:36:51 +00:00
class PhpListSaaSBackend(ServiceController):
2015-04-24 11:39:20 +00:00
"""
Creates a new phplist instance on a phpList multisite installation.
The site is created by means of creating a new database per phpList site, but all sites share the same code.
<tt>// config/config.php
$site = array_shift((explode(".",$_SERVER['HTTP_HOST'])));
$database_name = "phplist_mu_{$site}";</tt>
"""
2015-03-23 15:36:51 +00:00
verbose_name = _("phpList SaaS")
model = 'saas.SaaS'
default_route_match = "saas.service == 'phplist'"
serialize = True
2015-03-23 15:36:51 +00:00
2015-03-29 16:10:07 +00:00
def _save(self, saas, server):
admin_link = 'http://%s/admin/' % saas.get_site_domain()
2015-04-20 14:23:10 +00:00
admin_content = requests.get(admin_link).content.decode('utf8')
2015-03-23 15:36:51 +00:00
if admin_content.startswith('Cannot connect to Database'):
raise RuntimeError("Database is not yet configured")
install = re.search(r'([^"]+firstinstall[^"]+)', admin_content)
if install:
2015-03-27 19:50:54 +00:00
if not hasattr(saas, 'password'):
2015-03-23 15:36:51 +00:00
raise RuntimeError("Password is missing")
2015-03-29 16:10:07 +00:00
install_path = install.groups()[0]
install_link = admin_link + install_path[1:]
2015-03-23 15:36:51 +00:00
post = {
'adminname': saas.name,
2015-03-23 15:36:51 +00:00
'orgname': saas.account.username,
'adminemail': saas.account.username,
'adminpassword': saas.password,
}
response = requests.post(install_link, data=post)
2015-04-20 14:23:10 +00:00
print(response.content.decode('utf8'))
2015-03-23 15:36:51 +00:00
if response.status_code != 200:
raise RuntimeError("Bad status code %i" % response.status_code)
2015-03-29 16:10:07 +00:00
else:
raise NotImplementedError("Change password not implemented")
2015-03-23 15:36:51 +00:00
def save(self, saas):
2015-03-29 16:10:07 +00:00
if hasattr(saas, 'password'):
self.append(self._save, saas)