From 948f6ae8af7f98d8a52ef8ddb2beb6ab8b903419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20J=C3=BAnior?= Date: Wed, 27 Feb 2013 14:39:16 -0300 Subject: [PATCH] Configurable get_domain added. This changes allow project's users to define ther own way to get_domain, freeing them to use django sites framework. --- subdomains/utils.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/subdomains/utils.py b/subdomains/utils.py index 75d2370..e8f9761 100644 --- a/subdomains/utils.py +++ b/subdomains/utils.py @@ -3,6 +3,8 @@ from django.conf import settings from django.contrib.sites.models import Site +from django.utils.importlib import import_module +from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse as simple_reverse @@ -16,7 +18,31 @@ def current_site_domain(): return domain -get_domain = current_site_domain + +def get_domain(): + global get_domain + + fn_path = getattr(settings, 'SUBDOMAIN_GET_DOMAIN') + + if fn_path is None: + get_domain = current_site_domain + + else: + module_name, fn_name = fn_path.rsplit('.', 1) + + try: + module = import_module(module_name) + fn = getattr(module, fn_name) + assert callable(fn) + + except (ImportError, AttributeError, AssertionError): + raise ImproperlyConfigured('SUBDOMAIN_GET_DOMAIN doesn\'t exist or' + ' isn\'t a callable.') + + else: + get_domain = fn + + return get_domain() def urljoin(domain, path=None, scheme=None):