76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
from six.moves.urllib.parse import urlparse
|
|
|
|
from django.conf import settings
|
|
from django.core.urlresolvers import reverse
|
|
from django.db import connection
|
|
|
|
from hobo.middleware.utils import StoreRequestMiddleware
|
|
|
|
|
|
def get_installed_services():
|
|
from .models import AVAILABLE_SERVICES
|
|
installed_services = []
|
|
for available_service in AVAILABLE_SERVICES:
|
|
installed_services.extend(available_service.objects.all())
|
|
return installed_services
|
|
|
|
def get_operational_services():
|
|
return [x for x in get_installed_services() if x.is_operational()]
|
|
|
|
def get_installed_services_dict():
|
|
from .models import AVAILABLE_SERVICES, Variable
|
|
hobo_service = []
|
|
build_absolute_uri = None
|
|
if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
|
|
build_absolute_uri = connection.get_tenant().build_absolute_uri
|
|
else:
|
|
request = StoreRequestMiddleware.get_request()
|
|
if request:
|
|
build_absolute_uri = request.build_absolute_uri
|
|
if build_absolute_uri:
|
|
# if there's a known base url hobo can advertise itself.
|
|
hobo_service = [{
|
|
'service-id': 'hobo',
|
|
'title': 'Hobo',
|
|
'slug': 'hobo',
|
|
'base_url': build_absolute_uri(reverse('home')),
|
|
'saml-sp-metadata-url': build_absolute_uri(reverse('mellon_metadata')),
|
|
'backoffice-menu-url': build_absolute_uri(reverse('menu_json')),
|
|
}]
|
|
return {
|
|
'services': hobo_service + [x.as_dict() for x in get_installed_services()],
|
|
'variables': dict(((v.name, v.json)
|
|
for v in Variable.objects.filter(service_pk__isnull=True))),
|
|
}
|
|
|
|
|
|
class Zone:
|
|
title = None
|
|
zone_icon_id = None
|
|
href = None
|
|
|
|
def __init__(self, title, zone_icon_id, href):
|
|
self.title = title
|
|
self.zone_icon_id = zone_icon_id
|
|
self.href = href
|
|
|
|
|
|
def get_setting_variable(name):
|
|
from .models import Variable
|
|
variable, created = Variable.objects.get_or_create(name=name,
|
|
defaults={'auto': True, 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''})
|
|
return variable
|
|
|
|
def create_base_url(hostname, service):
|
|
"""
|
|
Distinguish mutualised domains (matching a "-" in the first part of the netloc)
|
|
from the normal scenario with a dedicated parent domain.
|
|
"""
|
|
ph = urlparse(hostname)
|
|
parts = ph.netloc.split('.')
|
|
if '-' in parts[0]:
|
|
netloc = '%s-%s.%s' % (service, parts[0].split('-')[1], '.'.join(parts[1:]))
|
|
else:
|
|
netloc = '%s.%s' % (service, '.'.join(parts[1:]))
|
|
return '%s://%s' % (ph.scheme, netloc)
|