Skip to content

Commit

Permalink
Merge pull request #29 from mikebryant/wip-28
Browse files Browse the repository at this point in the history
Allow remapping the prefixes in autourlconf. Fixes #28
  • Loading branch information
mikebryant committed Apr 15, 2016
2 parents d651a70 + a3b73d2 commit e885164
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions django_autoconfig/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@

#: A view name (suitable for reverse()) that the base / will redirect to.
AUTOCONFIG_INDEX_VIEW = getattr(settings, 'AUTOCONFIG_INDEX_VIEW', None)

#: A dictionary from app name to the prefix it should be mapped to
#: The default for each app is the app name itself, with _ replaced by -
AUTOCONFIG_URL_PREFIXES = getattr(settings, 'AUTOCONFIG_URL_PREFIXES', {})
7 changes: 5 additions & 2 deletions django_autoconfig/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,12 @@ def configure_settings(settings, environment_settings=True):
iterations += 1
LOGGER.debug("Autoconfiguration took %d iterations.", iterations)

def configure_urls(apps, index_view=None):

def configure_urls(apps, index_view=None, prefixes=None):
'''
Configure urls from a list of apps.
'''
prefixes = prefixes or {}
urlpatterns = patterns('')

if index_view:
Expand All @@ -210,10 +212,11 @@ def configure_urls(apps, index_view=None):
if not hasattr(module, 'urlpatterns'):
# Resolver will break if the urls.py file is completely blank.
continue
app_prefix = prefixes.get(app_name, app_name.replace("_","-"))
urlpatterns += patterns(
'',
url(
r'^%s/' % app_name.replace("_","-"),
r'^%s/' % app_prefix if app_prefix else '',
include("%s.urls" % app_name),
),
)
Expand Down
8 changes: 6 additions & 2 deletions django_autoconfig/autourlconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

from django.conf import settings

from .app_settings import AUTOCONFIG_EXTRA_URLS, AUTOCONFIG_INDEX_VIEW
from .app_settings import AUTOCONFIG_EXTRA_URLS, AUTOCONFIG_INDEX_VIEW, AUTOCONFIG_URL_PREFIXES
from .autoconfig import configure_urls

urlpatterns = configure_urls(list(settings.INSTALLED_APPS) + list(AUTOCONFIG_EXTRA_URLS), index_view=AUTOCONFIG_INDEX_VIEW) # pylint: disable=C0103
urlpatterns = configure_urls( # pylint: disable=C0103
list(settings.INSTALLED_APPS) + list(AUTOCONFIG_EXTRA_URLS),
index_view=AUTOCONFIG_INDEX_VIEW,
prefixes=AUTOCONFIG_URL_PREFIXES,
)

if settings.DEBUG:
media_url = getattr(settings, 'MEDIA_URL', None)
Expand Down
23 changes: 23 additions & 0 deletions django_autoconfig/tests/test_autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ def test_broken_index_view(self):
response = view(test.RequestFactory().get(path='/'))
self.assertEqual(response.status_code, 410)

def test_url_prefix_blank(self):
'''Test the url prefix mapping works for blank prefixes.'''
self.create_urlconf(
['django_autoconfig.tests.app_urls'],
prefixes={
'django_autoconfig.tests.app_urls': '',
},
)
resolve('/index/', urlconf=self)
with self.assertRaises(django.core.urlresolvers.Resolver404):
resolve('/django-autoconfig.tests.app-urls/index/', urlconf=self)

def test_url_prefixes(self):
'''Test the url prefix mapping works for prefixes.'''
self.create_urlconf(
['django_autoconfig.tests.app_urls'],
prefixes={
'django_autoconfig.tests.app_urls': 'flibble',
},
)
resolve('/flibble/index/', urlconf=self)


class IndexViewTestCase(test.TestCase):
'''Test the index view.'''
urls = 'django_autoconfig.tests.index_view_urlconf'
Expand Down

0 comments on commit e885164

Please sign in to comment.