Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to use Django JSONField if Available #473

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 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,27 @@ 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)
break
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"
)