diff --git a/rhodecode/apps/admin/views/users.py b/rhodecode/apps/admin/views/users.py --- a/rhodecode/apps/admin/views/users.py +++ b/rhodecode/apps/admin/views/users.py @@ -40,7 +40,7 @@ from rhodecode.lib.exceptions import ( UserCreationError, UserOwnsReposException, UserOwnsRepoGroupsException, UserOwnsUserGroupsException, UserOwnsPullRequestsException, UserOwnsArtifactsException, DefaultUserException) -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib.auth import ( LoginRequired, HasPermissionAllDecorator, CSRFRequired) from rhodecode.lib import helpers as h @@ -1169,7 +1169,7 @@ class UsersView(UserAppView): groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) for group in c.user.group_member] - c.groups = json.dumps(groups) + c.groups = ext_json.str_json(groups) c.active = 'groups' return self._get_template_context(c) @@ -1258,9 +1258,8 @@ class UsersView(UserAppView): for entry in user_log: audit_log_data[entry.user_log_id] = entry.get_dict() - response = Response(json.dumps(audit_log_data, indent=4)) - response.content_disposition = str( - 'attachment; filename=%s' % 'user_{}_audit_logs.json'.format(c.user.user_id)) + response = Response(ext_json.formatted_str_json(audit_log_data)) + response.content_disposition = f'attachment; filename=user_{c.user.user_id}_audit_logs.json' response.content_type = 'application/json' return response diff --git a/rhodecode/apps/channelstream/__init__.py b/rhodecode/apps/channelstream/__init__.py --- a/rhodecode/apps/channelstream/__init__.py +++ b/rhodecode/apps/channelstream/__init__.py @@ -25,6 +25,7 @@ from pyramid.settings import asbool from rhodecode.apps._base import ADMIN_PREFIX from rhodecode.lib.ext_json import json +from rhodecode.lib.str_utils import safe_str def url_gen(request): @@ -38,7 +39,7 @@ def url_gen(request): 'longpoll': longpoll_url or proxy_url, 'ws': ws_url or proxy_url.replace('http', 'ws') } - return json.dumps(urls) + return safe_str(json.dumps(urls)) PLUGIN_DEFINITION = { diff --git a/rhodecode/apps/gist/views.py b/rhodecode/apps/gist/views.py --- a/rhodecode/apps/gist/views.py +++ b/rhodecode/apps/gist/views.py @@ -30,7 +30,7 @@ from pyramid.renderers import render from pyramid.response import Response from rhodecode.apps._base import BaseAppView -from rhodecode.lib import helpers as h +from rhodecode.lib import helpers as h, ext_json from rhodecode.lib.auth import LoginRequired, NotAnonymous, CSRFRequired from rhodecode.lib.utils2 import time_to_datetime from rhodecode.lib.ext_json import json @@ -124,7 +124,7 @@ class GistView(BaseAppView): 'expires': _render('gist_expires', gist.gist_expires), 'description': _render('gist_description', gist.gist_description) }) - c.data = json.dumps(data) + c.data = ext_json.str_json(data) return self._get_template_context(c) diff --git a/rhodecode/apps/my_account/views/my_account.py b/rhodecode/apps/my_account/views/my_account.py --- a/rhodecode/apps/my_account/views/my_account.py +++ b/rhodecode/apps/my_account/views/my_account.py @@ -31,7 +31,7 @@ from rhodecode.apps._base import BaseApp from rhodecode import forms from rhodecode.lib import helpers as h from rhodecode.lib import audit_logger -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib.auth import ( LoginRequired, NotAnonymous, CSRFRequired, HasRepoPermissionAny, HasRepoGroupPermissionAny, AuthUser) @@ -457,7 +457,7 @@ class MyAccountView(BaseAppView, DataGri repos_data.append(row) # json used to render the grid - return json.dumps(repos_data) + return ext_json.str_json(repos_data) @LoginRequired() @NotAnonymous() @@ -779,5 +779,5 @@ class MyAccountView(BaseAppView, DataGri c.active = 'user_group_membership' groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) for group in self._rhodecode_db_user.group_member] - c.user_groups = json.dumps(groups) + c.user_groups = ext_json.str_json(groups) return self._get_template_context(c) diff --git a/rhodecode/apps/repository/views/repo_bookmarks.py b/rhodecode/apps/repository/views/repo_bookmarks.py --- a/rhodecode/apps/repository/views/repo_bookmarks.py +++ b/rhodecode/apps/repository/views/repo_bookmarks.py @@ -21,9 +21,8 @@ import logging from pyramid.httpexceptions import HTTPNotFound - from rhodecode.apps._base import BaseReferencesView -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib import helpers as h from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator) @@ -47,5 +46,5 @@ class RepoBookmarksView(BaseReferencesVi ref_items=ref_items, partials_template='bookmarks/bookmarks_data.mako') c.has_references = bool(data) - c.data = json.dumps(data) + c.data = ext_json.str_json(data) return self._get_template_context(c) diff --git a/rhodecode/apps/repository/views/repo_branches.py b/rhodecode/apps/repository/views/repo_branches.py --- a/rhodecode/apps/repository/views/repo_branches.py +++ b/rhodecode/apps/repository/views/repo_branches.py @@ -22,7 +22,7 @@ import logging from rhodecode.apps._base import BaseReferencesView -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator) @@ -42,5 +42,5 @@ class RepoBranchesView(BaseReferencesVie ref_items=ref_items, partials_template='branches/branches_data.mako') c.has_references = bool(data) - c.data = json.dumps(data) + c.data = ext_json.str_json(data) return self._get_template_context(c) diff --git a/rhodecode/apps/repository/views/repo_changelog.py b/rhodecode/apps/repository/views/repo_changelog.py --- a/rhodecode/apps/repository/views/repo_changelog.py +++ b/rhodecode/apps/repository/views/repo_changelog.py @@ -28,6 +28,7 @@ from pyramid.response import Response from rhodecode.apps._base import RepoAppView import rhodecode.lib.helpers as h +from rhodecode.lib import ext_json from rhodecode.lib.auth import ( LoginRequired, HasRepoPermissionAnyDecorator) @@ -106,7 +107,7 @@ class RepoChangelogView(RepoAppView): data = [[commit_id, vtx, edges, branch] for commit_id, vtx, edges, branch in _colored(dag)] - return json.dumps(data), json.dumps(current) + return ext_json.str_json(data), ext_json.str_json(current) def _check_if_valid_branch(self, branch_name, repo_name, f_path): if branch_name not in self.rhodecode_vcs_repo.branches_all: diff --git a/rhodecode/apps/repository/views/repo_commits.py b/rhodecode/apps/repository/views/repo_commits.py --- a/rhodecode/apps/repository/views/repo_commits.py +++ b/rhodecode/apps/repository/views/repo_commits.py @@ -33,7 +33,7 @@ from rhodecode.apps.file_store.exception from rhodecode.lib import diffs, codeblocks, channelstream from rhodecode.lib.auth import ( LoginRequired, HasRepoPermissionAnyDecorator, NotAnonymous, CSRFRequired) -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from collections import OrderedDict from rhodecode.lib.diffs import ( cache_diff, load_cached_diff, diff_cache_exist, get_diff_context, @@ -218,7 +218,7 @@ class RepoCommitsView(RepoAppView): member_reviewer['allowed_to_update'] = False c.commit_set_reviewers_data_json['reviewers'].append(member_reviewer) - c.commit_set_reviewers_data_json = json.dumps(c.commit_set_reviewers_data_json) + c.commit_set_reviewers_data_json = ext_json.str_json(c.commit_set_reviewers_data_json) # NOTE(marcink): this uses the same voting logic as in pull-requests c.commit_review_status = ChangesetStatusModel().calculate_status(review_statuses) diff --git a/rhodecode/apps/repository/views/repo_pull_requests.py b/rhodecode/apps/repository/views/repo_pull_requests.py --- a/rhodecode/apps/repository/views/repo_pull_requests.py +++ b/rhodecode/apps/repository/views/repo_pull_requests.py @@ -35,7 +35,7 @@ from rhodecode.lib import helpers as h, from rhodecode.lib.base import vcs_operation_context from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist from rhodecode.lib.exceptions import CommentVersionMismatch -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib.auth import ( LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator, NotAnonymous, CSRFRequired) @@ -468,7 +468,7 @@ class RepoPullRequestsView(RepoAppView, c.observers_count = pull_request.observers_count # reviewers and statuses - c.pull_request_default_reviewers_data_json = json.dumps(pull_request.reviewer_data) + c.pull_request_default_reviewers_data_json = ext_json.str_json(pull_request.reviewer_data) c.pull_request_set_reviewers_data_json = collections.OrderedDict({'reviewers': []}) c.pull_request_set_observers_data_json = collections.OrderedDict({'observers': []}) @@ -485,7 +485,7 @@ class RepoPullRequestsView(RepoAppView, member_reviewer['allowed_to_update'] = c.allowed_to_update c.pull_request_set_reviewers_data_json['reviewers'].append(member_reviewer) - c.pull_request_set_reviewers_data_json = json.dumps(c.pull_request_set_reviewers_data_json) + c.pull_request_set_reviewers_data_json = ext_json.str_json(c.pull_request_set_reviewers_data_json) for observer_obj, member in pull_request_at_ver.observers(): member_observer = h.reviewer_as_json( @@ -496,7 +496,7 @@ class RepoPullRequestsView(RepoAppView, member_observer['allowed_to_update'] = c.allowed_to_update c.pull_request_set_observers_data_json['observers'].append(member_observer) - c.pull_request_set_observers_data_json = json.dumps(c.pull_request_set_observers_data_json) + c.pull_request_set_observers_data_json = ext_json.str_json(c.pull_request_set_observers_data_json) general_comments, inline_comments = \ self.register_comments_vars(c, pull_request_latest, versions) @@ -889,9 +889,9 @@ class RepoPullRequestsView(RepoAppView, c.default_repo_data = { 'source_repo_name': source_repo.repo_name, - 'source_refs_json': json.dumps(source_repo_data), + 'source_refs_json': ext_json.str_json(source_repo_data), 'target_repo_name': default_target_repo.repo_name, - 'target_refs_json': json.dumps(target_repo_data), + 'target_refs_json': ext_json.str_json(target_repo_data), } c.default_source_ref = selected_source_ref diff --git a/rhodecode/apps/repository/views/repo_tags.py b/rhodecode/apps/repository/views/repo_tags.py --- a/rhodecode/apps/repository/views/repo_tags.py +++ b/rhodecode/apps/repository/views/repo_tags.py @@ -21,7 +21,7 @@ import logging from rhodecode.apps._base import BaseReferencesView -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator) log = logging.getLogger(__name__) @@ -40,5 +40,5 @@ class RepoTagsView(BaseReferencesView): ref_items=ref_items, partials_template='tags/tags_data.mako') c.has_references = bool(data) - c.data = json.dumps(data) + c.data = ext_json.str_json(data) return self._get_template_context(c) diff --git a/rhodecode/lib/ext_json_renderer.py b/rhodecode/lib/ext_json_renderer.py --- a/rhodecode/lib/ext_json_renderer.py +++ b/rhodecode/lib/ext_json_renderer.py @@ -18,7 +18,7 @@ # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ -from rhodecode.lib.ext_json import json +from rhodecode.lib import ext_json def pyramid_ext_json(info): @@ -34,7 +34,8 @@ def pyramid_ext_json(info): if ct == response.default_content_type: response.content_type = 'application/json' indent = getattr(request, 'ext_json_indent', None) - - return json.dumps(value, indent=indent) + if indent: + return ext_json.formatted_json(value) + return ext_json.json.dumps(value) return _render diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -78,7 +78,9 @@ from webhelpers2.number import format_by from rhodecode.lib.action_parser import action_parser from rhodecode.lib.pagination import Page, RepoPage, SqlPage +from rhodecode.lib import ext_json from rhodecode.lib.ext_json import json +from rhodecode.lib.str_utils import safe_bytes from rhodecode.lib.utils import repo_name_slug, get_custom_lexer from rhodecode.lib.utils2 import ( str2bool, safe_unicode, safe_str, @@ -135,6 +137,14 @@ def html_escape(text, html_escape_table= return text.translate(html_escape_table) +def str_json(*args, **kwargs): + return ext_json.str_json(*args, **kwargs) + + +def formatted_str_json(*args, **kwargs): + return ext_json.formatted_str_json(*args, **kwargs) + + def chop_at_smart(s, sub, inclusive=False, suffix_if_chopped=None): """ Truncate string ``s`` at the first occurrence of ``sub``. @@ -346,7 +356,7 @@ def files_url_data(request): if 'commit_id' not in matchdict: matchdict['commit_id'] = 'tip' - return json.dumps(matchdict) + return ext_json.str_json(matchdict) def repo_files_by_ref_url(db_repo_name, db_repo_type, f_path, ref_name, commit_id, query=None, ): @@ -768,7 +778,7 @@ class Flash(object): 'subdata': message.sub_data } }) - return json.dumps(payloads) + return safe_str(json.dumps(payloads)) def __call__(self, message, category=None, ignore_duplicate=True, session=None, request=None): diff --git a/rhodecode/subscribers.py b/rhodecode/subscribers.py --- a/rhodecode/subscribers.py +++ b/rhodecode/subscribers.py @@ -251,7 +251,7 @@ def write_usage_data(event): ) with open(dest_file, 'wb') as f: - f.write(ext_json.json.dumps(metadata, indent=2, sort_keys=True)) + f.write(ext_json.formatted_json(metadata)) try: log.debug('Writing usage file at: %s', usage_metadata_destination) diff --git a/rhodecode/templates/admin/admin_audit_log_entry.mako b/rhodecode/templates/admin/admin_audit_log_entry.mako --- a/rhodecode/templates/admin/admin_audit_log_entry.mako +++ b/rhodecode/templates/admin/admin_audit_log_entry.mako @@ -83,7 +83,7 @@
${h.json.dumps(c.audit_log_entry.action_data, indent=4, sort_keys=True)}+
${h.formatted_str_json(c.audit_log_entry.action_data)}
-diff --git a/rhodecode/templates/admin/admin_log_base.mako b/rhodecode/templates/admin/admin_log_base.mako --- a/rhodecode/templates/admin/admin_log_base.mako +++ b/rhodecode/templates/admin/admin_log_base.mako @@ -41,7 +41,7 @@ % if l.version == l.VERSION_2: ${_('toggle')} % else:
-diff --git a/rhodecode/templates/base/issue_tracker_settings.mako b/rhodecode/templates/base/issue_tracker_settings.mako --- a/rhodecode/templates/base/issue_tracker_settings.mako +++ b/rhodecode/templates/base/issue_tracker_settings.mako @@ -92,7 +92,7 @@ examples = [ % if loop.index == 0: ${_('show examples')} % else: - copy to input + copy to input % endif