Skip to content

Commit

Permalink
attempt to use django JSONField if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Starner committed Dec 22, 2020
1 parent 35f9a0e commit acba5e6
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions actstream/jsonfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
afterwards.
'''
from collections import namedtuple

from django.db import models
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string

from actstream.settings import USE_JSONFIELD


__all__ = ('DataField', 'register_app')

JSONFieldImport = namedtuple('JSONFieldImport', ['field_import', 'register_app_import'])

def register_app(app):
"""Noop unless django-jsonfield-compat overwrites it."""
Expand All @@ -30,19 +34,26 @@ def register_app(app):

DataField = models.TextField

possible_imports = [
JSONFieldImport('django.db.models.JSONField', None),
JSONFieldImport('jsonfield_compat.JSONField', 'jsonfield_compat.register_app'),
JSONFieldImport('django_mysql.models.JSONField', None)
]

if USE_JSONFIELD:
try:
from jsonfield_compat import JSONField, register_app
DataField = JSONField
except ImportError as err:
for possible_import in possible_imports:
module, register_app_import = possible_import
try:
from django_mysql.models import JSONField
DataField = JSONField

item = import_string(module)
DataField = item
if register_app_import:
register_app = import_string(register_app_import)
except ImportError:
raise ImproperlyConfigured(
'You must either install django-jsonfield + '
'django-jsonfield-compat, or django-mysql as an '
'alternative, if you wish to use a JSONField on your '
'actions'
)
pass

raise ImproperlyConfigured(
"You must either install django-jsonfield + "
"django-jsonfield-compat, or django-mysql as an "
"alternative, if you wish to use a JSONField on your "
"actions"
)

0 comments on commit acba5e6

Please sign in to comment.