Skip to content

Commit

Permalink
Merge pull request #89 from lampslave/compare_unicode
Browse files Browse the repository at this point in the history
compare unicode instead of bytes on python 2
  • Loading branch information
jedie authored Apr 10, 2017
2 parents db19112 + 72bda7a commit 9005d24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 12 additions & 12 deletions reversion_compare/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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 str(obj)
return force_text(obj)
except Exception:
return repr(obj)

Expand Down Expand Up @@ -117,12 +117,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, str(self.field.related_name)).pk), }
ids = {force_text(getattr(obj, force_text(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.id) for v in getattr(obj, str(self.field.related_name)).all()}
ids = {force_text(v.id) for v in getattr(obj, force_text(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 @@ -134,8 +134,8 @@ def get_reverse_foreign_key(self):
p_obj = getattr(p, '_object_version').object
else:
p_obj = getattr(p, 'object_version').object
if type(p_obj) != type(obj) and hasattr(p_obj, str(self.field.related_name)):
ids = {force_text(v.id) for v in getattr(p_obj, str(self.field.related_name)).all()}
if type(p_obj) != type(obj) and hasattr(p_obj, force_text(self.field.related_name)):
ids = {force_text(v.id) for v in getattr(p_obj, force_text(self.field.related_name)).all()}
else:
return {}, {}, [] # TODO: refactor that

Expand Down Expand Up @@ -388,13 +388,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: str(item))
added_items.sort(key=lambda item: str(item))
same_items.sort(key=lambda item: str(item))
deleted1.sort(key=lambda item: str(item))
same_missing_objects = sorted(same_missing_objects_dict.values(), key=lambda item: str(item))
removed_missing_objects = sorted(removed_missing_objects_dict.values(), key=lambda item: str(item))
added_missing_objects = sorted(added_missing_objects_dict.values(), key=lambda item: str(item))
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))

return {
"changed_items": changed_items,
Expand Down
7 changes: 4 additions & 3 deletions reversion_compare/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import django
from django.db import models
from django.template.loader import render_to_string
from django.utils.encoding import force_text

from reversion_compare.helpers import html_diff
from reversion_compare.compare import CompareObjects
Expand Down Expand Up @@ -179,14 +180,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 = str(related1), str(related2)
value1, value2 = force_text(related1), force_text(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([str(item) for item in m2m1])
new = ", ".join([str(item) for item in m2m2])
old = ", ".join([force_text(item) for item in m2m1])
new = ", ".join([force_text(item) for item in m2m2])
html = html_diff(old, new)
return html

Expand Down

0 comments on commit 9005d24

Please sign in to comment.