Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compatibility for all Django versions up to 2.1 #66

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
build/
dist/
pip-log.txt
.idea/
21 changes: 20 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ env:
- DJANGO='Django==1.7.*'
- DJANGO='Django==1.8.*'
- DJANGO='Django==1.9.*'
- DJANGO='Django==1.10.*'
- DJANGO='Django==1.11.*'
- DJANGO='Django==2.0.*'
- DJANGO='Django==2.1.*'
matrix:
exclude:
# Django >= 1.7 does not support Python 2.6
Expand All @@ -20,13 +24,28 @@ matrix:
env: DJANGO='Django==1.8.*'
- python: "2.6"
env: DJANGO='Django==1.9.*'
# Django < 1.7 does not support Python 3.4
- python: "2.6"
env: DJANGO='Django==1.10.*'
- python: "2.6"
env: DJANGO='Django==1.11.*'
- python: "2.6"
env: DJANGO='Django==2.0.*'
- python: "2.6"
env: DJANGO='Django==2.1.*'
# Django >= 2.0 does not support Python 2.7
- python: "2.7"
env: DJANGO='Django==2.0.*'
- python: "2.7"
env: DJANGO='Django==2.1.*'
# Django < 1.7 and >= 2.1 does not support Python 3.4
- python: "3.4"
env: DJANGO='Django==1.4.*'
- python: "3.4"
env: DJANGO='Django==1.5.*'
- python: "3.4"
env: DJANGO='Django==1.6.*'
- python: "3.4"
env: DJANGO='Django==2.1.*'
# Django < 1.8 does not support Python 3.5
- python: "3.5"
env: DJANGO='Django==1.4.*'
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Full documentation can be found here: http://django-subdomains.readthedocs.org/
Build Status
------------

.. image:: https://secure.travis-ci.org/tkaemming/django-subdomains.png?branch=master
:target: http://travis-ci.org/tkaemming/django-subdomains
.. image:: https://secure.travis-ci.org/contraslash/django-subdomains.png?branch=master
:target: http://travis-ci.org/contraslash/django-subdomains

Tested on Python 2.6, 2.7, 3.4 and 3.5 on their supported Django versions from
1.4 through 1.9.
1.4 through 1.10.
11 changes: 6 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ To set up subdomain URL routing and reversing in a Django project:
:class:`django.middleware.common.CommonMiddleware`, the subdomain middleware
should come before :class:`~django.middleware.common.CommonMiddleware`.
2. Configure your ``SUBDOMAIN_URLCONFS`` dictionary in your Django settings file.
3. Ensure that you've set up your ``SITE_ID`` in your Django settings file,
and that the ``Site.domain`` attribute for that site corresponds to the
domain name where users will be accessing your site at.
3. Ensure that `django.contrib.sites` is in your INSTALLED_APPS, you've set up your
``SITE_ID`` in your Django settings file, and that the ``Site.domain`` attribute
for that site corresponds to the domain name where users will be accessing your
site at.
4. If you want to use the subdomain-based ``{% url %}`` template tag, add
``subdomains`` to your ``INSTALLED_APPS``.

Expand Down Expand Up @@ -82,7 +83,7 @@ Resolving Named URLs by Subdomain
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Included is a :func:`subdomains.utils.reverse` function that responds similarly to
:func:`django.core.urlresolvers.reverse`, but accepts optional ``subdomain``
:func:`django.urls.reverse`, but accepts optional ``subdomain``
and ``scheme`` arguments and does not allow a ``urlconf`` parameter.

If no ``subdomain`` argument is provided, the URL will be resolved relative to
Expand All @@ -109,7 +110,7 @@ the ``ROOT_URLCONF``::
>>> reverse('login', subdomain='wildcard')
'http://wildcard.example.com/login/'

If a URL cannot be resolved, a :exc:`django.core.urlresolvers.NoReverseMatch`
If a URL cannot be resolved, a :exc:`django.urls.NoReverseMatch`
will be raised.

Resolving Named URLs in Templates
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def get_version():
install_requires = ['django']
tests_require = install_requires + ['mock']

setup(name='django-subdomains',
setup(
name='django-subdomains',
version=version,
url='http://github.com/tkaemming/django-subdomains/',
author='ted kaemming',
Expand Down
2 changes: 1 addition & 1 deletion subdomains/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = (2, 1, '0')
__version__ = (2, 2, '0')
15 changes: 13 additions & 2 deletions subdomains/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@

from subdomains.utils import get_domain

try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
# Pre Django 1.10 middleware does not require the mixin.
MiddlewareMixin = object


logger = logging.getLogger(__name__)
lower = operator.methodcaller('lower')

UNSET = object()
try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object

UNSET = MiddlewareMixin()


class SubdomainMiddleware(object):
class SubdomainMiddleware(MiddlewareMixin):
"""
A middleware class that adds a ``subdomain`` attribute to the current request.
"""
Expand Down
18 changes: 14 additions & 4 deletions subdomains/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.conf import settings

if not settings.configured:
MIDDLEWARES = (
'django.middleware.common.CommonMiddleware',
'subdomains.middleware.SubdomainURLRoutingMiddleware',
)
settings.configure(
INSTALLED_APPS=(
'django.contrib.sites',
Expand All @@ -13,10 +17,16 @@
},
},
SITE_ID=1,
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'subdomains.middleware.SubdomainURLRoutingMiddleware',
),
MIDDLEWARE_CLASSES=MIDDLEWARES,
MIDDLEWARE=MIDDLEWARES,
ALLOWED_HOSTS=[
'.example.com',
],
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
}
]
)


