Merge pull request #333 from imnotjames/configurable-base-url

Configurable URL prefix
This commit is contained in:
Matthew Brown 2016-10-05 16:34:31 +01:00 committed by GitHub
commit a6caf63431
6 changed files with 111 additions and 9 deletions

View File

@ -0,0 +1,86 @@
import sys
from django.core.urlresolvers import reverse, clear_url_caches
from django.conf import settings
from django.test.utils import override_settings
from django.utils.importlib import import_module
from rest_framework import status, HTTP_HEADER_ENCODING
from tests_basic import LocalTestCase
class override_urlprefix(override_settings):
def clear_cache(self):
# If we don't do this, nothing gets correctly set for the URL Prefix
urlconf = settings.ROOT_URLCONF
if urlconf in sys.modules:
reload(sys.modules[urlconf])
import_module(urlconf)
# Don't forget to clear out the cache for `reverse`
clear_url_caches()
def __init__(self, urlprefix):
urlprefix = urlprefix.rstrip("/")
# Have to turn off the compressor here, can't find a way to reload
# the COMPRESS_URL into it on the fly
super(override_urlprefix, self).__init__(
URL_PREFIX = urlprefix,
MEDIA_URL = "%s/media/" % urlprefix,
STATIC_URL = "%s/static/" % urlprefix,
COMPRESS_URL = "%s/static/" % urlprefix,
COMPRESS_ENABLED = False
)
def __enter__(self):
super(override_urlprefix, self).__enter__()
self.clear_cache()
def __exit__(self, exc_type, exc_value, traceback):
super(override_urlprefix, self).__exit__(exc_type, exc_value, traceback)
self.clear_cache()
class URLPrefixTestCase(LocalTestCase):
def set_url_prefix(self, prefix):
return override_urlprefix(prefix)
def test_reverse(self):
prefix = '/test'
before = reverse('services')
with self.set_url_prefix(prefix):
self.assertNotEqual(reverse('services'), before)
self.assertTrue(reverse('services').startswith(prefix))
self.assertEqual(reverse('services')[len(prefix):], before)
def test_loginurl(self):
prefix = '/test'
with self.set_url_prefix(prefix):
loginurl = str(settings.LOGIN_URL)
response = self.client.get(reverse('services'))
self.assertTrue(loginurl.startswith(prefix))
self.assertTrue(loginurl in response.url)
def test_query(self):
prefix = '/test'
self.client.login(username=self.username, password=self.password)
before_services = self.client.get(reverse('services'))
before_systemstatus = self.client.get(reverse('system-status'))
with self.set_url_prefix(prefix):
response = self.client.get(reverse('services'))
self.assertEqual(response.status_code, before_services.status_code)
self.assertNotEqual(response.content, before_services.content)
self.assertIn(reverse('services'), response.content)
resposnse = self.client.get(reverse('system-status'))
self.assertEqual(response.status_code, before_systemstatus.status_code)

View File

@ -2,6 +2,7 @@ import os
import dj_database_url
import re
from django.conf import settings
from django.core.urlresolvers import reverse_lazy
from cabot.celeryconfig import *
from cabot.cabot_config import *
@ -24,6 +25,10 @@ DATABASES = {'default': dj_database_url.parse(os.environ["DATABASE_URL"])}
if not DEBUG:
DATABASES['default']['OPTIONS'] = {'autocommit': True}
URL_PREFIX = os.environ.get('URL_PREFIX', '/').rstrip("/")
LOGIN_URL = reverse_lazy('login')
USE_TZ = True
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',')
@ -58,7 +63,7 @@ MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'
MEDIA_URL = '%s/media/' % URL_PREFIX
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
@ -70,7 +75,7 @@ COMPRESS_ROOT = STATIC_ROOT
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
STATIC_URL = '%s/static/' % URL_PREFIX
# Additional locations of static files
STATICFILES_DIRS = [os.path.join(PROJECT_ROOT, 'static')]
@ -152,7 +157,7 @@ EMAIL_USE_TLS = os.environ.get('SES_USE_TLS', 0)
COMPRESS_OFFLINE = not DEBUG
COMPRESS_URL = '/static/'
COMPRESS_URL = '%s/static/' % URL_PREFIX
RECOVERY_SNIPPETS_WHITELIST = (
r'https?://[^.]+\.hackpad\.com/[^./]+\.js',

View File

@ -90,15 +90,15 @@
<a class="dropdown-toggle" data-toggle="dropdown" href="#" id="download">{{ user.username }} <span class="caret"></span></a>
<ul class="dropdown-menu" aria-labelledby="admin">
<li>
<a href="/admin/auth/user/add/"><i class="glyphicon glyphicon-user"></i> Add user</a>
<a href="{% url "admin:auth_user_add" %}"><i class="glyphicon glyphicon-user"></i> Add user</a>
</li>
<li class="divider"></li>
<li>
<a href="/admin/"><i class="glyphicon glyphicon-lock"></i> Django admin</a>
<a href="{% url "admin:index" %}"><i class="glyphicon glyphicon-lock"></i> Django admin</a>
</li>
<li class="divider"></li>
<li>
<a href="/accounts/logout/"><i class="glyphicon glyphicon-log-out"></i> Log out</a>
<a href="{% url "logout" %}"><i class="glyphicon glyphicon-log-out"></i> Log out</a>
</li>
</ul>
</li>

View File

@ -30,6 +30,9 @@ import logging
logger = logging.getLogger(__name__)
urlpatterns = patterns('',
# for the password reset views
url('^', include('django.contrib.auth.urls')),
url(r'^$', view=RedirectView.as_view(url='services/', permanent=False),
name='dashboard'),
url(r'^subscriptions/', view=subscriptions,
@ -140,9 +143,6 @@ urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
# for the password reset views
url('^', include('django.contrib.auth.urls')),
# Comment below line to disable browsable rest api
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
@ -165,3 +165,8 @@ def append_plugin_urls():
)
append_plugin_urls()
if settings.URL_PREFIX.strip('/'):
urlpatterns = patterns('',
('^%s/' % settings.URL_PREFIX.strip('/'), include(urlpatterns))
)

View File

@ -10,6 +10,9 @@ PORT=5001
# You shouldn't need to change anything above this line
# Base path to include before generated URLs. If not defined, uses `/`
# URL_PREFIX=/
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
TIME_ZONE=Etc/UTC

View File

@ -10,6 +10,9 @@ VENV=/home/ubuntu/venv
# You shouldn't need to change anything above this line
# Base path to include before generated URLs. If not defined, uses `/`
# URL_PREFIX=/
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
TIME_ZONE=Etc/UTC