Skip to content

Commit

Permalink
Merge pull request #61 from vdboor/fix-m2m
Browse files Browse the repository at this point in the history
Fix Python error when ManyToMany relations didn't exist.
  • Loading branch information
jedie committed Feb 16, 2016
2 parents 2e7b956 + 1a61c0b commit 5d42adf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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 ([], [], [], []) # TODO: refactory that

ids = None
if self.has_int_pk:
ids = [int(v) for v in self.value] # is: version.field_dict[field.name]
Expand Down
2 changes: 1 addition & 1 deletion reversion_compare/locale/el/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ msgid "Compare %(name)s"
msgstr "Σύγκριση %(name)s"

#: .\compare.py:34
msgid "Field Didn't exist!"
msgid "Field didn't exist!"
msgstr "Το πεδίο δεν υπήρχε"

#: .\templates\reversion-compare\action_list_partial.html:9
Expand Down
2 changes: 1 addition & 1 deletion reversion_compare/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ msgid "Compare %(name)s"
msgstr "Vergelijk %(name)s"

#: .\compare.py:34
msgid "Field Didn't exist!"
msgid "Field didn't exist!"
msgstr "Veld bestond niet!"

#: .\templates\reversion-compare\action_list_partial.html:9
Expand Down

0 comments on commit 5d42adf

Please sign in to comment.