Skip to content

Commit

Permalink
Merge pull request #70 from druids/ImproveDomain
Browse files Browse the repository at this point in the history
Improved domain class
  • Loading branch information
matllubos committed Jan 25, 2019
2 parents be5fbc4 + b16e3e4 commit c22c094
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
30 changes: 21 additions & 9 deletions chamber/multidomains/domain.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
from urllib.parse import urlparse

from django.core.exceptions import ImproperlyConfigured


class Domain:

def __init__(self, name, protocol, hostname, urlconf, user_model, port=None):
def __init__(self, name, urlconf=None, user_model=None, url=None, protocol=None, hostname=None, port=None):
self.name = name
self.protocol = protocol
self.hostname = hostname
self.urlconf = urlconf
if port is None and protocol == 'http':
self.port = 80
elif port is None and protocol == 'https':
self.port = 443
elif port is None:
raise ImproperlyConfigured('Port must be set')
else:
self.port = port
self.port = port

if url:
parsed_url = urlparse(url)
self.protocol, self.hostname, self.port = parsed_url.scheme, parsed_url.hostname, parsed_url.port

if self.protocol is None:
raise ImproperlyConfigured('protocol must be set')
if self.hostname is None:
raise ImproperlyConfigured('hostname must be set')

if self.port is None:
if self.protocol == 'http':
self.port = 80
elif self.protocol == 'https':
self.port = 443
else:
raise ImproperlyConfigured('port must be set')
self.user_model = user_model

@property
Expand Down
2 changes: 1 addition & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cleanvar: clean cleanvirtualenv
cleanall: cleanvar

pip:
$(PYTHON_BIN)/pip install --process-dependency-links -r requirements/base.txt
$(PYTHON_BIN)/pip install -r requirements/base.txt

initvirtualenv:
virtualenv -p $(PYTHON) --no-site-packages $(VIRTUAL_ENV)
Expand Down
38 changes: 23 additions & 15 deletions example/dj/apps/test_chamber/tests/multidomains.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,29 @@ def test_get_domain_url(self):
assert_equal(get_domain(settings.FRONTEND_SITE_ID).url, 'https://localhost')

def test_new_domain_port(self):
assert_equal(Domain('testA', 'http', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser').port, 80)
assert_equal(Domain('testB', 'https', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser').port, 443)
assert_equal(Domain('testC', 'http', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser', 443).port, 443)
assert_equal(Domain('testD', 'https', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser', 80).port, 80)
assert_raises(ImproperlyConfigured, Domain, 'testF', 'hbbs', 'localhost', 'dj.backend_urls',
'test_chamber.BackendUser')

assert_equal(Domain('testA', 'http', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser').url,
'http://localhost')
assert_equal(Domain('testB', 'https', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser').url,
'https://localhost')
assert_equal(Domain('testC', 'http', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser', 443).url,
'http://localhost:443')
assert_equal(Domain('testD', 'https', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser', 80).url,
'https://localhost:80')
assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='http', hostname='localhost').port, 80)
assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='https', hostname='localhost').port, 443)
assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='http', hostname='localhost', port=443).port, 443)
assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='https', hostname='localhost', port=80).port, 80)
assert_raises(ImproperlyConfigured, Domain, 'testF', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='hbbs', hostname='localhost')
assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
url='https://localhost:80').port, 80)
assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
url='https://localhost').port, 443)

assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='http', hostname='localhost').url, 'http://localhost')
assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='https', hostname='localhost').url, 'https://localhost')
assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='http', hostname='localhost', port=443).url, 'http://localhost:443')
assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
protocol='https', hostname='localhost', port=80).url, 'https://localhost:80')

def test_reverse(self):
assert_equal(reverse('current-datetime'), '/current_time_backend/')
Expand Down
10 changes: 8 additions & 2 deletions example/dj/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@
FRONTEND_SITE_ID = 2

DOMAINS = {
BACKEND_SITE_ID: Domain('backend', 'http', 'localhost', 'dj.backend_urls', 'test_chamber.BackendUser', 8000),
FRONTEND_SITE_ID: Domain('frontend', 'https', 'localhost', 'dj.frontend_urls', 'test_chamber.FrontendUser'),
BACKEND_SITE_ID: Domain(
name='backend', protocol='http', hostname='localhost', urlconf='dj.backend_urls',
user_model='test_chamber.BackendUser', port=8000
),
FRONTEND_SITE_ID: Domain(
name='frontend', protocol='https', hostname='localhost', urlconf='dj.frontend_urls',
user_model='test_chamber.FrontendUser'
),
}

# If you set this to False, Django will make some optimizations so as not
Expand Down

0 comments on commit c22c094

Please sign in to comment.