From 339661a6a5387b4d87ec0fe81e61f9c75a48d556 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 14 Nov 2016 13:07:00 -0500 Subject: [PATCH] New method to monkeypatch JSONField --- .../migrations/0003_v310_JSONField_changes.py | 20 +++++ awx/conf/models.py | 4 +- awx/main/fields.py | 12 ++- .../migrations/0050_v310_JSONField_changes.py | 90 +++++++++++++++++++ awx/main/models/ad_hoc_commands.py | 4 +- awx/main/models/jobs.py | 4 +- awx/main/models/mixins.py | 2 +- awx/main/models/notifications.py | 3 +- awx/main/models/projects.py | 4 +- awx/main/models/schedules.py | 4 +- awx/main/models/unified_jobs.py | 4 +- awx/main/models/workflow.py | 3 +- 12 files changed, 130 insertions(+), 24 deletions(-) create mode 100644 awx/conf/migrations/0003_v310_JSONField_changes.py create mode 100644 awx/main/migrations/0050_v310_JSONField_changes.py diff --git a/awx/conf/migrations/0003_v310_JSONField_changes.py b/awx/conf/migrations/0003_v310_JSONField_changes.py new file mode 100644 index 0000000000..78a4c02de7 --- /dev/null +++ b/awx/conf/migrations/0003_v310_JSONField_changes.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations +import awx.main.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('conf', '0002_v310_copy_tower_settings'), + ] + + operations = [ + migrations.AlterField( + model_name='setting', + name='value', + field=awx.main.fields.JSONField(null=True), + ), + ] diff --git a/awx/conf/models.py b/awx/conf/models.py index ffddab3c54..8a018cbd0b 100644 --- a/awx/conf/models.py +++ b/awx/conf/models.py @@ -7,11 +7,9 @@ import json # Django from django.db import models -# Django-JSONField -from jsonfield import JSONField - # Tower from awx.main.models.base import CreatedModifiedModel +from awx.main.fields import JSONField __all__ = ['Setting'] diff --git a/awx/main/fields.py b/awx/main/fields.py index e95dbc1ee7..cc9ff5303d 100644 --- a/awx/main/fields.py +++ b/awx/main/fields.py @@ -19,14 +19,24 @@ from django.db.models.fields.related import ( ) from django.utils.encoding import smart_text +# Django-JSONField +from jsonfield import JSONField as upstream_JSONField + # AWX from awx.main.models.rbac import batch_role_ancestor_rebuilding, Role from awx.main.utils import get_current_apps -__all__ = ['AutoOneToOneField', 'ImplicitRoleField'] +__all__ = ['AutoOneToOneField', 'ImplicitRoleField', 'JSONField'] +class JSONField(upstream_JSONField): + + def from_db_value(self, value, expression, connection, context): + if value in ['', None]: + return {} + return super(JSONField, self).from_db_value(value, expression, connection, context) + # Based on AutoOneToOneField from django-annoying: # https://bitbucket.org/offline/django-annoying/src/a0de8b294db3/annoying/fields.py diff --git a/awx/main/migrations/0050_v310_JSONField_changes.py b/awx/main/migrations/0050_v310_JSONField_changes.py new file mode 100644 index 0000000000..b2f3f2534c --- /dev/null +++ b/awx/main/migrations/0050_v310_JSONField_changes.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import awx.main.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0049_v310_workflow_surveys'), + ] + + operations = [ + migrations.AlterField( + model_name='adhoccommandevent', + name='event_data', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='job', + name='artifacts', + field=awx.main.fields.JSONField(default={}, editable=False, blank=True), + ), + migrations.AlterField( + model_name='job', + name='survey_passwords', + field=awx.main.fields.JSONField(default={}, editable=False, blank=True), + ), + migrations.AlterField( + model_name='jobevent', + name='event_data', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='jobtemplate', + name='survey_spec', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='notification', + name='body', + field=awx.main.fields.JSONField(default=dict, blank=True), + ), + migrations.AlterField( + model_name='notificationtemplate', + name='notification_configuration', + field=awx.main.fields.JSONField(default=dict), + ), + migrations.AlterField( + model_name='project', + name='playbook_files', + field=awx.main.fields.JSONField(default=[], help_text='List of playbooks found in the project', verbose_name='Playbook Files', editable=False, blank=True), + ), + migrations.AlterField( + model_name='schedule', + name='extra_data', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='unifiedjob', + name='job_env', + field=awx.main.fields.JSONField(default={}, editable=False, blank=True), + ), + migrations.AlterField( + model_name='workflowjob', + name='survey_passwords', + field=awx.main.fields.JSONField(default={}, editable=False, blank=True), + ), + migrations.AlterField( + model_name='workflowjobnode', + name='ancestor_artifacts', + field=awx.main.fields.JSONField(default={}, editable=False, blank=True), + ), + migrations.AlterField( + model_name='workflowjobnode', + name='char_prompts', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='workflowjobtemplate', + name='survey_spec', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + migrations.AlterField( + model_name='workflowjobtemplatenode', + name='char_prompts', + field=awx.main.fields.JSONField(default={}, blank=True), + ), + ] diff --git a/awx/main/models/ad_hoc_commands.py b/awx/main/models/ad_hoc_commands.py index 9cb1d96f32..bcb835763e 100644 --- a/awx/main/models/ad_hoc_commands.py +++ b/awx/main/models/ad_hoc_commands.py @@ -17,13 +17,11 @@ from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse -# Django-JSONField -from jsonfield import JSONField - # AWX from awx.main.models.base import * # noqa from awx.main.models.unified_jobs import * # noqa from awx.main.models.notifications import JobNotificationMixin +from awx.main.fields import JSONField logger = logging.getLogger('awx.main.models.ad_hoc_commands') diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 7621649889..cefc97e8eb 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -21,9 +21,6 @@ from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse -# Django-JSONField -from jsonfield import JSONField - # AWX from awx.main.constants import CLOUD_PROVIDERS from awx.main.models.base import * # noqa @@ -40,6 +37,7 @@ from awx.main.redact import PlainTextCleaner from awx.main.fields import ImplicitRoleField from awx.main.models.mixins import ResourceMixin, SurveyJobTemplateMixin, SurveyJobMixin from awx.main.models.base import PERM_INVENTORY_SCAN +from awx.main.fields import JSONField from awx.main.consumers import emit_channel_notification diff --git a/awx/main/models/mixins.py b/awx/main/models/mixins.py index efe36bcefc..8ce7025e8c 100644 --- a/awx/main/models/mixins.py +++ b/awx/main/models/mixins.py @@ -5,13 +5,13 @@ import json from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User # noqa -from jsonfield import JSONField # AWX from awx.main.models.rbac import ( Role, RoleAncestorEntry, get_roles_on_resource ) from awx.main.utils import parse_yaml_or_json +from awx.main.fields import JSONField __all__ = ['ResourceMixin', 'SurveyJobTemplateMixin', 'SurveyJobMixin'] diff --git a/awx/main/models/notifications.py b/awx/main/models/notifications.py index a9dbcdebdc..4f304fccda 100644 --- a/awx/main/models/notifications.py +++ b/awx/main/models/notifications.py @@ -18,9 +18,8 @@ from awx.main.notifications.pagerduty_backend import PagerDutyBackend from awx.main.notifications.hipchat_backend import HipChatBackend from awx.main.notifications.webhook_backend import WebhookBackend from awx.main.notifications.irc_backend import IrcBackend +from awx.main.fields import JSONField -# Django-JSONField -from jsonfield import JSONField logger = logging.getLogger('awx.main.models.notifications') diff --git a/awx/main/models/projects.py b/awx/main/models/projects.py index 378035c3ac..1ed4e418bf 100644 --- a/awx/main/models/projects.py +++ b/awx/main/models/projects.py @@ -7,9 +7,6 @@ import os import re import urlparse -# JSONField -from jsonfield import JSONField - # Django from django.conf import settings from django.db import models @@ -34,6 +31,7 @@ from awx.main.models.rbac import ( ROLE_SINGLETON_SYSTEM_ADMINISTRATOR, ROLE_SINGLETON_SYSTEM_AUDITOR, ) +from awx.main.fields import JSONField __all__ = ['Project', 'ProjectUpdate'] diff --git a/awx/main/models/schedules.py b/awx/main/models/schedules.py index 8fe83d832b..786e788aa3 100644 --- a/awx/main/models/schedules.py +++ b/awx/main/models/schedules.py @@ -11,14 +11,12 @@ from django.db import models from django.db.models.query import QuerySet from django.utils.timezone import now, make_aware, get_default_timezone -# Django-JSONField -from jsonfield import JSONField - # AWX from awx.main.models.base import * # noqa from awx.main.utils import ignore_inventory_computed_fields from awx.main.consumers import emit_channel_notification from django.core.urlresolvers import reverse +from awx.main.fields import JSONField logger = logging.getLogger('awx.main.models.schedule') diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 2c46157007..0e48491d04 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -20,9 +20,6 @@ from django.utils.timezone import now from django.utils.encoding import smart_text from django.apps import apps -# Django-JSONField -from jsonfield import JSONField - # Django-Polymorphic from polymorphic import PolymorphicModel @@ -35,6 +32,7 @@ from awx.main.models.schedules import Schedule from awx.main.utils import decrypt_field, _inventory_updates from awx.main.redact import UriCleaner, REPLACE_STR from awx.main.consumers import emit_channel_notification +from awx.main.fields import JSONField __all__ = ['UnifiedJobTemplate', 'UnifiedJob'] diff --git a/awx/main/models/workflow.py b/awx/main/models/workflow.py index 6d99be326f..4e2b6e3b90 100644 --- a/awx/main/models/workflow.py +++ b/awx/main/models/workflow.py @@ -9,8 +9,6 @@ from django.db import models from django.core.urlresolvers import reverse #from django import settings as tower_settings -from jsonfield import JSONField - # AWX from awx.main.models import UnifiedJobTemplate, UnifiedJob from awx.main.models.notifications import ( @@ -26,6 +24,7 @@ from awx.main.fields import ImplicitRoleField from awx.main.models.mixins import ResourceMixin, SurveyJobTemplateMixin, SurveyJobMixin from awx.main.redact import REPLACE_STR from awx.main.utils import parse_yaml_or_json +from awx.main.fields import JSONField from copy import copy