Skip to content

Commit

Permalink
Merge pull request #16 from metsavaht/feature/django-20-support
Browse files Browse the repository at this point in the history
Add Django 2.0 support
  • Loading branch information
jorgenader committed Jul 26, 2018
2 parents 2e4bdf1 + 3e92df6 commit 5a6e83d
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 123 deletions.
22 changes: 16 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
dist: trusty
sudo: false

language: python

python: "3.6"
Expand All @@ -8,24 +11,28 @@ install:
script:
- tox

matrix:
jobs:
include:
- python: "3.5"
env: TOXENV=py35-django110

- python: "3.5"
env: TOXENV=py35-django111

- python: "3.5"
env: TOXENV=py35-django200

- python: "3.6"
env: TOXENV=py36-django110

- python: "3.6"
env: TOXENV=py36-django111

after_success:
- test $TOXENV = "py36-django111" && coveralls
- python: "3.6"
env: TOXENV=py36-django200

jobs:
include:
- stage: deploy
env: TOXENV=py36-django111
env: TOXENV=py36-django200
deploy:
provider: pypi
user: thorgate
Expand All @@ -43,5 +50,8 @@ jobs:
distributions: sdist bdist_wheel
repo: thorgate/tg-react

after_success:
- test $TOXENV = "py36-django200" && coveralls

notifications:
email: false
2 changes: 0 additions & 2 deletions example/invalid_urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import unicode_literals

from django.conf.urls import include, url

from django.contrib import admin

admin.autodiscover()
Expand Down
4 changes: 1 addition & 3 deletions example/resolver_urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import unicode_literals

from django.conf.urls import include, url

from django.contrib import admin

admin.autodiscover()


urlpatterns = [
include(admin.site.urls),
admin.site.urls,
]
7 changes: 5 additions & 2 deletions example/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import unicode_literals

from django.conf.urls import include, url
try:
from django.urls import re_path as url
except ImportError:
from django.conf.urls import url

from django.contrib import admin

Expand All @@ -12,6 +15,6 @@ def test_view(request):


urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
url(r'^test/', test_view, name='test_view'),
]
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
Expand Down
13 changes: 13 additions & 0 deletions tests/test_flatten_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def test_url_flatten(self):
'admin::authUserChange': 'admin/auth/user/${arg0}/',
})

if LooseVersion(django.get_version()) == LooseVersion("2.0"):
expected_urls_dict.update({
'admin::authGroupAutocomplete': 'admin/auth/group/autocomplete/',
'admin::authGroupChange': 'admin/auth/group/${object_id}/change/',
'admin::authGroupDelete': 'admin/auth/group/${object_id}/delete/',
'admin::authGroupHistory': 'admin/auth/group/${object_id}/history/',
'admin::authUserAutocomplete': 'admin/auth/user/autocomplete/',
'admin::authUserChange': 'admin/auth/user/${object_id}/change/',
'admin::authUserDelete': 'admin/auth/user/${object_id}/delete/',
'admin::authUserHistory': 'admin/auth/user/${object_id}/history/',
'admin::authUserPasswordChange': 'admin/auth/user/${id}/password/',
})

self.assertEqual(flatten_urls('example.urls', None), expected_urls_dict)

def test_url_flatten_invalid_conf(self):
Expand Down
2 changes: 1 addition & 1 deletion tg_react/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '2.0.0a3'
__version__ = '2.0.0'

default_app_config = 'tg_react.apps.TgReactConfig'
31 changes: 23 additions & 8 deletions tg_react/apiurls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
import re

try:
from django.utils.module_loading import import_string
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string

try:
from django.urls import URLPattern as RegexURLPattern, URLResolver as RegexURLResolver
except ImportError:
from django.utils.module_loading import import_by_path as import_string
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver # Removed in Django 2.0


def ucfirst(word):
Expand All @@ -21,6 +21,16 @@ def to_camelcase(value):
return "".join([ucfirst(x) if i > 0 else x for i, x in enumerate(value.split('_'))])


def get_url_regex_pattern(urlpattern):
if hasattr(urlpattern, 'pattern'):
# Django 2.0
return urlpattern.pattern.regex

else:
# Django < 2.0
return urlpattern.regex


def tokenize_pattern(regex):
if not regex:
return regex
Expand All @@ -36,7 +46,7 @@ def tokenize_pattern(regex):
for key, idx in regex.groupindex.items():
pattern_str = re.sub(r"(\(\?P<%s>)[^\)]+\)" % key, "${%s}" % key, pattern_str)

return pattern_str.lstrip('^').rstrip('$')
return pattern_str.replace('\\/', '/').replace('\\/', '/').lstrip('^').rstrip('$')


def flatten_patterns(urlconf, base_path=None, namespace=None):
Expand All @@ -53,18 +63,23 @@ def flatten_patterns(urlconf, base_path=None, namespace=None):
urlconf = urlconf.url_patterns

for url in urlconf:
url_regex = None

if isinstance(url, (RegexURLPattern, RegexURLResolver)):
url_regex = get_url_regex_pattern(url)

if isinstance(url, RegexURLPattern):
if url.name == 'api-noop' or not url.name:
continue

url_result = '%s%s' % (base_path, tokenize_pattern(url.regex))
url_result = '%s%s' % (base_path, tokenize_pattern(url_regex))

result['%s%s' % (namespace, to_camelcase(url.name))] = url_result

elif isinstance(url, RegexURLResolver):
flattened = flatten_patterns(
url,
base_path=base_path + (tokenize_pattern(url.regex) or ''),
base_path=base_path + (tokenize_pattern(url_regex) or ''),
namespace=namespace + (url.namespace or '')
)

Expand Down
82 changes: 0 additions & 82 deletions tg_react/catalogue_legacy.py

This file was deleted.

14 changes: 4 additions & 10 deletions tg_react/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from django.utils import translation
from django.utils.translation.trans_real import DjangoTranslation

from tg_react.catalogue_legacy import get_catalogue_legacy


class DjangoLocaleData(object):
languages = settings.LANGUAGES
Expand All @@ -30,14 +28,10 @@ def __init__(self, domain=None, packages=None):
def get_catalog(self, locale):
"""Create Django translation catalogue for `locale`."""
with translation.override(locale):
try:
translation_engine = DjangoTranslation(locale, domain=self.domain, localedirs=self.paths)

trans_cat = translation_engine._catalog
trans_fallback_cat = translation_engine._fallback._catalog if translation_engine._fallback else {}
except Exception:
trans_cat = get_catalogue_legacy(self.domain, locale, self.packages)
trans_fallback_cat = get_catalogue_legacy(self.domain, settings.LANGUAGE_CODE, self.packages)
translation_engine = DjangoTranslation(locale, domain=self.domain, localedirs=self.paths)

trans_cat = translation_engine._catalog
trans_fallback_cat = translation_engine._fallback._catalog if translation_engine._fallback else {}

return trans_cat, trans_fallback_cat

Expand Down
11 changes: 4 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
[tox]
envlist =
py{27,34,35,36}-django{18,19,110,111}
py{35,36}-django{110,111,200}

[testenv]
basepython =
py27: python2.7
py34: python3.4
py35: python3.5
py36: python3.6
deps=
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11
django111: Django>1.10,<=1.11
coverage: Django>=1.10,<1.11
django200: Django>1.11,<=2.0.0
coverage: Django>1.11,<=2.0.0
-rrequirements-test.txt
commands=make test
setenv =
PYTHONPATH = {toxinidir}
whitelist_externals=make
passenv = *

[testenv:py36-django111]
[testenv:py36-django200]
commands = make test-full

0 comments on commit 5a6e83d

Please sign in to comment.