Skip to content

Commit

Permalink
Fix import error for django 4.0 (#18)
Browse files Browse the repository at this point in the history
* Fix import error for django 4.0

The smart_text function has been deprecated from django 4.0 and is replaced by smart_text. To work with both django 4.0 and earlier versions, we try to import smart_text, and if that fails, we import smart_str and create an alias function called smart_text.

* move compatilbity imports into compatible.py

Co-authored-by: Johan Thelin <[email protected]>
  • Loading branch information
e8johan and Johan Thelin authored Jun 26, 2022
1 parent 60a9592 commit 785be32
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 15 deletions.
8 changes: 1 addition & 7 deletions compressor/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
from django.core.files.base import ContentFile
from django.utils.encoding import smart_bytes
from django.utils.functional import SimpleLazyObject

try:
from django.utils.encoding import force_text
except ImportError:
from django.utils.encoding import force_str
force_text = force_str

from compressor.compatible import force_text
from compressor.conf import settings
from compressor.storage import default_storage
from compressor.utils import get_mod_func
Expand Down
11 changes: 11 additions & 0 deletions compressor/compatible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
try:
from django.utils.encoding import smart_text
except ImportError:
from django.utils.encoding import smart_str
smart_text = smart_str

try:
from django.utils.encoding import force_text
except ImportError:
from django.utils.encoding import force_str
force_text = force_str
3 changes: 1 addition & 2 deletions compressor/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def shell_quote(s):
from django.core.files.temp import NamedTemporaryFile

import six
from django.utils.encoding import smart_text

from compressor.compatible import smart_text
from compressor.cache import cache, get_precompiler_cachekey

from compressor.conf import settings
Expand Down
2 changes: 1 addition & 1 deletion compressor/management/commands/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.core.management.base import BaseCommand, CommandError
import django.template
from django.template import Context
from django.utils.encoding import smart_text
from compressor.compatible import smart_text
from django.template.loader import get_template # noqa Leave this in to preload template locations
from django.template import engines

Expand Down
2 changes: 1 addition & 1 deletion compressor/parser/beautifulsoup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import smart_text
from compressor.compatible import smart_text

from compressor.parser import ParserBase

Expand Down
2 changes: 1 addition & 1 deletion compressor/parser/default_htmlparser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

import six
from django.utils.encoding import smart_text
from compressor.compatible import smart_text

from compressor.exceptions import ParserError
from compressor.parser import ParserBase
Expand Down
2 changes: 1 addition & 1 deletion compressor/parser/html5lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import smart_text
from compressor.compatible import smart_text
from django.utils.functional import cached_property

from compressor.exceptions import ParserError
Expand Down
2 changes: 1 addition & 1 deletion compressor/parser/lxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import six
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import smart_text
from compressor.compatible import smart_text
from django.utils.functional import cached_property

from compressor.exceptions import ParserError
Expand Down
3 changes: 2 additions & 1 deletion compressor/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import mock

import six
from django.utils.encoding import smart_text
from compressor.compatible import smart_text

from django.test import TestCase
from django.test.utils import override_settings

Expand Down

0 comments on commit 785be32

Please sign in to comment.