diff --git a/reversion_compare/compare.py b/reversion_compare/compare.py index 1fbedd0..471ae40 100644 --- a/reversion_compare/compare.py +++ b/reversion_compare/compare.py @@ -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) @@ -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 @@ -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 @@ -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, diff --git a/reversion_compare/mixins.py b/reversion_compare/mixins.py index 688531c..a26bcc0 100644 --- a/reversion_compare/mixins.py +++ b/reversion_compare/mixins.py @@ -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 @@ -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