def _c(): log.debug('generating switcher repo/groups list') all_repos = Repository.query(sorted=True).all() repo_iter = self.scm_model.get_repos(all_repos) all_groups = RepoGroup.query(sorted=True).all() repo_groups_iter = self.scm_model.get_repo_groups(all_groups) res = [{ 'text': _('Groups'), 'children': [ {'id': obj.group_name, 'text': obj.group_name, 'type': 'group', 'obj': {}} for obj in repo_groups_iter ], }, { 'text': _('Repositories'), 'children': [ {'id': obj.repo_name, 'text': obj.repo_name, 'type': 'repo', 'obj': obj.get_dict()} for obj in repo_iter ], }] data = { 'more': False, 'results': res, } return data
def show(self, group_name): """GET /repo_groups/group_name: Show a specific item""" # url('repos_group', group_name=GROUP_NAME) c.active = 'settings' c.group = c.repo_group = RepoGroupModel()._get_repo_group(group_name) c.group_repos = c.group.repositories.all() #overwrite our cached list with current filter 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_repo_groups(groups) 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/repo_groups/repo_group_show.html')
def _c(): log.debug('generating switcher repo/groups list') all_repos = Repository.query().order_by(Repository.repo_name).all() repo_iter = self.scm_model.get_repos(all_repos) all_groups = RepoGroup.query().order_by(RepoGroup.group_name).all() repo_groups_iter = self.scm_model.get_repo_groups(all_groups) res = [{ 'text': _('Groups'), 'children': [ {'id': obj.group_name, 'text': obj.group_name, 'type': 'group', 'obj': {}} for obj in repo_groups_iter ], }, { 'text': _('Repositories'), 'children': [ {'id': obj.repo_name, 'text': obj.repo_name, 'type': 'repo', 'obj': obj.get_dict()} for obj in repo_iter ], }] data = { 'more': False, 'results': res, } return data
def __load_defaults(self): acl_groups = RepoGroupList(RepoGroup.query().all(), perm_set=['group.write', 'group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) choices, c.landing_revs = ScmModel().get_repo_landing_revs() c.landing_revs_choices = choices c.can_update = Ui.get_by_key(Ui.HOOK_UPDATE).ui_active
def get_repo_groups(self, groups=None): """Return the repo groups the user has access to If no groups are specified, use top level groups. """ if groups is None: groups = RepoGroup.query() \ .filter(RepoGroup.parent_group_id == None).all() return RepoGroupList(groups, perm_level='read')
def get_repo_groups(self, groups=None): """Return the repo groups the user has access to If no groups are specified, use top level groups. """ if groups is None: groups = RepoGroup.query() \ .filter(RepoGroup.group_parent_id == None).all() return RepoGroupList(groups)
def index(self, format='html'): """GET /repo_groups: All items in the collection""" # url('repos_groups') _list = RepoGroup.query()\ .order_by(func.lower(RepoGroup.group_name))\ .all() group_iter = RepoGroupList(_list, perm_set=['group.admin']) repo_groups_data = [] total_records = len(group_iter) _tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') repo_group_name = lambda repo_group_name, children_groups: ( template.get_def("repo_group_name").render( repo_group_name, children_groups, _=_, h=h, c=c)) repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( template.get_def("repo_group_actions").render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, ungettext=ungettext)) for repo_gr in group_iter: children_groups = map( h.safe_unicode, itertools.chain((g.name for g in repo_gr.parents), (x.name for x in [repo_gr]))) repo_count = repo_gr.repositories.count() repo_groups_data.append({ "raw_name": repo_gr.group_name, "group_name": repo_group_name(repo_gr.group_name, children_groups), "desc": repo_gr.group_description, "repos": repo_count, "owner": h.person(repo_gr.user), "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, repo_count) }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": repo_groups_data }) return render('admin/repo_groups/repo_groups.html')
def fixup_groups(self): def_usr = User.get_default_user() for g in RepoGroup.query().all(): g.group_name = g.get_new_name(g.name) # get default perm default = UserRepoGroupToPerm.query() \ .filter(UserRepoGroupToPerm.group == g) \ .filter(UserRepoGroupToPerm.user == def_usr) \ .scalar() if default is None: log.debug('missing default permission for group %s adding', g) RepoGroupModel()._create_default_perms(g)
def index(self, format='html'): _list = RepoGroup.query(sorted=True).all() group_iter = RepoGroupList(_list, perm_level='admin') repo_groups_data = [] total_records = len(group_iter) _tmpl_lookup = app_globals.mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') repo_group_name = lambda repo_group_name, children_groups: ( template.get_def("repo_group_name").render( repo_group_name, children_groups, _=_, h=h, c=c)) repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( template.get_def("repo_group_actions").render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, ungettext=ungettext)) for repo_gr in group_iter: children_groups = map( h.safe_unicode, itertools.chain((g.name for g in repo_gr.parents), (x.name for x in [repo_gr]))) repo_count = repo_gr.repositories.count() repo_groups_data.append({ "raw_name": repo_gr.group_name, "group_name": repo_group_name(repo_gr.group_name, children_groups), "desc": h.escape(repo_gr.group_description), "repos": repo_count, "owner": h.person(repo_gr.owner), "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, repo_count) }) c.data = { "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": repo_groups_data } return render('admin/repo_groups/repo_groups.html')
def show(self, group_name): c.active = 'settings' c.group = c.repo_group = RepoGroup.guess_instance(group_name) groups = RepoGroup.query(sorted=True).filter_by(parent_group=c.group).all() repo_groups_list = self.scm_model.get_repo_groups(groups) repos_list = Repository.query(sorted=True).filter_by(group=c.group).all() c.data = RepoModel().get_repos_as_dict(repos_list, repo_groups_list=repo_groups_list, short_name=True) return render('admin/repo_groups/repo_group_show.html')
def show(self, group_name): c.active = 'settings' c.group = c.repo_group = RepoGroup.guess_instance(group_name) groups = RepoGroup.query(sorted=True).filter_by(parent_group=c.group).all() c.groups = self.scm_model.get_repo_groups(groups) repos_list = Repository.query(sorted=True).filter_by(group=c.group).all() repos_data = RepoModel().get_repos_as_dict(repos_list=repos_list, admin=False, short_name=True) # data used to render the grid c.data = repos_data return render('admin/repo_groups/repo_group_show.html')
def validate_python(self, value, state): # TODO WRITE VALIDATIONS group_name = value.get('group_name') group_parent_id = value.get('group_parent_id') # slugify repo group just in case :) slug = repo_name_slug(group_name) # check for parent of self parent_of_self = lambda: (old_data['group_id'] == int( group_parent_id) if group_parent_id else False) if edit and parent_of_self(): msg = M(self, 'group_parent_id', state) raise formencode.Invalid(msg, value, state, error_dict=dict(group_parent_id=msg)) old_gname = None if edit: old_gname = RepoGroup.get(old_data.get('group_id')).group_name if old_gname != group_name or not edit: # check group gr = RepoGroup.query()\ .filter(RepoGroup.group_name == slug)\ .filter(RepoGroup.group_parent_id == group_parent_id)\ .scalar() if gr: msg = M(self, 'group_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg)) # check for same repo repo = Repository.query()\ .filter(Repository.repo_name == slug)\ .scalar() if repo: msg = M(self, 'repo_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg))
def validate_python(self, value, state): # TODO WRITE VALIDATIONS group_name = value.get('group_name') group_parent_id = value.get('group_parent_id') # slugify repo group just in case :) slug = repo_name_slug(group_name) # check for parent of self parent_of_self = lambda: ( old_data['group_id'] == group_parent_id if group_parent_id else False ) if edit and parent_of_self(): msg = M(self, 'group_parent_id', state) raise formencode.Invalid(msg, value, state, error_dict=dict(group_parent_id=msg) ) old_gname = None if edit: old_gname = RepoGroup.get(old_data.get('group_id')).group_name if old_gname != group_name or not edit: # check group gr = RepoGroup.query() \ .filter(RepoGroup.group_name == slug) \ .filter(RepoGroup.group_parent_id == group_parent_id) \ .scalar() if gr is not None: msg = M(self, 'group_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg) ) # check for same repo repo = Repository.query() \ .filter(Repository.repo_name == slug) \ .scalar() if repo is not None: msg = M(self, 'repo_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg) )
def index(self, format='html'): """GET /repo_groups: All items in the collection""" # url('repos_groups') _list = RepoGroup.query()\ .order_by(func.lower(RepoGroup.group_name))\ .all() group_iter = RepoGroupList(_list, perm_set=['group.admin']) repo_groups_data = [] total_records = len(group_iter) _tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') repo_group_name = lambda repo_group_name, children_groups: ( template.get_def("repo_group_name") .render(repo_group_name, children_groups, _=_, h=h, c=c) ) repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( template.get_def("repo_group_actions") .render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, ungettext=ungettext) ) for repo_gr in group_iter: children_groups = map(h.safe_unicode, itertools.chain((g.name for g in repo_gr.parents), (x.name for x in [repo_gr]))) repo_count = repo_gr.repositories.count() repo_groups_data.append({ "raw_name": repo_gr.group_name, "group_name": repo_group_name(repo_gr.group_name, children_groups), "desc": h.escape(repo_gr.group_description), "repos": repo_count, "owner": h.person(repo_gr.user), "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, repo_count) }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": repo_groups_data }) return render('admin/repo_groups/repo_groups.html')
def AvailableRepoGroupChoices(top_perms, repo_group_perm_level, extras=()): """Return group_id,string tuples with choices for all the repo groups where the user has the necessary permissions. Top level is -1. """ groups = RepoGroup.query().all() if HasPermissionAny('hg.admin')('available repo groups'): groups.append(None) else: groups = list(RepoGroupList(groups, perm_level=repo_group_perm_level)) if top_perms and HasPermissionAny(*top_perms)('available repo groups'): groups.append(None) for extra in extras: if not any(rg == extra for rg in groups): groups.append(extra) return RepoGroup.groups_choices(groups=groups)
def AvailableRepoGroupChoices(top_perms, repo_group_perms, extras=()): """Return group_id,string tuples with choices for all the repo groups where the user has the necessary permissions. Top level is -1. """ groups = RepoGroup.query().all() if HasPermissionAll('hg.admin')('available repo groups'): groups.append(None) else: groups = list(RepoGroupList(groups, perm_set=repo_group_perms)) if top_perms and HasPermissionAny(*top_perms)('available repo groups'): groups.append(None) for extra in extras: if not any(rg == extra for rg in groups): groups.append(extra) return RepoGroup.groups_choices(groups=groups)
def _validate_python(self, value, state): # TODO WRITE VALIDATIONS group_name = value.get('group_name') parent_group_id = value.get('parent_group_id') # slugify repo group just in case :) slug = repo_name_slug(group_name) # check for parent of self if edit and parent_group_id and old_data[ 'group_id'] == parent_group_id: msg = self.message('parent_group_id', state) raise formencode.Invalid(msg, value, state, error_dict=dict(parent_group_id=msg)) old_gname = None if edit: old_gname = RepoGroup.get(old_data.get('group_id')).group_name if old_gname != group_name or not edit: # check group gr = RepoGroup.query() \ .filter(func.lower(RepoGroup.group_name) == func.lower(slug)) \ .filter(RepoGroup.parent_group_id == parent_group_id) \ .scalar() if gr is not None: msg = self.message('group_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg)) # check for same repo repo = Repository.query() \ .filter(func.lower(Repository.repo_name) == func.lower(slug)) \ .scalar() if repo is not None: msg = self.message('repo_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg))
def __load_defaults(self, allow_empty_group=False, exclude_group_ids=[]): if HasPermissionAll('hg.admin')('group edit'): #we're global admin, we're ok and we can create TOP level groups allow_empty_group = True #override the choices for this form, we need to filter choices #and display only those we have ADMIN right groups_with_admin_rights = RepoGroupList(RepoGroup.query().all(), perm_set=['group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=groups_with_admin_rights, show_empty_group=allow_empty_group) # exclude filtered ids c.repo_groups = filter(lambda x: x[0] not in exclude_group_ids, c.repo_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) repo_model = RepoModel() c.users_array = repo_model.get_users_js() c.user_groups_array = repo_model.get_user_groups_js()
def __load_defaults(self, allow_empty_group=False, exclude_group_ids=[]): if HasPermissionAll('hg.admin')('group edit'): #we're global admin, we're ok and we can create TOP level groups allow_empty_group = True #override the choices for this form, we need to filter choices #and display only those we have ADMIN right groups_with_admin_rights = RepoGroupList(RepoGroup.query().all(), perm_set=['group.admin']) c.repo_groups = RepoGroup.groups_choices( groups=groups_with_admin_rights, show_empty_group=allow_empty_group) # exclude filtered ids c.repo_groups = filter(lambda x: x[0] not in exclude_group_ids, c.repo_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) repo_model = RepoModel() c.users_array = repo_model.get_users_js() c.user_groups_array = repo_model.get_user_groups_js()
def __load_defaults(self, repo=None): acl_groups = RepoGroupList(RepoGroup.query().all(), perm_set=['group.write', 'group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) # in case someone no longer have a group.write access to a repository # pre fill the list with this entry, we don't care if this is the same # but it will allow saving repo data properly. repo_group = None if repo: repo_group = repo.group if repo_group and unicode(repo_group.group_id) not in c.repo_groups_choices: c.repo_groups_choices.append(unicode(repo_group.group_id)) c.repo_groups.append(RepoGroup._generate_choice(repo_group)) choices, c.landing_revs = ScmModel().get_repo_landing_revs() c.landing_revs_choices = choices
def index(self, format='html'): _list = RepoGroup.query(sorted=True).all() group_iter = RepoGroupList(_list, perm_level='admin') repo_groups_data = [] total_records = len(group_iter) _tmpl_lookup = app_globals.mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') repo_group_name = lambda repo_group_name, children_groups: ( template.get_def("repo_group_name") .render(repo_group_name, children_groups, _=_, h=h, c=c) ) repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( template.get_def("repo_group_actions") .render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, ungettext=ungettext) ) for repo_gr in group_iter: children_groups = map(h.safe_unicode, itertools.chain((g.name for g in repo_gr.parents), (x.name for x in [repo_gr]))) repo_count = repo_gr.repositories.count() repo_groups_data.append({ "raw_name": repo_gr.group_name, "group_name": repo_group_name(repo_gr.group_name, children_groups), "desc": h.escape(repo_gr.group_description), "repos": repo_count, "owner": h.person(repo_gr.owner), "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, repo_count) }) c.data = { "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": repo_groups_data } return render('admin/repo_groups/repo_groups.html')
def __load_defaults(self, repo=None): acl_groups = RepoGroupList(RepoGroup.query().all(), perm_set=['group.write', 'group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) # in case someone no longer have a group.write access to a repository # pre fill the list with this entry, we don't care if this is the same # but it will allow saving repo data properly. repo_group = None if repo: repo_group = repo.group if repo_group and unicode( repo_group.group_id) not in c.repo_groups_choices: c.repo_groups_choices.append(unicode(repo_group.group_id)) c.repo_groups.append(RepoGroup._generate_choice(repo_group)) choices, c.landing_revs = ScmModel().get_repo_landing_revs() c.landing_revs_choices = choices
def repo_switcher_data(self): if request.is_xhr: all_repos = Repository.query(sorted=True).all() repo_iter = self.scm_model.get_repos(all_repos) all_groups = RepoGroup.query(sorted=True).all() repo_groups_iter = self.scm_model.get_repo_groups(all_groups) res = [{ 'text': _('Groups'), 'children': [{ 'id': obj.group_name, 'text': obj.group_name, 'type': 'group', 'obj': {} } for obj in repo_groups_iter], }, { 'text': _('Repositories'), 'children': [{ 'id': obj.repo_name, 'text': obj.repo_name, 'type': 'repo', 'obj': obj.get_dict() } for obj in repo_iter], }] for res_dict in res: for child in (res_dict['children']): child['obj'].pop( '_changeset_cache', None ) # bytes cannot be encoded in json ... but this value isn't relevant on client side at all ... data = { 'more': False, 'results': res, } return data else: raise HTTPBadRequest()
def create_repository(self): """GET /_admin/create_repository: Form to create a new item""" new_repo = request.GET.get('repo', '') parent_group = request.GET.get('parent_group') if not HasPermissionAny('hg.admin', 'hg.create.repository')(): #you're not super admin nor have global create permissions, #but maybe you have at least write permission to a parent group ? _gr = RepoGroup.get(parent_group) gr_name = _gr.group_name if _gr else None # create repositories with write permission on group is set to true create_on_write = HasPermissionAny( 'hg.create.write_on_repogroup.true')() group_admin = HasRepoGroupPermissionAny('group.admin')( group_name=gr_name) group_write = HasRepoGroupPermissionAny('group.write')( group_name=gr_name) if not (group_admin or (group_write and create_on_write)): raise HTTPForbidden acl_groups = RepoGroupList(RepoGroup.query().all(), perm_set=['group.write', 'group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) choices, c.landing_revs = ScmModel().get_repo_landing_revs() c.new_repo = repo_name_slug(new_repo) ## apply the defaults from defaults page defaults = Setting.get_default_repo_settings(strip_prefix=True) if parent_group: defaults.update({'repo_group': parent_group}) return htmlfill.render(render('admin/repos/repo_add.html'), defaults=defaults, errors={}, prefix_error=False, encoding="UTF-8", force_defaults=False)
def create_repository(self): """GET /_admin/create_repository: Form to create a new item""" new_repo = request.GET.get('repo', '') parent_group = request.GET.get('parent_group') if not HasPermissionAny('hg.admin', 'hg.create.repository')(): #you're not super admin nor have global create permissions, #but maybe you have at least write permission to a parent group ? _gr = RepoGroup.get(parent_group) gr_name = _gr.group_name if _gr else None # create repositories with write permission on group is set to true create_on_write = HasPermissionAny('hg.create.write_on_repogroup.true')() group_admin = HasRepoGroupPermissionAny('group.admin')(group_name=gr_name) group_write = HasRepoGroupPermissionAny('group.write')(group_name=gr_name) if not (group_admin or (group_write and create_on_write)): raise HTTPForbidden acl_groups = RepoGroupList(RepoGroup.query().all(), perm_set=['group.write', 'group.admin']) c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) choices, c.landing_revs = ScmModel().get_repo_landing_revs() c.new_repo = repo_name_slug(new_repo) ## apply the defaults from defaults page defaults = Setting.get_default_repo_settings(strip_prefix=True) if parent_group: defaults.update({'repo_group': parent_group}) return htmlfill.render( render('admin/repos/repo_add.html'), defaults=defaults, errors={}, prefix_error=False, encoding="UTF-8", force_defaults=False)
def _c(): log.debug('generating switcher repo/groups list') all_repos = Repository.query().order_by(Repository.repo_name).all() repo_iter = self.scm_model.get_repos(all_repos, simple=True) all_groups = RepoGroup.query().order_by(RepoGroup.group_name).all() repo_groups_iter = self.scm_model.get_repo_groups(all_groups) res = [{ 'text': _('Groups'), 'children': [ {'id': obj.group_name, 'text': obj.group_name, 'type': 'group', 'obj': {}} for obj in repo_groups_iter] }, { 'text': _('Repositories'), 'children': [ {'id': obj['name'], 'text': obj['name'], 'type': 'repo', 'obj': obj['dbrepo']} for obj in repo_iter] }] data = { 'more': False, 'results': res } return data
def index(self, format='html'): _list = RepoGroup.query(sorted=True).all() group_iter = RepoGroupList(_list, perm_level='admin') repo_groups_data = [] _tmpl_lookup = app_globals.mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') def repo_group_name(repo_group_name, children_groups): return template.get_def("repo_group_name") \ .render_unicode(repo_group_name, children_groups, _=_, h=h, c=c) def repo_group_actions(repo_group_id, repo_group_name, gr_count): return template.get_def("repo_group_actions") \ .render_unicode(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, ungettext=ungettext) for repo_gr in group_iter: children_groups = [g.name for g in repo_gr.parents] + [repo_gr.name] repo_count = repo_gr.repositories.count() repo_groups_data.append({ "raw_name": repo_gr.group_name, "group_name": repo_group_name(repo_gr.group_name, children_groups), "desc": h.escape(repo_gr.group_description), "repos": repo_count, "owner": h.person(repo_gr.owner), "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, repo_count) }) c.data = { "sort": None, "dir": "asc", "records": repo_groups_data } return render('admin/repo_groups/repo_groups.html')
def get_repo_groups(self, all_groups=None): if all_groups is None: all_groups = RepoGroup.query()\ .filter(RepoGroup.group_parent_id == None).all() return [x for x in RepoGroupList(all_groups)]