def test_dumps_unserializable_class(): unserializable_obj = object() with pytest.raises(TypeError) as excinfo: json.dumps(unserializable_obj) assert repr(unserializable_obj) in str(excinfo.value) assert 'is not JSON serializable' in str(excinfo.value)
def index(self): source_repo = c.rhodecode_db_repo try: source_repo.scm_instance().get_commit() except EmptyRepositoryError: h.flash(h.literal(_('There are no commits yet')), category='warning') redirect(url('summary_home', repo_name=source_repo.repo_name)) commit_id = request.GET.get('commit') branch_ref = request.GET.get('branch') bookmark_ref = request.GET.get('bookmark') try: source_repo_data = PullRequestModel().generate_repo_data( source_repo, commit_id=commit_id, branch=branch_ref, bookmark=bookmark_ref) except CommitDoesNotExistError as e: log.exception(e) h.flash(_('Commit does not exist'), 'error') redirect(url('pullrequest_home', repo_name=source_repo.repo_name)) default_target_repo = source_repo if (source_repo.parent and not source_repo.parent.scm_instance().is_empty()): # change default if we have a parent repo default_target_repo = source_repo.parent target_repo_data = PullRequestModel().generate_repo_data( default_target_repo) selected_source_ref = source_repo_data['refs']['selected_ref'] title_source_ref = selected_source_ref.split(':', 2)[1] c.default_title = PullRequestModel().generate_pullrequest_title( source=source_repo.repo_name, source_ref=title_source_ref, target=default_target_repo.repo_name) c.default_repo_data = { 'source_repo_name': source_repo.repo_name, 'source_refs_json': json.dumps(source_repo_data), 'target_repo_name': default_target_repo.repo_name, 'target_refs_json': json.dumps(target_repo_data), } c.default_source_ref = selected_source_ref return render('/pullrequests/pullrequest.html')
def _graph(self, repo, commits): """ Generates a DAG graph for repo :param repo: repo instance :param commits: list of commits """ if not commits: c.jsdata = json.dumps([]) return dag = _dagwalker(repo, commits) data = [['', vtx, edges] for vtx, edges in _colored(dag)] c.jsdata = json.dumps(data)
def show(self, group_name, format='html'): """GET /repos_groups/group_name: Show a specific item""" # url('repos_group', group_name=GROUP_NAME) c.group = c.repos_group = ReposGroupModel()._get_repos_group(group_name) c.group_repos = c.group.repositories.all() #overwrite our cached list with current filter gr_filter = c.group_repos c.repo_cnt = 0 groups = RepoGroup.query().order_by(RepoGroup.group_name)\ .filter(RepoGroup.group_parent_id == c.group.group_id).all() c.groups = self.scm_model.get_repos_groups(groups) if not c.visual.lightweight_dashboard: c.repos_list = self.scm_model.get_repos(all_repos=gr_filter) ## lightweight version of dashboard else: c.repos_list = Repository.query()\ .filter(Repository.group_id == c.group.group_id)\ .order_by(func.lower(Repository.repo_name))\ .all() repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, admin=False) #json used to render the grid c.data = json.dumps(repos_data) return render('admin/repos_groups/repos_groups.html')
def show(self, group_name, format='html'): """GET /repos_groups/group_name: Show a specific item""" # url('repos_group', group_name=GROUP_NAME) c.group = c.repos_group = ReposGroupModel()._get_repos_group( group_name) c.group_repos = c.group.repositories.all() #overwrite our cached list with current filter gr_filter = c.group_repos c.repo_cnt = 0 groups = RepoGroup.query().order_by(RepoGroup.group_name)\ .filter(RepoGroup.group_parent_id == c.group.group_id).all() c.groups = self.scm_model.get_repos_groups(groups) if not c.visual.lightweight_dashboard: c.repos_list = self.scm_model.get_repos(all_repos=gr_filter) ## lightweight version of dashboard else: c.repos_list = Repository.query()\ .filter(Repository.group_id == c.group.group_id)\ .order_by(func.lower(Repository.repo_name))\ .all() repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, admin=False) #json used to render the grid c.data = json.dumps(repos_data) return render('admin/repos_groups/repos_groups.html')
def user_auth(self, username, password): """Authenticate a user against crowd. Returns brief information about the user.""" url = ("%s/rest/usermanagement/%s/authentication?username=%s" % (self._uri, self._version, username)) body = json.dumps({"value": password}) return self._request(url, body)
def test_api_lock_repo_lock_optional_locked(self, backend): # TODO: Provide a fixture locked_repository or similar repo = Repository.get_by_repo_name(backend.repo_name) user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) Repository.lock(repo, user.user_id, lock_reason=Repository.LOCK_API) id_, params = build_data(self.apikey, 'lock', repoid=backend.repo_name) response = api_call(self.app, params) time_ = response.json['result']['locked_since'] expected = { 'repo': backend.repo_name, 'locked': True, 'locked_since': time_, 'locked_by': TEST_USER_ADMIN_LOGIN, 'lock_state_changed': False, 'lock_reason': Repository.LOCK_API, 'msg': ('Repo `%s` locked by `%s` on `%s`.' % (backend.repo_name, TEST_USER_ADMIN_LOGIN, json.dumps(time_to_datetime(time_)))) } assert_ok(id_, expected, given=response.body)
def index(self): c.groups = self.scm_model.get_repos_groups() c.group = None if c.visual.lightweight_dashboard is False: c.repos_list = self.scm_model.get_repos() ## lightweight version of dashboard else: c.repos_list = Repository.query()\ .filter(Repository.group_id == None)\ .order_by(func.lower(Repository.repo_name))\ .all() repos_data = [] total_records = len(c.repos_list) _tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') quick_menu = lambda repo_name: (template.get_def("quick_menu") .render(repo_name, _=_, h=h, c=c)) repo_lnk = lambda name, rtype, private, fork_of: ( template.get_def("repo_name") .render(name, rtype, private, fork_of, short_name=False, admin=False, _=_, h=h, c=c)) last_change = lambda last_change: (template.get_def("last_change") .render(last_change, _=_, h=h, c=c)) rss_lnk = lambda repo_name: (template.get_def("rss") .render(repo_name, _=_, h=h, c=c)) atom_lnk = lambda repo_name: (template.get_def("atom") .render(repo_name, _=_, h=h, c=c)) def desc(desc): if c.visual.stylify_metatags: return h.urlify_text(h.desc_stylize(h.truncate(desc, 60))) else: return h.urlify_text(h.truncate(desc, 60)) for repo in c.repos_list: repos_data.append({ "menu": quick_menu(repo.repo_name), "raw_name": repo.repo_name.lower(), "name": repo_lnk(repo.repo_name, repo.repo_type, repo.private, repo.fork), "last_change": last_change(repo.last_db_change), "desc": desc(repo.description), "owner": h.person(repo.user.username), "rss": rss_lnk(repo.repo_name), "atom": atom_lnk(repo.repo_name), }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": "name", "dir": "asc", "records": repos_data }) return render('/index.html')
def index(self): """GET /repo_groups: All items in the collection""" # url('repo_groups') repo_group_list = RepoGroup.get_all_repo_groups() _perms = ['group.admin'] repo_group_list_acl = RepoGroupList(repo_group_list, perm_set=_perms) repo_group_data = RepoGroupModel().get_repo_groups_as_dict( repo_group_list=repo_group_list_acl, admin=True) c.data = json.dumps(repo_group_data) return render('admin/repo_groups/repo_groups.html')
def test_dump_is_like_dumps(): data = { 'decimal': decimal.Decimal('1.5'), 'set': set([1]), # Just one element to guarantee the order 'complex': 1 - 1j, 'datetime': datetime.datetime(1969, 7, 20, 3, 14, 15, 926535), 'time': datetime.time(3, 14, 15, 926535), 'date': datetime.date(1969, 7, 20), } json_buffer = io.BytesIO() json.dump(data, json_buffer) assert json.dumps(data) == json_buffer.getvalue()
def index(self, format='html'): """GET /repos: All items in the collection""" # url('repos') repo_list = Repository.get_all_repos() c.repo_list = RepoList(repo_list, perm_set=['repository.admin']) repos_data = RepoModel().get_repos_as_dict(repo_list=c.repo_list, admin=True, super_user_actions=True) # json used to render the grid c.data = json.dumps(repos_data) return render('admin/repos/repos.html')
def build_data(apikey, method, **kw): """ Builds API data with given random ID :param random_id: """ random_id = random.randrange(1, 9999) return random_id, json.dumps({ "id": random_id, "api_key": apikey, "method": method, "args": kw })
def show_all(self, repo_name): # filter types c.active = 'open' c.source = str2bool(request.GET.get('source')) c.closed = str2bool(request.GET.get('closed')) c.my = str2bool(request.GET.get('my')) c.awaiting_review = str2bool(request.GET.get('awaiting_review')) c.awaiting_my_review = str2bool(request.GET.get('awaiting_my_review')) c.repo_name = repo_name opened_by = None if c.my: c.active = 'my' opened_by = [c.rhodecode_user.user_id] statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN] if c.closed: c.active = 'closed' statuses = [PullRequest.STATUS_CLOSED] if c.awaiting_review and not c.source: c.active = 'awaiting' if c.source and not c.awaiting_review: c.active = 'source' if c.awaiting_my_review: c.active = 'awaiting_my' data = self._get_pull_requests_list(repo_name=repo_name, opened_by=opened_by, statuses=statuses) if not request.is_xhr: c.data = json.dumps(data['data']) c.records_total = data['recordsTotal'] return render('/pullrequests/pullrequests.html') else: return json.dumps(data)
def api_call(apikey, apihost, format, method=None, **kw): """ Api_call wrapper for RhodeCode :param apikey: :param apihost: :param format: formatting, pretty means prints and pprint of json json returns unparsed json :param method: """ def _build_data(random_id): """ Builds API data with given random ID :param random_id: :type random_id: """ return { "id": random_id, "api_key": apikey, "method": method, "args": kw } if not method: raise Exception('please specify method name !') id_ = random.randrange(1, 9999) req = urllib2.Request('%s/_admin/api' % apihost, data=json.dumps(_build_data(id_)), headers={'content-type': 'text/plain'}) if format == FORMAT_PRETTY: sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost)) ret = urllib2.urlopen(req) raw_json = ret.read() json_data = json.loads(raw_json) id_ret = json_data['id'] _formatted_json = pprint.pformat(json_data) if id_ret == id_: if format == FORMAT_JSON: sys.stdout.write(str(raw_json)) else: sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json)) else: raise Exception('something went wrong. ' 'ID mismatch got %s, expected %s | %s' % ( id_ret, id_, _formatted_json))
def api_call(apikey, apihost, format, method=None, **kw): """ Api_call wrapper for RhodeCode :param apikey: :param apihost: :param format: formatting, pretty means prints and pprint of json json returns unparsed json :param method: """ def _build_data(random_id): """ Builds API data with given random ID :param random_id: :type random_id: """ return { "id": random_id, "api_key": apikey, "method": method, "args": kw } if not method: raise Exception('please specify method name !') id_ = random.randrange(1, 9999) req = urllib2.Request('%s/_admin/api' % apihost, data=json.dumps(_build_data(id_)), headers={'content-type': 'text/plain'}) if format == FORMAT_PRETTY: sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost)) ret = urllib2.urlopen(req) raw_json = ret.read() json_data = json.loads(raw_json) id_ret = json_data['id'] _formatted_json = pprint.pformat(json_data) if id_ret == id_: if format == FORMAT_JSON: sys.stdout.write(str(raw_json)) else: sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json)) else: raise Exception('something went wrong. ' 'ID mismatch got %s, expected %s | %s' % (id_ret, id_, _formatted_json))
def api_call(apikey, apihost, method=None, **kw): """ Api_call wrapper for RhodeCode. :param apikey: :param apihost: :param format: formatting, pretty means prints and pprint of json json returns unparsed json :param method: :returns: json response from server """ def _build_data(random_id): """ Builds API data with given random ID :param random_id: """ return { "id": random_id, "api_key": apikey, "method": method, "args": kw } if not method: raise Exception('please specify method name !') id_ = random.randrange(1, 9999) req = urllib2.Request('%s/_admin/api' % apihost, data=json.dumps(_build_data(id_)), headers={'content-type': 'text/plain'}) ret = urllib2.urlopen(req) raw_json = ret.read() json_data = json.loads(raw_json) id_ret = json_data['id'] if id_ret == id_: return json_data else: _formatted_json = pprint.pformat(json_data) raise Exception('something went wrong. ' 'ID mismatch got %s, expected %s | %s' % (id_ret, id_, _formatted_json))
def index(self): c.groups = self.scm_model.get_repos_groups() c.group = None if not c.visual.lightweight_dashboard: c.repos_list = self.scm_model.get_repos() ## lightweight version of dashboard else: c.repos_list = Repository.query()\ .filter(Repository.group_id == None)\ .order_by(func.lower(Repository.repo_name))\ .all() repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, admin=False) #json used to render the grid c.data = json.dumps(repos_data) return render('/index.html')
def api_call(apikey, apihost, method=None, **kw): """ Api_call wrapper for RhodeCode. :param apikey: :param apihost: :param format: formatting, pretty means prints and pprint of json json returns unparsed json :param method: :returns: json response from server """ def _build_data(random_id): """ Builds API data with given random ID :param random_id: """ return { "id": random_id, "api_key": apikey, "method": method, "args": kw } if not method: raise Exception('please specify method name !') id_ = random.randrange(1, 9999) req = urllib2.Request('%s/_admin/api' % apihost, data=json.dumps(_build_data(id_)), headers={'content-type': 'text/plain'}) ret = urllib2.urlopen(req) raw_json = ret.read() json_data = json.loads(raw_json) id_ret = json_data['id'] if id_ret == id_: return json_data else: _formatted_json = pprint.pformat(json_data) raise Exception('something went wrong. ' 'ID mismatch got %s, expected %s | %s' % ( id_ret, id_, _formatted_json))
def index(self): """GET /users_groups: All items in the collection""" # url('users_groups') from rhodecode.lib.utils import PartialRenderer _render = PartialRenderer('data_table/_dt_elements.html') def user_group_name(user_group_id, user_group_name): return _render("user_group_name", user_group_id, user_group_name) def user_group_actions(user_group_id, user_group_name): return _render("user_group_actions", user_group_id, user_group_name) ## json generate group_iter = UserGroupList(UserGroup.query().all(), perm_set=['usergroup.admin']) user_groups_data = [] for user_gr in group_iter: user_groups_data.append({ "group_name": user_group_name(user_gr.users_group_id, h.escape(user_gr.users_group_name)), "group_name_raw": user_gr.users_group_name, "desc": h.escape(user_gr.user_group_description), "members": len(user_gr.members), "active": h.bool2icon(user_gr.users_group_active), "owner": h.escape(h.link_to_user(user_gr.user.username)), "action": user_group_actions(user_gr.users_group_id, user_gr.users_group_name) }) c.data = json.dumps(user_groups_data) return render('admin/user_groups/user_groups.html')
def index(self): _render = PartialRenderer(self.partials_template) _data = [] pre_load = ["author", "date", "message"] repo = c.rhodecode_repo is_svn = h.is_svn(repo) format_ref_id = utils.get_format_ref_id(repo) for ref_name, commit_id in self._get_reference_items(repo): commit = repo.get_commit(commit_id=commit_id, pre_load=pre_load) # TODO: johbo: Unify generation of reference links use_commit_id = '/' in ref_name or is_svn files_url = h.url( 'files_home', repo_name=c.repo_name, f_path=ref_name if is_svn else '', revision=commit_id if use_commit_id else ref_name, at=ref_name) _data.append({ "name": _render('name', ref_name, files_url), "name_raw": ref_name, "date": _render('date', commit.date), "date_raw": datetime_to_time(commit.date), "author": _render('author', commit.author), "commit": _render('commit', commit.message, commit.raw_id, commit.idx), "commit_raw": commit.idx, "compare": _render('compare', format_ref_id(ref_name, commit.raw_id)), }) c.has_references = bool(_data) c.data = json.dumps(_data) return render(self.template)
def _store_metadata(self, repo, gist_id, gist_access_id, user_id, username, gist_type, gist_expires, gist_acl_level): """ store metadata inside the gist repo, this can be later used for imports or gist identification. Currently we use this inside RhodeCode tools to do cleanup of gists that are in storage but not in database. """ metadata = { 'metadata_version': '2', 'gist_db_id': gist_id, 'gist_access_id': gist_access_id, 'gist_owner_id': user_id, 'gist_owner_username': username, 'gist_type': gist_type, 'gist_expires': gist_expires, 'gist_updated': time.time(), 'gist_acl_level': gist_acl_level, } metadata_file = os.path.join(repo.path, '.hg', GIST_METADATA_FILE) with open(metadata_file, 'wb') as f: f.write(json.dumps(metadata))
def jsonify(func, *args, **kwargs): """Action decorator that formats output for JSON Given a function that will return content, this decorator will turn the result into JSON, with a content-type of 'application/json' and output it. """ from pylons.decorators.util import get_pylons from rhodecode.lib.ext_json import json pylons = get_pylons(args) pylons.response.headers['Content-Type'] = 'application/json; charset=utf-8' data = func(*args, **kwargs) if isinstance(data, (list, tuple)): msg = "JSON responses with Array envelopes are susceptible to " \ "cross-site data leak attacks, see " \ "http://wiki.pylonshq.com/display/pylonsfaq/Warnings" warnings.warn(msg, Warning, 2) log.warning(msg) log.debug("Returning JSON wrapped action output") return json.dumps(data, encoding='utf-8')
def _load_my_repos_data(self, watched=False): if watched: admin = False follows_repos = Session().query(UserFollowing)\ .filter(UserFollowing.user_id == c.rhodecode_user.user_id)\ .options(joinedload(UserFollowing.follows_repository))\ .all() repo_list = [x.follows_repository for x in follows_repos] else: admin = True repo_list = Repository.get_all_repos( user_id=c.rhodecode_user.user_id) repo_list = RepoList(repo_list, perm_set=[ 'repository.read', 'repository.write', 'repository.admin' ]) repos_data = RepoModel().get_repos_as_dict(repo_list=repo_list, admin=admin) # json used to render the grid return json.dumps(repos_data)
def test_dumps_datetime(): json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535)) assert '"1969-07-20T03:14:15.926"' == json_data
def jsonify(obj): return json.loads(json.dumps(obj))
def test_dumps_decimal(): assert '"1.5"' == json.dumps(decimal.Decimal('1.5'))
def show(self, id, format='html'): """GET /repos_groups/id: Show a specific item""" # url('repos_group', id=ID) c.group = RepoGroup.get_or_404(id) c.group_repos = c.group.repositories.all() #overwrite our cached list with current filter gr_filter = c.group_repos c.repo_cnt = 0 groups = RepoGroup.query().order_by(RepoGroup.group_name)\ .filter(RepoGroup.group_parent_id == id).all() c.groups = self.scm_model.get_repos_groups(groups) if c.visual.lightweight_dashboard is False: c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter) c.repos_list = c.cached_repo_list ## lightweight version of dashboard else: c.repos_list = Repository.query()\ .filter(Repository.group_id == id)\ .order_by(func.lower(Repository.repo_name))\ .all() repos_data = [] total_records = len(c.repos_list) _tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') quick_menu = lambda repo_name: (template.get_def("quick_menu") .render(repo_name, _=_, h=h, c=c)) repo_lnk = lambda name, rtype, private, fork_of: ( template.get_def("repo_name") .render(name, rtype, private, fork_of, short_name=False, admin=False, _=_, h=h, c=c)) last_change = lambda last_change: (template.get_def("last_change") .render(last_change, _=_, h=h, c=c)) rss_lnk = lambda repo_name: (template.get_def("rss") .render(repo_name, _=_, h=h, c=c)) atom_lnk = lambda repo_name: (template.get_def("atom") .render(repo_name, _=_, h=h, c=c)) for repo in c.repos_list: repos_data.append({ "menu": quick_menu(repo.repo_name), "raw_name": repo.repo_name.lower(), "name": repo_lnk(repo.repo_name, repo.repo_type, repo.private, repo.fork), "last_change": last_change(repo.last_db_change), "desc": repo.description, "owner": h.person(repo.user.username), "rss": rss_lnk(repo.repo_name), "atom": atom_lnk(repo.repo_name), }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": "name", "dir": "asc", "records": repos_data }) return render('admin/repos_groups/repos_groups.html')
def test_dumps_object_with_json_method(): class SerializableObject(object): def __json__(self): return 'foo' assert '"foo"' == json.dumps(SerializableObject())
def test_dumps_date(): assert '"1969-07-20"' == json.dumps(datetime.date(1969, 7, 20))
def test_dumps_time_with_timezone(): with pytest.raises(TypeError) as excinfo: json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0))) error_msg = str(excinfo.value) assert 'Time-zone aware times are not JSON serializable' in error_msg
def test_dumps_time_no_microseconds(): assert '"03:14:15"' == json.dumps(datetime.time(3, 14, 15))
def test_dumps_time(): assert '"03:14:15.926"' == json.dumps(datetime.time(3, 14, 15, 926535))
def test_dumps_object_with_json_attribute(): class SerializableObject(object): __json__ = 'foo' assert '"foo"' == json.dumps(SerializableObject())
def test_dumps_complex(): assert "[0.0, 1.0]" == json.dumps(1j) assert "[1.0, 0.0]" == json.dumps(1 + 0j) assert "[1.1, 1.2]" == json.dumps(1.1 + 1.2j)
def test_dumps_datetime_no_microseconds(): json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15)) assert '"1969-07-20T03:14:15"' == json_data