Skip to content

Commit

Permalink
Added django 4.0 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Dec 8, 2021
1 parent bc1d47a commit 2bb2809
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
26 changes: 13 additions & 13 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from django.conf import settings
from django.test import TransactionTestCase
from django.utils.encoding import force_text

from treenode.cache import clear_cache
from treenode.compat import force_str
from treenode.utils import join_pks

from .models import (
Expand Down Expand Up @@ -317,19 +317,19 @@ def test_get_display(self):
o = self.__create_cat(name='ò', parent=i)
u = self.__create_cat(name='ù', parent=o)
opts = {'indent': False, 'mark': '- '}
self.assertEqual(a.get_display(**opts), force_text('à'))
self.assertEqual(c.get_display(**opts), force_text('ç'))
self.assertEqual(e.get_display(**opts), force_text('è'))
self.assertEqual(i.get_display(**opts), force_text('ì'))
self.assertEqual(o.get_display(**opts), force_text('ò'))
self.assertEqual(u.get_display(**opts), force_text('ù'))
self.assertEqual(a.get_display(**opts), force_str('à'))
self.assertEqual(c.get_display(**opts), force_str('ç'))
self.assertEqual(e.get_display(**opts), force_str('è'))
self.assertEqual(i.get_display(**opts), force_str('ì'))
self.assertEqual(o.get_display(**opts), force_str('ò'))
self.assertEqual(u.get_display(**opts), force_str('ù'))
opts = {'indent': True, 'mark': '- '}
self.assertEqual(a.get_display(**opts), force_text('à'))
self.assertEqual(c.get_display(**opts), force_text('- ç'))
self.assertEqual(e.get_display(**opts), force_text('- - è'))
self.assertEqual(i.get_display(**opts), force_text('- - - ì'))
self.assertEqual(o.get_display(**opts), force_text('- - - - ò'))
self.assertEqual(u.get_display(**opts), force_text('- - - - - ù'))
self.assertEqual(a.get_display(**opts), force_str('à'))
self.assertEqual(c.get_display(**opts), force_str('- ç'))
self.assertEqual(e.get_display(**opts), force_str('- - è'))
self.assertEqual(i.get_display(**opts), force_str('- - - ì'))
self.assertEqual(o.get_display(**opts), force_str('- - - - ò'))
self.assertEqual(u.get_display(**opts), force_str('- - - - - ù'))

def test_get_first_child(self):
self.__create_cat_tree()
Expand Down
3 changes: 0 additions & 3 deletions tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

from django.conf import settings
from django.test import TransactionTestCase
# from django.utils.encoding import force_text

# from treenode.cache import clear_cache
from treenode.debug import debug_performance
# from treenode.utils import join_pks, split_pks
from treenode.signals import no_signals

from .models import Category
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ envlist =
py37-{dj20,dj21,dj22,dj30,dj31,dj32}-{sqlite,postgres},
py38-{dj22,dj30,dj31,dj32}-{sqlite,postgres},
py39-{dj22,dj30,dj31,dj32}-{sqlite,postgres},
py310-{dj32}-{sqlite,postgres},
py310-{dj32,dj40}-{sqlite,postgres},

[gh-actions]
python =
Expand All @@ -29,6 +29,7 @@ deps =
dj30: Django == 3.0.*
dj31: Django == 3.1.*
dj32: Django == 3.2.*
dj40: Django == 4.0.*
# mysql: mysqlclient
postgres: psycopg2-binary
coverage
Expand Down
8 changes: 8 additions & 0 deletions treenode/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

import django

if django.VERSION >= (3, 0):
from django.utils.encoding import force_str
else:
from django.utils.encoding import force_text as force_str
8 changes: 4 additions & 4 deletions treenode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction
from django.utils.encoding import force_text
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _

from six import python_2_unicode_compatible

from . import classproperty
from .cache import clear_cache, query_cache, update_cache
from .compat import force_str
from .debug import debug_performance
from .memory import clear_refs, update_refs
from .signals import connect_signals, no_signals
Expand Down Expand Up @@ -201,9 +201,9 @@ def get_descendants_tree_display(self, cache=True):

def get_display(self, indent=True, mark='— '):
indentation = (mark * self.tn_ancestors_count) if indent else ''
indentation = force_text(indentation)
indentation = force_str(indentation)
text = self.get_display_text()
text = force_text(text)
text = force_str(text)
return indentation + text

def get_display_text(self):
Expand All @@ -219,7 +219,7 @@ def get_display_text(self):
text = getattr(self, field_name, '')
if not text and self.pk:
text = self.pk
return force_text(text)
return force_str(text)

def get_first_child(self, cache=True):
return self.get_children(cache=cache)[0] \
Expand Down

0 comments on commit 2bb2809

Please sign in to comment.