From 3b58c8c4381cae026bd0742be6695c92c5607263 Mon Sep 17 00:00:00 2001 From: Aretuza Date: Tue, 7 Aug 2018 16:56:36 -0700 Subject: [PATCH] Change "to_json" into "to_dict" I changed the definition "to_json" into "to_dict" since it returns a json "friendly" dict. This change was requested in issue #174. --- .../versions/cfb0ed4cced9_new_accounts_table.py | 6 +++--- backend/cloud_inquisitor/json_utils.py | 8 ++++---- backend/cloud_inquisitor/plugins/types/accounts.py | 2 +- backend/cloud_inquisitor/plugins/types/issues.py | 10 +++++----- backend/cloud_inquisitor/plugins/types/resources.py | 12 ++++++------ backend/cloud_inquisitor/plugins/views/accounts.py | 8 ++++---- backend/cloud_inquisitor/plugins/views/config.py | 2 +- backend/cloud_inquisitor/plugins/views/emails.py | 2 +- backend/cloud_inquisitor/plugins/views/metadata.py | 4 ++-- backend/cloud_inquisitor/plugins/views/search.py | 2 +- backend/cloud_inquisitor/plugins/views/users.py | 6 +++--- backend/cloud_inquisitor/schema/base.py | 10 +++++----- backend/cloud_inquisitor/utils.py | 2 +- backend/tests/service/service.py | 2 +- 14 files changed, 38 insertions(+), 38 deletions(-) diff --git a/backend/cloud_inquisitor/data/migrations/versions/cfb0ed4cced9_new_accounts_table.py b/backend/cloud_inquisitor/data/migrations/versions/cfb0ed4cced9_new_accounts_table.py index ff9faf8..cce1b19 100644 --- a/backend/cloud_inquisitor/data/migrations/versions/cfb0ed4cced9_new_accounts_table.py +++ b/backend/cloud_inquisitor/data/migrations/versions/cfb0ed4cced9_new_accounts_table.py @@ -5,7 +5,7 @@ Create Date: 2018-07-10 13:26:01.588708 """ -from json import dumps as to_json +from json import dumps as to_dict from alembic import op import sqlalchemy as sa @@ -114,7 +114,7 @@ def migrate_data(): { 'id': acct['account_id'], 'name': 'account_number', - 'value': to_json(acct['account_number']) + 'value': to_dict(acct['account_number']) } ) @@ -123,7 +123,7 @@ def migrate_data(): { 'id': acct['account_id'], 'name': 'ad_group_base', - 'value': to_json(acct['ad_group_base'] or '') + 'value': to_dict(acct['ad_group_base'] or '') } ) print('Migrated {} account {}'.format(acct['account_type'], acct['account_name'])) diff --git a/backend/cloud_inquisitor/json_utils.py b/backend/cloud_inquisitor/json_utils.py index 9341755..0b393d6 100644 --- a/backend/cloud_inquisitor/json_utils.py +++ b/backend/cloud_inquisitor/json_utils.py @@ -17,7 +17,7 @@ class InquisitorJSONEncoder(JSONEncoder): """Custom JSON encoding function. - This class will check if the object being serialized has a function called `to_json()`, and call it if available, + This class will check if the object being serialized has a function called `to_dict()`, and call it if available, as well as adding a type-hint value to the output dict (`__type` key/value pair) """ @@ -36,9 +36,9 @@ def default(self, obj): if issubclass(obj.__class__, Enum.__class__): return obj.value - to_json = getattr(obj, 'to_json', None) - if to_json: - out = obj.to_json() + to_dict = getattr(obj, 'to_dict', None) + if to_dict: + out = obj.to_dict() if issubclass(obj.__class__, Model): out.update({'__type': obj.__class__.__name__}) diff --git a/backend/cloud_inquisitor/plugins/types/accounts.py b/backend/cloud_inquisitor/plugins/types/accounts.py index b5afa0b..b1b194f 100644 --- a/backend/cloud_inquisitor/plugins/types/accounts.py +++ b/backend/cloud_inquisitor/plugins/types/accounts.py @@ -87,7 +87,7 @@ def delete(self, *, auto_commit=True): self.log.exception('Failed deleting account: {}'.format(self.id)) db.session.rollback() - def to_json(self, is_admin=False): + def to_dict(self, is_admin=False): """Returns a dict representation of the object Args: diff --git a/backend/cloud_inquisitor/plugins/types/issues.py b/backend/cloud_inquisitor/plugins/types/issues.py index c1afdc5..6dfcd56 100644 --- a/backend/cloud_inquisitor/plugins/types/issues.py +++ b/backend/cloud_inquisitor/plugins/types/issues.py @@ -84,7 +84,7 @@ def delete(self, *, auto_commit=True): self.log.exception('Failed deleting issue: {}'.format(self.id)) db.session.rollback() - def to_json(self): + def to_dict(self): return { 'issueType': self.issue.issue_type_id, 'issueId': self.id, @@ -399,8 +399,8 @@ def state_name(self): else: raise ValueError('Invalid state: {}'.format(self.state)) - def to_json(self): - data = super().to_json() + def to_dict(self): + data = super().to_dict() data['resource'] = self.resource return data @@ -588,8 +588,8 @@ def update(self, data): return updated - def to_json(self): - data = super().to_json() + def to_dict(self): + data = super().to_dict() data['volume'] = self.volume return data diff --git a/backend/cloud_inquisitor/plugins/types/resources.py b/backend/cloud_inquisitor/plugins/types/resources.py index e0cd965..87457e8 100644 --- a/backend/cloud_inquisitor/plugins/types/resources.py +++ b/backend/cloud_inquisitor/plugins/types/resources.py @@ -480,7 +480,7 @@ def delete(self, *, auto_commit=False): self.log.exception('Failed deleting resource: {}'.format(self.id)) db.session.rollback() - def to_json(self): + def to_dict(self): """Return a `dict` representation of the resource, including all properties and tags Returns: @@ -671,13 +671,13 @@ def search_by_age(cls, *, limit=100, page=1, accounts=None, locations=None, age= return total, [cls(x) for x in qry.all()] - def to_json(self, with_volumes=True): - """Augment the base `to_json` function, adding information about volumes + def to_dict(self, with_volumes=True): + """Augment the base `to_dict` function, adding information about volumes Returns: `dict` """ - data = super().to_json() + data = super().to_dict() if with_volumes: data['volumes'] = [ { @@ -1219,8 +1219,8 @@ def update(self, data): return updated - def to_json(self, with_records=True): - data = super().to_json() + def to_dict(self, with_records=True): + data = super().to_dict() if with_records: data['recordCount'] = len(self.records) data['records'] = self.records diff --git a/backend/cloud_inquisitor/plugins/views/accounts.py b/backend/cloud_inquisitor/plugins/views/accounts.py index 03f87ac..0317bed 100644 --- a/backend/cloud_inquisitor/plugins/views/accounts.py +++ b/backend/cloud_inquisitor/plugins/views/accounts.py @@ -50,7 +50,7 @@ def get(self): if accounts: return self.make_response({ 'message': None, - 'accounts': [x.to_json(is_admin=ROLE_ADMIN in session['user'].roles or False) for x in accounts] + 'accounts': [x.to_dict(is_admin=ROLE_ADMIN in session['user'].roles or False) for x in accounts] }) else: return self.make_response({ @@ -120,7 +120,7 @@ def get(self, accountId): if account: return self.make_response({ 'message': None, - 'account': account.to_json(is_admin=True) + 'account': account.to_dict(is_admin=True) }) else: return self.make_response({ @@ -170,7 +170,7 @@ def put(self, accountId): auditlog(event='account.update', actor=session['user'].username, data=args) - return self.make_response({'message': 'Object updated', 'account': account.to_json(is_admin=True)}) + return self.make_response({'message': 'Object updated', 'account': account.to_dict(is_admin=True)}) @rollback @check_auth(ROLE_ADMIN) @@ -192,7 +192,7 @@ class AccountImportExport(BaseView): @rollback @check_auth(ROLE_ADMIN) def get(self): - out = [ns.to_json(is_admin=True) for ns in db.Account.find()] + out = [ns.to_dict(is_admin=True) for ns in db.Account.find()] auditlog(event='account.export', actor=session['user'].username, data={}) return Response( diff --git a/backend/cloud_inquisitor/plugins/views/config.py b/backend/cloud_inquisitor/plugins/views/config.py index 31641d8..c8a46a4 100644 --- a/backend/cloud_inquisitor/plugins/views/config.py +++ b/backend/cloud_inquisitor/plugins/views/config.py @@ -257,7 +257,7 @@ class ConfigImportExport(BaseView): @rollback @check_auth(ROLE_ADMIN) def get(self): - out = [ns.to_json() for ns in db.ConfigNamespace.find()] + out = [ns.to_dict() for ns in db.ConfigNamespace.find()] auditlog(event='config.export', actor=session['user'].username, data={}) return Response( diff --git a/backend/cloud_inquisitor/plugins/views/emails.py b/backend/cloud_inquisitor/plugins/views/emails.py index f0a829e..01f98fe 100644 --- a/backend/cloud_inquisitor/plugins/views/emails.py +++ b/backend/cloud_inquisitor/plugins/views/emails.py @@ -75,7 +75,7 @@ def get(self, emailId): }, HTTP.NOT_FOUND) return self.make_response({ - 'email': email.to_json(True) + 'email': email.to_dict(True) }) @rollback diff --git a/backend/cloud_inquisitor/plugins/views/metadata.py b/backend/cloud_inquisitor/plugins/views/metadata.py index a358c7b..e7348de 100644 --- a/backend/cloud_inquisitor/plugins/views/metadata.py +++ b/backend/cloud_inquisitor/plugins/views/metadata.py @@ -17,7 +17,7 @@ class MetaData(BaseView): @check_auth(ROLE_USER) def get(self): _, accts = BaseAccount.search(account_ids=session['accounts']) - accounts = [acct.to_json(is_admin=ROLE_ADMIN in session['user'].roles) for acct in accts] + accounts = [acct.to_dict(is_admin=ROLE_ADMIN in session['user'].roles) for acct in accts] account_types = list(self.__get_account_types()) menu_items = {} @@ -37,7 +37,7 @@ def get(self): 'menuItems': menu_items, 'accountTypes': account_types, 'resourceTypes': {v.resource_name: k for k, v in current_app.types.items()}, - 'currentUser': session['user'].to_json(), + 'currentUser': session['user'].to_dict(), 'notifiers': [{'type': k, 'validation': v} for k, v in current_app.notifiers.items()], }) diff --git a/backend/cloud_inquisitor/plugins/views/search.py b/backend/cloud_inquisitor/plugins/views/search.py index 24114bd..7e57590 100644 --- a/backend/cloud_inquisitor/plugins/views/search.py +++ b/backend/cloud_inquisitor/plugins/views/search.py @@ -151,7 +151,7 @@ def get(self): results = [] for resource in qry.all(): cls = current_app.types[resource.resource_type_id] - data = cls(resource).to_json() + data = cls(resource).to_dict() results.append(data) return self.make_response({ diff --git a/backend/cloud_inquisitor/plugins/views/users.py b/backend/cloud_inquisitor/plugins/views/users.py index 80f4929..87b4786 100644 --- a/backend/cloud_inquisitor/plugins/views/users.py +++ b/backend/cloud_inquisitor/plugins/views/users.py @@ -48,7 +48,7 @@ def get(self): users = qry.all() return self.make_response({ - 'users': [x.to_json() for x in users], + 'users': [x.to_dict() for x in users], 'userCount': total, 'authSystems': list(current_app.available_auth_systems.keys()), 'activeAuthSystem': current_app.active_auth_system.name @@ -146,7 +146,7 @@ def get(self, user_id): return self.make_response('Unable to find the user requested, might have been removed', HTTP.NOT_FOUND) return self.make_response({ - 'user': user.to_json(), + 'user': user.to_dict(), 'roles': roles }, HTTP.OK) @@ -234,6 +234,6 @@ def put(self, user_id): db.session.commit() return self.make_response({ - 'user': user.to_json(), + 'user': user.to_dict(), 'newPassword': new_pass if not args['password'] else None }, HTTP.OK) diff --git a/backend/cloud_inquisitor/schema/base.py b/backend/cloud_inquisitor/schema/base.py index aac3410..271b29d 100644 --- a/backend/cloud_inquisitor/schema/base.py +++ b/backend/cloud_inquisitor/schema/base.py @@ -34,7 +34,7 @@ class BaseModelMixin(object): def __tablename__(cls): return cls.__name__.lower() - def to_json(self): + def to_dict(self): """Exports the object to a JSON friendly dict Returns: @@ -53,11 +53,11 @@ def to_json(self): if issubclass(type(attr), QueryableAttribute): # List of Model, BaseModelMixin objects (one-to-many relationship) if issubclass(value_class, InstrumentedList): - output[to_camelcase(attrName)] = [x.to_json() for x in value] + output[to_camelcase(attrName)] = [x.to_dict() for x in value] # Model, BaseModelMixin object (one-to-one relationship) elif issubclass(value_class, Model): - output[to_camelcase(attrName)] = value.to_json() + output[to_camelcase(attrName)] = value.to_dict() # Datetime object elif isinstance(value, datetime): @@ -132,7 +132,7 @@ class Email(Model, BaseModelMixin): message_html = Column(Text) message_text = Column(Text) - def to_json(self, include_body=False): + def to_dict(self, include_body=False): """Exports the object to a JSON friendly dict Args: @@ -367,7 +367,7 @@ def from_json(cls, data): return user - def to_json(self): + def to_dict(self): """Exports the object to a JSON friendly dict Returns: diff --git a/backend/cloud_inquisitor/utils.py b/backend/cloud_inquisitor/utils.py index 8a88020..0771750 100644 --- a/backend/cloud_inquisitor/utils.py +++ b/backend/cloud_inquisitor/utils.py @@ -44,7 +44,7 @@ def __init__(self, group=None, name=None, state=None, active=None, section=None, self.args = args or {} self.order = order - def to_json(self): + def to_dict(self): return { 'group': self.group, 'name': self.name, diff --git a/backend/tests/service/service.py b/backend/tests/service/service.py index b2ea2b1..3521835 100644 --- a/backend/tests/service/service.py +++ b/backend/tests/service/service.py @@ -14,7 +14,7 @@ class CinqTestService(object): def __init__(self): - self.default_dbconfig = [ns.to_json() for ns in db.ConfigNamespace.find()] + self.default_dbconfig = [ns.to_dict() for ns in db.ConfigNamespace.find()] self.scheduler = None self.api_server = None