Skip to content

Commit

Permalink
Change "to_json" into "to_dict"
Browse files Browse the repository at this point in the history
I changed the definition "to_json" into "to_dict" since it returns a json "friendly" dict. This change was requested in issue RiotGames#174.
  • Loading branch information
Aretusa committed Aug 7, 2018
1 parent f393823 commit 3b58c8c
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
}
)

Expand All @@ -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']))
Expand Down
8 changes: 4 additions & 4 deletions backend/cloud_inquisitor/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""

Expand All @@ -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__})

Expand Down
2 changes: 1 addition & 1 deletion backend/cloud_inquisitor/plugins/types/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions backend/cloud_inquisitor/plugins/types/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions backend/cloud_inquisitor/plugins/types/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'] = [
{
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions backend/cloud_inquisitor/plugins/views/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion backend/cloud_inquisitor/plugins/views/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion backend/cloud_inquisitor/plugins/views/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/cloud_inquisitor/plugins/views/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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()],
})

Expand Down
2 changes: 1 addition & 1 deletion backend/cloud_inquisitor/plugins/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions backend/cloud_inquisitor/plugins/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
10 changes: 5 additions & 5 deletions backend/cloud_inquisitor/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion backend/cloud_inquisitor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b58c8c

Please sign in to comment.