Skip to content

Commit

Permalink
Fix Python error when ManyToMany relations didn't exist.
Browse files Browse the repository at this point in the history
This avoids storing an ugettext value in self.version.
It's value got parsed in `ids = [int(v) for v in self.value]`
causing an `int("F")` call that fails.

Instead, a sentinal value is introduced that can be recognized in the
code, yet output a meaningful message when it's shown to the user.

Fixes jedie#44
  • Loading branch information
vdboor committed Feb 16, 2016
1 parent 2e7b956 commit b5b461e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion reversion_compare/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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, python_2_unicode_compatible
from django.utils.translation import ugettext as _


Expand All @@ -23,6 +24,18 @@
logger = logging.getLogger(__name__)


@python_2_unicode_compatible
class FieldVersionDoesNotExist(object):
"""
Sentinel object to handle missing fields
"""

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

DOES_NOT_EXIST = FieldVersionDoesNotExist()


class CompareObject(object):
def __init__(self, field, field_name, obj, version, has_int_pk, adapter):
self.field = field
Expand All @@ -32,7 +45,7 @@ def __init__(self, field, field_name, obj, version, has_int_pk, adapter):
self.has_int_pk = has_int_pk
self.adapter = adapter
# try and get a value, if none punt
self.value = version.field_dict.get(field_name, _("Field Didn't exist!"))
self.value = version.field_dict.get(field_name, DOES_NOT_EXIST)

def _obj_repr(self, obj):
# FIXME: How to create a better representation of the current value?
Expand Down Expand Up @@ -122,6 +135,9 @@ def get_many_to_many(self):
"""
if self.field.get_internal_type() != "ManyToManyField": # FIXME!
return ([], [], [], []) # TODO: refactory that
elif self.value is DOES_NOT_EXIST:
return ([], [], [], [])

ids = None
if self.has_int_pk:
ids = [int(v) for v in self.value] # is: version.field_dict[field.name]
Expand Down

0 comments on commit b5b461e

Please sign in to comment.