Skip to content

Commit

Permalink
Get django-reversion to work with django-money
Browse files Browse the repository at this point in the history
  • Loading branch information
szymi- committed Sep 16, 2020
1 parent 16c2329 commit ca0320e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 0 additions & 3 deletions src/ralph/__init__.py
@@ -1,4 +1,3 @@
from django.core import serializers
from django.db.models.options import Options

__version__ = '3.0.0'
Expand All @@ -13,5 +12,3 @@ def monkey_options_init(self, meta, app_label):
Options.__init__ = lambda self, meta, app_label=None: monkey_options_init(
self, meta, app_label
)

serializers.register_serializer("json", "ralph.lib.serializers")
3 changes: 3 additions & 0 deletions src/ralph/apps.py
Expand Up @@ -2,6 +2,7 @@
from importlib import import_module

from django.apps import AppConfig
from django.core import serializers


class RalphAppConfig(AppConfig):
Expand All @@ -15,6 +16,8 @@ def ready(self):
"""
super().ready()

serializers.register_serializer("json", "ralph.lib.serializers")

package = self.module.__name__
for module in self.get_load_modules_when_ready():
try:
Expand Down
23 changes: 14 additions & 9 deletions src/ralph/lib/serializers/__init__.py
Expand Up @@ -20,15 +20,17 @@ def Deserializer(stream_or_string, **options): # noqa
Deserialize a stream or string of JSON data.
"""
# Copied almost without changes from djmoney.serializers (django-money).
# Adding support for situation where old models to be deserialized have price field,
# but not price_currency field.
# In Ralph, price field existed before in various models as a Decimal field.
# All price fields were migrated to MoneyField without changing the original field name.
# This can cause problems in original django-money's implementation of Deserializer.
# This updated Deserializer is needed to get revision (django-revision)
# Adding support for situation where old models to be deserialized have
# price field, but not price_currency field.
# In Ralph, price field existed before in various models as
# a Decimal field. All price fields were migrated to MoneyField
# without changing the original field name. This can cause problems
# in original django-money's implementation of Deserializer.
# This updated Deserializer is needed to get reversion (django-reversion)
# to work in circumstances described above.

from django.core.serializers.python import Deserializer as PythonDeserializer, _get_model
from django.core.serializers.python import \
Deserializer as PythonDeserializer, _get_model

ignore = options.pop("ignorenonexistent", False)

Expand All @@ -55,7 +57,8 @@ def Deserializer(stream_or_string, **options): # noqa
field = Model._meta.get_field(field_name)
if isinstance(field, MoneyField) and field_value is not None:
try:
currency = obj["fields"][get_currency_field_name(field_name)]
currency = \
obj["fields"][get_currency_field_name(field_name)]
except KeyError:
currency = DEFAULT_CURRENCY_CODE
money_fields[field_name] = Money(field_value, currency)
Expand All @@ -70,4 +73,6 @@ def Deserializer(stream_or_string, **options): # noqa
except (GeneratorExit, DeserializationError):
raise
except Exception as exc:
six.reraise(DeserializationError, DeserializationError(exc), sys.exc_info()[2])
six.reraise(
DeserializationError, DeserializationError(exc), sys.exc_info()[2]
)

0 comments on commit ca0320e

Please sign in to comment.