Skip to content

Commit ea123f6

Browse files
federicobondjambonrose
authored andcommitted
Add support for Django 2.1 (#57)
1 parent 13c0d87 commit ea123f6

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

django_coverage_plugin/plugin.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,35 @@
1414

1515
import django
1616
import django.template
17-
from django.template.base import (
18-
Lexer, TextNode, NodeList, Template,
19-
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
20-
)
17+
from django.template.base import Lexer, TextNode, NodeList, Template
2118
from django.templatetags.i18n import BlockTranslateNode
19+
2220
try:
2321
from django.template.defaulttags import VerbatimNode
2422
except ImportError:
2523
# Django 1.4 didn't have VerbatimNode
2624
VerbatimNode = None
2725

26+
try:
27+
from django.template.base import TokenType
28+
29+
def _token_name(token_type):
30+
token_type.name.capitalize()
31+
32+
except ImportError:
33+
# Django <2.1 uses separate constants for token types
34+
from django.template.base import (
35+
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR
36+
)
37+
38+
class TokenType:
39+
TEXT = TOKEN_TEXT
40+
VAR = TOKEN_VAR
41+
BLOCK = TOKEN_BLOCK
42+
43+
def _token_name(token_type):
44+
return TOKEN_MAPPING[token_type]
45+
2846

2947
class DjangoTemplatePluginException(Exception):
3048
"""Used for any errors from the plugin itself."""
@@ -298,20 +316,20 @@ def lines(self):
298316
if SHOW_PARSING:
299317
print(
300318
"%10s %2d: %r" % (
301-
TOKEN_MAPPING[token.token_type],
319+
_token_name(token.token_type),
302320
token.lineno,
303321
token.contents,
304322
)
305323
)
306-
if token.token_type == TOKEN_BLOCK:
324+
if token.token_type == TokenType.BLOCK:
307325
if token.contents == "endcomment":
308326
comment = False
309327
continue
310328

311329
if comment:
312330
continue
313331

314-
if token.token_type == TOKEN_BLOCK:
332+
if token.token_type == TokenType.BLOCK:
315333
if token.contents.startswith("endblock"):
316334
inblock = False
317335
elif token.contents.startswith("block"):
@@ -340,10 +358,10 @@ def lines(self):
340358

341359
source_lines.add(token.lineno)
342360

343-
elif token.token_type == TOKEN_VAR:
361+
elif token.token_type == TokenType.VAR:
344362
source_lines.add(token.lineno)
345363

346-
elif token.token_type == TOKEN_TEXT:
364+
elif token.token_type == TokenType.TEXT:
347365
if extends and not inblock:
348366
continue
349367
# Text nodes often start with newlines, but we don't want to

tox.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
envlist =
1818
py27-django{18,19,110,111,111tip},
1919
py34-django{18,19,110,111,111tip,20},
20-
py35-django{18,19,110,111,111tip,20,tip},
21-
py36-django{18,19,110,111,111tip,20,tip},
20+
py35-django{18,19,110,111,111tip,20,21,tip},
21+
py36-django{18,19,110,111,111tip,20,21,tip},
22+
py37-django{20,21,tip},
2223
check,doc
2324

2425
[testenv]
@@ -30,6 +31,7 @@ deps =
3031
django111: Django >=1.11, <2.0
3132
django111tip: https://github.com/django/django/archive/stable/1.11.x.tar.gz
3233
django20: Django>=2.0b1,<2.1
34+
django21: Django>=2.1, <2.2
3335
djangotip: https://github.com/django/django/archive/master.tar.gz
3436

3537
commands =

0 commit comments

Comments
 (0)