Skip to content

Commit 056df76

Browse files
author
Nikos Vasileiou
authored
Merge pull request #102 from transifex/django4
Native SDK: Add support for Django 4.x
2 parents 3da588c + e065874 commit 056df76

6 files changed

Lines changed: 100 additions & 7 deletions

File tree

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
docker run -e CODECOV_TOKEN=$CODECOV_TOKEN --rm native sh -c 'pytest --cov --cov-report=term-missing && codecov'
2828
env:
2929
CODECOV_TOKEN: ${{secrets.codecov_token}}
30+
3031
python3_6-django2_0-tests:
3132
runs-on: ubuntu-latest
3233
steps:
@@ -37,6 +38,7 @@ jobs:
3738
docker run -e CODECOV_TOKEN=$CODECOV_TOKEN --rm native sh -c 'pytest --cov --cov-report=term-missing && codecov'
3839
env:
3940
CODECOV_TOKEN: ${{secrets.codecov_token}}
41+
4042
python3_7-django2_2-tests:
4143
runs-on: ubuntu-latest
4244
steps:
@@ -47,6 +49,7 @@ jobs:
4749
docker run -e CODECOV_TOKEN=$CODECOV_TOKEN --rm native sh -c 'pytest --cov --cov-report=term-missing && codecov'
4850
env:
4951
CODECOV_TOKEN: ${{secrets.codecov_token}}
52+
5053
python3_8-django2_2-tests:
5154
runs-on: ubuntu-latest
5255
steps:
@@ -57,6 +60,7 @@ jobs:
5760
docker run -e CODECOV_TOKEN=$CODECOV_TOKEN --rm native sh -c 'pytest --cov --cov-report=term-missing && codecov'
5861
env:
5962
CODECOV_TOKEN: ${{secrets.codecov_token}}
63+
6064
python3_9-django3_2-tests:
6165
runs-on: ubuntu-latest
6266
steps:
@@ -68,6 +72,17 @@ jobs:
6872
env:
6973
CODECOV_TOKEN: ${{secrets.codecov_token}}
7074