Expand Down
5 changes: 4 additions & 1 deletion subdomains/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
except ImportError: # Python 3
from urllib import parse as urlparse

from django.core.urlresolvers import NoReverseMatch, set_urlconf
try:
from django.urls import NoReverseMatch, set_urlconf
except ImportError: # Django<2.0
from django.core.urlresolvers import NoReverseMatch, set_urlconf
from django.template import Context, Template
from django.test import TestCase
from django.test.client import RequestFactory
Expand Down
17 changes: 15 additions & 2 deletions subdomains/tests/urls/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
try:
from django.conf.urls import patterns, url
from django.conf.urls import url
except ImportError:
from django.conf.urls.defaults import patterns, url # noqa
from django.conf.urls.defaults import url # noqa

try:
from django.conf.urls import patterns
except ImportError:
try:
from django.conf.urls.defaults import patterns # noqa
except ImportError:
def patterns(*args):
new_patterns = []
for a in args:
if a:
new_patterns.append(a)
return new_patterns

from subdomains.tests.urls.default import urlpatterns as default_patterns
from subdomains.tests.views import view
Expand Down
17 changes: 15 additions & 2 deletions subdomains/tests/urls/application.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
try:
from django.conf.urls import patterns, url
from django.conf.urls import url
except ImportError:
from django.conf.urls.defaults import patterns, url # noqa
from django.conf.urls.defaults import url # noqa

try:
from django.conf.urls import patterns
except ImportError:
try:
from django.conf.urls.defaults import patterns # noqa
except ImportError:
def patterns(*args):
new_patterns = []
for a in args:
if a:
new_patterns.append(a)
return new_patterns

from subdomains.tests.urls.default import urlpatterns as default_patterns
from subdomains.tests.views import view
Expand Down
17 changes: 15 additions & 2 deletions subdomains/tests/urls/default.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
try:
from django.conf.urls import patterns, url
from django.conf.urls import url
except ImportError:
from django.conf.urls.defaults import patterns, url # noqa
from django.conf.urls.defaults import url # noqa

try:
from django.conf.urls import patterns
except ImportError:
try:
from django.conf.urls.defaults import patterns # noqa
except ImportError:
def patterns(*args):
new_patterns = []
for a in args:
if a:
new_patterns.append(a)
return new_patterns

from subdomains.tests.views import view

Expand Down
17 changes: 15 additions & 2 deletions subdomains/tests/urls/marketing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
try:
from django.conf.urls import patterns, url
from django.conf.urls import url
except ImportError:
from django.conf.urls.defaults import patterns, url # noqa
from django.conf.urls.defaults import url # noqa

try:
from django.conf.urls import patterns
except ImportError:
try:
from django.conf.urls.defaults import patterns # noqa
except ImportError:
def patterns(*args):
new_patterns = []
for a in args:
if a:
new_patterns.append(a)
return new_patterns

from subdomains.tests.urls.default import urlpatterns as default_patterns

Expand Down
7 changes: 5 additions & 2 deletions subdomains/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from urllib.parse import urlunparse

from django.conf import settings
from django.core.urlresolvers import reverse as simple_reverse
try:
from django.urls import reverse as simple_reverse
except ImportError: # Django<2.0
from django.core.urlresolvers import reverse as simple_reverse


def current_site_domain():
Expand Down Expand Up @@ -42,7 +45,7 @@ def reverse(viewname, subdomain=None, scheme=None, args=None, kwargs=None,
current_app=None):
"""
Reverses a URL from the given parameters, in a similar fashion to
:meth:`django.core.urlresolvers.reverse`.
:meth:`django.urls.reverse`.

:param viewname: the name of URL
:param subdomain: the subdomain to use for URL reversing
Expand Down
13 changes: 10 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[tox]
envlist:
py26-django1.{4,5,6}
py27-django1.{4,5,6,7,8,9,X-dev}
py34-django1.{7,8,9,X-dev}
py35-django1.{8,9,X-dev}
py27-django1.{4,5,6,7,8,9,10,11,X-dev}
py34-django1.{7,8,9,10,11,X-dev}
py35-django1.{8,9,10,11,X-dev}

py34-django2.{0,1}
py35-django2.{0,1}

[testenv]
commands = python setup.py test
Expand All @@ -19,4 +22,8 @@ deps =
django1.7: Django==1.7.*
django1.8: Django==1.8.*
django1.9: Django==1.9.*
django1.10: Django==1.10.*
django1.11: Django==1.11.*
django2.0: Django==2.0.*
django2.1: Django==2.1.*
django1.X-dev: https://github.com/django/django/zipball/master