Skip to content

Commit

Permalink
Replace deprecated force_text with force_str
Browse files Browse the repository at this point in the history
  • Loading branch information
GregKaleka committed Jul 2, 2020
1 parent a661d8b commit 49776b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
34 changes: 17 additions & 17 deletions reversion_compare/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.encoding import force_text
from django.utils.encoding import force_str
from django.utils.translation import ugettext as _

from reversion import is_registered
Expand All @@ -30,7 +30,7 @@ class FieldVersionDoesNotExist:
"""

def __str__(self):
return force_text(_("Field didn't exist!"))
return force_str(_("Field didn't exist!"))


DOES_NOT_EXIST = FieldVersionDoesNotExist()
Expand All @@ -55,12 +55,12 @@ def __init__(self, field, field_name, obj, version_record, follow):
def _obj_repr(self, obj):
# FIXME: How to create a better representation of the current value?
try:
return force_text(obj)
return force_str(obj)
except Exception:
return repr(obj)

def _choices_repr(self, obj):
return force_text(dict(self.field.flatchoices).get(obj, obj), strings_only=True)
return force_str(dict(self.field.flatchoices).get(obj, obj), strings_only=True)

def _to_string_ManyToManyField(self):
return ", ".join(self._obj_repr(item) for item in self.get_many_to_many())
Expand Down Expand Up @@ -121,12 +121,12 @@ def get_reverse_foreign_key(self):
if self.field.related_name and hasattr(obj, self.field.related_name):
if isinstance(self.field, models.fields.related.OneToOneRel):
try:
ids = {force_text(getattr(obj, force_text(self.field.related_name)).pk)}
ids = {force_str(getattr(obj, force_str(self.field.related_name)).pk)}
except ObjectDoesNotExist:
ids = set()
else:
# If there is a _ptr this is a multi-inheritance table and inherits from a non-abstract class
ids = {force_text(v.pk) for v in getattr(obj, force_text(self.field.related_name)).all()}
ids = {force_str(v.pk) for v in getattr(obj, force_str(self.field.related_name)).all()}
if not ids and any([f.name.endswith("_ptr") for f in obj._meta.get_fields()]):
# If there is a _ptr this is a multi-inheritance table and inherits from a non-abstract class
# lets try and get the parent items associated entries for this field
Expand All @@ -135,8 +135,8 @@ def get_reverse_foreign_key(self):
).all()
for p in others:
p_obj = p._object_version.object
if not isinstance(p_obj, type(obj)) and hasattr(p_obj, force_text(self.field.related_name)):
ids = {force_text(v.pk) for v in getattr(p_obj, force_text(self.field.related_name)).all()}
if not isinstance(p_obj, type(obj)) and hasattr(p_obj, force_str(self.field.related_name)):
ids = {force_str(v.pk) for v in getattr(p_obj, force_str(self.field.related_name)).all()}
else:
return {}, {}, [] # TODO: refactor that

Expand All @@ -154,7 +154,7 @@ def get_many_to_many(self):
return {}, {}, [] # TODO: refactor that

try:
ids = frozenset(map(force_text, self.value))
ids = frozenset(map(force_str, self.value))
except TypeError:
# catch errors e.g. produced by taggit's TaggableManager
logger.exception("Can't collect m2m ids")
Expand Down Expand Up @@ -189,7 +189,7 @@ def get_many_to_something(self, target_ids, related_model, is_reverse=False):
# )
if potentially_missing_ids:
missing_objects_dict = {
force_text(rel.pk): rel
force_str(rel.pk): rel
for rel in related_model.objects.filter(pk__in=potentially_missing_ids).iterator()
if is_registered(rel.__class__) or not self.ignore_not_registered
}
Expand Down Expand Up @@ -392,13 +392,13 @@ def get_m2s_change_info(self, obj1_data, obj2_data):
raise RuntimeError()

# In Place Sorting of Lists (exclude changed since its a tuple)
removed_items.sort(key=lambda item: force_text(item))
added_items.sort(key=lambda item: force_text(item))
same_items.sort(key=lambda item: force_text(item))
deleted1.sort(key=lambda item: force_text(item))
same_missing_objects = sorted(same_missing_objects_dict.values(), key=lambda item: force_text(item))
removed_missing_objects = sorted(removed_missing_objects_dict.values(), key=lambda item: force_text(item))
added_missing_objects = sorted(added_missing_objects_dict.values(), key=lambda item: force_text(item))
removed_items.sort(key=lambda item: force_str(item))
added_items.sort(key=lambda item: force_str(item))
same_items.sort(key=lambda item: force_str(item))
deleted1.sort(key=lambda item: force_str(item))
same_missing_objects = sorted(same_missing_objects_dict.values(), key=lambda item: force_str(item))
removed_missing_objects = sorted(removed_missing_objects_dict.values(), key=lambda item: force_str(item))
added_missing_objects = sorted(added_missing_objects_dict.values(), key=lambda item: force_str(item))

return {
"changed_items": changed_items,
Expand Down
6 changes: 3 additions & 3 deletions reversion_compare/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from django.utils.encoding import force_text
from django.utils.encoding import force_str
from django.utils.html import escape
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -117,8 +117,8 @@ def html_diff(value1, value2, cleanup=SEMANTIC):
The cleanup parameter can be SEMANTIC, EFFICIENCY or None to clean up the diff
for greater human readibility.
"""
value1 = force_text(value1, errors='replace')
value2 = force_text(value2, errors='replace')
value1 = force_str(value1, errors='replace')
value2 = force_str(value2, errors='replace')

if len(value1) > CHANGE_DIFF_THRESHOLD or len(value2) > CHANGE_DIFF_THRESHOLD:
# Bigger values -> use Google diff-match-patch
Expand Down
8 changes: 4 additions & 4 deletions reversion_compare/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from django.db import models
from django.template.loader import render_to_string
from django.utils.encoding import force_text
from django.utils.encoding import force_str

from reversion_compare.compare import CompareObjects
from reversion_compare.helpers import html_diff
Expand Down Expand Up @@ -162,14 +162,14 @@ def generic_add_remove(self, raw_value1, raw_value2, value1, value2):
def compare_ForeignKey(self, obj_compare):
related1, related2 = obj_compare.get_related()
# obj_compare.debug()
value1, value2 = force_text(related1), force_text(related2)
value1, value2 = force_str(related1), force_str(related2)
return self.generic_add_remove(related1, related2, value1, value2)

def simple_compare_ManyToManyField(self, obj_compare):
""" comma separated list of all m2m objects """
m2m1, m2m2 = obj_compare.get_many_to_many()
old = ", ".join(force_text(item) for item in m2m1)
new = ", ".join(force_text(item) for item in m2m2)
old = ", ".join(force_str(item) for item in m2m1)
new = ", ".join(force_str(item) for item in m2m2)
html = html_diff(old, new)
return html

Expand Down

0 comments on commit 49776b0

Please sign in to comment.