75+
python3_11-django4_1-tests:
76+
runs-on: ubuntu-latest
77+
steps:
78+
- uses: actions/checkout@v2
79+
- name: Test py3.11-dj4.1
80+
run: |
81+
docker build --build-arg PYTHON_VERSION=3.11 --build-arg DJANGO_VERSION=4.1 -t native -f Dockerfile-tmpl .
82+
docker run -e CODECOV_TOKEN=$CODECOV_TOKEN --rm native sh -c 'pytest --cov --cov-report=term-missing && codecov'
83+
env:
84+
CODECOV_TOKEN: ${{secrets.codecov_token}}
85+
7186
publish-pypi:
7287
needs: [
7388
quality-checks, python3_6-django1_11-tests,

Makefile

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
CUR_PATH=$(shell pwd)
22
FILES=$(shell git ls-files transifex* tests*)
33

4-
build:
4+
# --- Build ---
5+
6+
build: \
7+
build_dj1.11_py3.6 \
8+
build_dj2.0_py3.6 \
9+
build_dj2.2_py3.8 \
10+
build_dj3.2_py3.9 \
11+
build_dj4.1_py3.11
12+
13+
build_dj1.11_py3.6:
514
DOCKER_BUILDKIT=1 docker build \
615
--no-cache \
716
--progress=plain \
817
--build-arg PYTHON_VERSION=3.6 \
918
--build-arg DJANGO_VERSION=1.11 \
1019
-t native:3.6-1.11-latest \
1120
-f Dockerfile-tmpl .
21+
22+
build_dj2.0_py3.6:
1223
DOCKER_BUILDKIT=1 docker build \
1324
--no-cache \
1425
--progress=plain \
1526
--build-arg PYTHON_VERSION=3.6 \
1627
--build-arg DJANGO_VERSION=2.0 \
1728
-t native:3.6-2.0-latest \
1829
-f Dockerfile-tmpl .
30+
31+
build_dj2.2_py3.8:
1932
DOCKER_BUILDKIT=1 docker build \
2033
--no-cache \
2134
--progress=plain \
2235
--build-arg PYTHON_VERSION=3.8 \
2336
--build-arg DJANGO_VERSION=2.2 \
2437
-t native:3.8-2.2-latest \
2538
-f Dockerfile-tmpl .
39+
40+
build_dj3.2_py3.9:
2641
DOCKER_BUILDKIT=1 docker build \
2742
--no-cache \
2843
--progress=plain \
@@ -31,13 +46,26 @@ build:
3146
-t native:3.9-3.2-latest \
3247
-f Dockerfile-tmpl .
3348

49+
build_dj4.1_py3.11:
50+
DOCKER_BUILDKIT=1 docker build \
51+
--no-cache \
52+
--progress=plain \
53+
--build-arg PYTHON_VERSION=3.11 \
54+
--build-arg DJANGO_VERSION=4.1 \
55+
-t native:3.11-4.1-latest \
56+
-f Dockerfile-tmpl .
57+
58+
# --- Code quality ---
59+
3460
code_quality:
3561
git diff origin/devel..$(git rev-parse HEAD) --name-only | \
3662
xargs docker run --rm \
3763
--user $$(id -u):$$(id -g) \
3864
--mount src="$$(pwd)",target=/src,type=bind \
3965
transifex/txlint --files
4066

67+
# --- Shell ---
68+
4169
shell_dj1.11_py3.6:
4270
# Django 1.11 (3.6)
4371
docker run --rm -it --entrypoint bash native:3.6-1.11-latest
@@ -54,27 +82,51 @@ shell_dj3.2_py3.9:
5482
# Django 3.2 (Python 3.9)
5583
docker run --rm -it --entrypoint bash native:3.9-3.2-latest
5684

57-
localtests:
85+
shell_dj4.1_py3.11:
86+
# Django 4.1 (Python 3.11)
87+
docker run --rm -it --entrypoint bash native:3.11-4.1-latest
88+
89+
# --- Tests ---
90+
91+
localtests: \
92+
tests_dj1.11_py3.6 \
93+
tests_dj2.0_py3.6 \
94+
tests_dj2.2_py3.8 \
95+
tests_dj3.2_py3.9 \
96+
tests_dj4.1_py3.11 \
97+
tests_coverage
98+
99+
tests_dj1.11_py3.6:
58100
# Django 1.11 (3.6)
59101
docker run -v $(CUR_PATH):/usr/app \
60102
--rm native:3.6-1.11-latest\
61103
pytest --cov --cov-append --cov-report=term-missing
62104

105+
tests_dj2.0_py3.6:
63106
# Django 2.0 (Python 3.6)
64107
docker run -v $(CUR_PATH):/usr/app \
65108
--rm native:3.6-2.0-latest\
66109
pytest --cov --cov-append --cov-report=term-missing
67110

111+
tests_dj2.2_py3.8:
68112
# Django 2.2 (Python 3.8)
69113
docker run -v $(CUR_PATH):/usr/app \
70114
--rm native:3.8-2.2-latest\
71115
pytest --cov --cov-append --cov-report=term-missing
72116

117+
tests_dj3.2_py3.9:
73118
# Django 3.2 (Python 3.9)
74119
docker run -v $(CUR_PATH):/usr/app \
75120
--rm native:3.9-3.2-latest\
76121
pytest --cov --cov-append --cov-report=term-missing
77122

123+
tests_dj4.1_py3.11:
124+
# Django 4.1 (Python 3.11)
125+
docker run -v $(CUR_PATH):/usr/app \
126+
--rm native:3.11-4.1-latest\
127+
pytest --cov --cov-append --cov-report=term-missing
128+
129+
tests_coverage:
78130
# Coverage report
79131
docker run -v $(CUR_PATH):/usr/app \
80132
--rm native:3.6-1.11-latest \

transifex/native/django/management/utils/push.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import sys
55
import time
66

7+
from django import VERSION as DJANGO_VERSION
78
from django.conf import settings
89
from django.core.management.utils import handle_extensions
9-
from django.utils.encoding import force_text
1010
from transifex.common.console import Color
1111
from transifex.common.utils import generate_hashed_key, generate_key
1212
from transifex.native import tx
@@ -16,6 +16,11 @@
1616
extract_transifex_template_strings
1717
from transifex.native.parsing import Extractor
1818

19+
if DJANGO_VERSION[0] >= 4:
20+
from django.utils.encoding import force_str as force_text
21+
else:
22+
from django.utils.encoding import force_text
23+
1924

2025
class Push(CommandMixin):
2126
def add_arguments(self, subparsers):

transifex/native/django/templatetags/transifex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,24 @@ def render(self, context):
259259
# it back in place before returning from render.
260260
# https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#thread-safety-considerations # noqa
261261
old_source_string_var = self.source_string.var
262+
try:
263+
old_is_var = self.source_string.is_var
264+
except AttributeError:
265+
old_is_var = None
262266

263267
# Now we resolve the full source filter expression, after having
264268
# replaced its text with the outcome of the translation, in order to
265269
# apply the expression's filters to the translation. The translation is
266270
# marked as safe in order to prevent further escaping attempts that
267271
# would introduce the danger of double escaping (eg `<` => `&amp;lt;`)
268272
self.source_string.var = mark_safe(result)
273+
self.source_string.is_var = False
274+
269275
result = self.source_string.resolve(context)
276+
270277
self.source_string.var = old_source_string_var
278+
if old_is_var is not None:
279+
self.source_string.is_var = old_is_var
271280

272281
if self.asvar is not None:
273282
# Save the translation outcome to a context variable

transifex/native/django/tools/migrations/templatetags.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
from __future__ import unicode_literals
88

99
from django import VERSION as DJANGO_VERSION
10-
from django.template.base import TRANSLATOR_COMMENT_MARK, DebugLexer, Parser
10+
from django.template.base import DebugLexer, Parser
1111
from django.template.defaulttags import token_kwargs
1212
from django.templatetags.i18n import do_block_translate, do_translate
13-
from django.utils.encoding import force_text
1413
from django.utils.html import escape as escape_html
1514
from transifex.common._compat import string_types, text_type
1615
from transifex.native.django.compat import (TOKEN_BLOCK, TOKEN_COMMENT,
@@ -21,6 +20,13 @@
2120
FileMigration,
2221
StringMigration)
2322

23+
if DJANGO_VERSION[0] >= 4:
24+
from django.utils.translation.template import TRANSLATOR_COMMENT_MARK
25+
from django.utils.encoding import force_str as force_text
26+
else:
27+
from django.template.base import TRANSLATOR_COMMENT_MARK
28+
from django.utils.encoding import force_text
29+
2430
COMMENT_FOUND = object()
2531

2632

@@ -52,7 +58,8 @@ def _get_ordered_tokens(parser):
5258
This method is useful when we want to iterate all the remaining
5359
tokens of the parser in the correct order.
5460
"""
55-
if DJANGO_VERSION[0] >= 3 and DJANGO_VERSION[1] > 1:
61+
major, minor = DJANGO_VERSION[:2]
62+
if major == 3 and minor > 1 or major > 3:
5663
return reversed(parser.tokens)
5764
return parser.tokens
5865

transifex/native/django/utils/templates.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
from __future__ import unicode_literals
22

3+
from django import VERSION as DJANGO_VERSION
34
from django.template.base import Lexer, Parser
4-
from django.utils.encoding import force_text
55
from transifex.common._compat import string_types
66
from transifex.common.utils import generate_key
77
from transifex.native.consts import KEY_CONTEXT
88
from transifex.native.django.compat import TOKEN_BLOCK
99
from transifex.native.django.templatetags.transifex import do_t
1010
from transifex.native.parsing import SourceString
1111

12+
if DJANGO_VERSION[0] >= 4:
13+
from django.utils.encoding import force_str as force_text
14+
else:
15+
from django.utils.encoding import force_text
16+
1217
# Django template consts
1318
LOAD_TAG = 'load'
1419
WITH_TAG = 'with'

0 commit comments

Comments
 (0)