##// END OF EJS Templates
user-groups: fix potential problem with group sync of external plugins....
user-groups: fix potential problem with group sync of external plugins. - when using external plugin we used to check for a parameter that set the sync mode. The problem is we only checked if the flag was there. So toggling sync on and off set the value and then left the key still set but with None. This confused the sync and thought the group should be synced !

File last commit:

r2117:b02ca503 default
r2143:4314e88b default
Show More
views.py
322 lines | 11.2 KiB | text/x-python | PythonLexer
core: moved users and user groups autocomplete into pyramid....
r1666 # -*- coding: utf-8 -*-
# Copyright (C) 2016-2017 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
core: moved goto_switcher views into pyramid.
r1668 import re
core: moved users and user groups autocomplete into pyramid....
r1666 import logging
from pyramid.view import view_config
from rhodecode.apps._base import BaseAppView
core: moved repo_list data to pyramid.
r1667 from rhodecode.lib import helpers as h
apps: cleanup imports
r2080 from rhodecode.lib.auth import (
LoginRequired, NotAnonymous, HasRepoGroupPermissionAnyDecorator)
core: moved goto_switcher views into pyramid.
r1668 from rhodecode.lib.index import searcher_from_config
core: moved repo_list data to pyramid.
r1667 from rhodecode.lib.utils2 import safe_unicode, str2bool
home: moved home and repo group views into pyramid....
r1774 from rhodecode.lib.ext_json import json
goto-switcher: optimized performance and query capabilities....
r2038 from rhodecode.model.db import (
func, or_, in_filter_generator, Repository, RepoGroup)
core: moved users and user groups autocomplete into pyramid....
r1666 from rhodecode.model.repo import RepoModel
home: moved home and repo group views into pyramid....
r1774 from rhodecode.model.repo_group import RepoGroupModel
goto-switcher: optimized performance and query capabilities....
r2038 from rhodecode.model.scm import RepoGroupList, RepoList
users: moved get_users from RepoModel to UserModel.
r1677 from rhodecode.model.user import UserModel
user-groups: moved get_user_groups from RepoModel to UserGroupModel.
r1676 from rhodecode.model.user_group import UserGroupModel
core: moved users and user groups autocomplete into pyramid....
r1666
log = logging.getLogger(__name__)
class HomeView(BaseAppView):
def load_default_context(self):
c = self._get_local_tmpl_context()
c.user = c.auth_user.get_instance()
self._register_global_c(c)
return c
@LoginRequired()
@view_config(
route_name='user_autocomplete_data', request_method='GET',
renderer='json_ext', xhr=True)
def user_autocomplete_data(self):
query = self.request.GET.get('query')
active = str2bool(self.request.GET.get('active') or True)
include_groups = str2bool(self.request.GET.get('user_groups'))
pull-request-reviewers: added option to add reviewers by picking an user group for pull requests....
r1678 expand_groups = str2bool(self.request.GET.get('user_groups_expand'))
user-autocomplete: allow skipping default user.
r1768 skip_default_user = str2bool(self.request.GET.get('skip_default_user'))
core: moved users and user groups autocomplete into pyramid....
r1666
log.debug('generating user list, query:%s, active:%s, with_groups:%s',
query, active, include_groups)
users: moved get_users from RepoModel to UserModel.
r1677 _users = UserModel().get_users(
core: moved users and user groups autocomplete into pyramid....
r1666 name_contains=query, only_active=active)
user-autocomplete: allow skipping default user.
r1768 def maybe_skip_default_user(usr):
if skip_default_user and usr['username'] == UserModel.cls.DEFAULT_USER:
return False
return True
_users = filter(maybe_skip_default_user, _users)
core: moved users and user groups autocomplete into pyramid....
r1666 if include_groups:
# extend with user groups
user-groups: moved get_user_groups from RepoModel to UserGroupModel.
r1676 _user_groups = UserGroupModel().get_user_groups(
pull-request-reviewers: added option to add reviewers by picking an user group for pull requests....
r1678 name_contains=query, only_active=active,
expand_groups=expand_groups)
core: moved users and user groups autocomplete into pyramid....
r1666 _users = _users + _user_groups
return {'suggestions': _users}
@LoginRequired()
@NotAnonymous()
@view_config(
route_name='user_group_autocomplete_data', request_method='GET',
renderer='json_ext', xhr=True)
def user_group_autocomplete_data(self):
query = self.request.GET.get('query')
active = str2bool(self.request.GET.get('active') or True)
pull-request-reviewers: added option to add reviewers by picking an user group for pull requests....
r1678 expand_groups = str2bool(self.request.GET.get('user_groups_expand'))
core: moved users and user groups autocomplete into pyramid....
r1666 log.debug('generating user group list, query:%s, active:%s',
query, active)
user-groups: moved get_user_groups from RepoModel to UserGroupModel.
r1676 _user_groups = UserGroupModel().get_user_groups(
pull-request-reviewers: added option to add reviewers by picking an user group for pull requests....
r1678 name_contains=query, only_active=active,
expand_groups=expand_groups)
core: moved users and user groups autocomplete into pyramid....
r1666 _user_groups = _user_groups
return {'suggestions': _user_groups}
core: moved repo_list data to pyramid.
r1667
def _get_repo_list(self, name_contains=None, repo_type=None, limit=20):
goto-switcher: optimized performance and query capabilities....
r2038 allowed_ids = self._rhodecode_user.repo_acl_ids(
['repository.read', 'repository.write', 'repository.admin'],
cache=False, name_filter=name_contains)
core: moved repo_list data to pyramid.
r1667 query = Repository.query()\
.order_by(func.length(Repository.repo_name))\
goto-switcher: optimized performance and query capabilities....
r2038 .order_by(Repository.repo_name)\
.filter(or_(
# generate multiple IN to fix limitation problems
*in_filter_generator(Repository.repo_id, allowed_ids)
))
core: moved repo_list data to pyramid.
r1667
if repo_type:
query = query.filter(Repository.repo_type == repo_type)
if name_contains:
ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
query = query.filter(
Repository.repo_name.ilike(ilike_expression))
query = query.limit(limit)
goto-switcher: optimized performance and query capabilities....
r2038 acl_repo_iter = query
core: moved repo_list data to pyramid.
r1667 return [
{
goto-switcher: optimized performance and query capabilities....
r2038 'id': obj.repo_name,
'text': obj.repo_name,
core: moved repo_list data to pyramid.
r1667 'type': 'repo',
repo-data: fixed missing parameter we used to propagate JS with....
r2117 'obj': {'repo_type': obj.repo_type, 'private': obj.private,
'repo_id': obj.repo_id},
goto-switcher: optimized performance and query capabilities....
r2038 'url': h.route_path('repo_summary', repo_name=obj.repo_name)
core: moved repo_list data to pyramid.
r1667 }
goto-switcher: optimized performance and query capabilities....
r2038 for obj in acl_repo_iter]
core: moved repo_list data to pyramid.
r1667
core: moved goto_switcher views into pyramid.
r1668 def _get_repo_group_list(self, name_contains=None, limit=20):
goto-switcher: optimized performance and query capabilities....
r2038 allowed_ids = self._rhodecode_user.repo_group_acl_ids(
['group.read', 'group.write', 'group.admin'],
cache=False, name_filter=name_contains)
core: moved goto_switcher views into pyramid.
r1668 query = RepoGroup.query()\
.order_by(func.length(RepoGroup.group_name))\
goto-switcher: optimized performance and query capabilities....
r2038 .order_by(RepoGroup.group_name) \
.filter(or_(
# generate multiple IN to fix limitation problems
*in_filter_generator(RepoGroup.group_id, allowed_ids)
))
core: moved goto_switcher views into pyramid.
r1668
if name_contains:
ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
query = query.filter(
RepoGroup.group_name.ilike(ilike_expression))
query = query.limit(limit)
goto-switcher: optimized performance and query capabilities....
r2038 acl_repo_iter = query
core: moved goto_switcher views into pyramid.
r1668 return [
{
'id': obj.group_name,
'text': obj.group_name,
'type': 'group',
'obj': {},
goto-switcher: use special commit: prefix to explicitly search for commits....
r2035 'url': h.route_path(
'repo_group_home', repo_group_name=obj.group_name)
core: moved goto_switcher views into pyramid.
r1668 }
goto-switcher: optimized performance and query capabilities....
r2038 for obj in acl_repo_iter]
core: moved goto_switcher views into pyramid.
r1668
goto-switcher: use special commit: prefix to explicitly search for commits....
r2035 def _get_hash_commit_list(self, auth_user, query=None):
if not query or len(query) < 3:
core: moved goto_switcher views into pyramid.
r1668 return []
goto-switcher: use special commit: prefix to explicitly search for commits....
r2035 commit_hashes = re.compile('(?:commit:)([0-9a-f]{2,40})').findall(query)
core: moved goto_switcher views into pyramid.
r1668
if len(commit_hashes) != 1:
return []
commit_hash_prefix = commit_hashes[0]
searcher = searcher_from_config(self.request.registry.settings)
result = searcher.search(
'commit_id:%s*' % commit_hash_prefix, 'commit', auth_user,
raise_on_exc=False)
return [
{
'id': entry['commit_id'],
'text': entry['commit_id'],
'type': 'commit',
'obj': {'repo': entry['repository']},
goto-switcher: use special commit: prefix to explicitly search for commits....
r2035 'url': h.route_path(
'repo_commit',
repo_name=entry['repository'], commit_id=entry['commit_id'])
core: moved goto_switcher views into pyramid.
r1668 }
for entry in result['results']]
core: moved repo_list data to pyramid.
r1667 @LoginRequired()
@view_config(
route_name='repo_list_data', request_method='GET',
renderer='json_ext', xhr=True)
def repo_list_data(self):
_ = self.request.translate
query = self.request.GET.get('query')
repo_type = self.request.GET.get('repo_type')
log.debug('generating repo list, query:%s, repo_type:%s',
query, repo_type)
res = []
repos = self._get_repo_list(query, repo_type=repo_type)
if repos:
res.append({
'text': _('Repositories'),
'children': repos
})
data = {
'more': False,
'results': res
}
return data
core: moved goto_switcher views into pyramid.
r1668
@LoginRequired()
@view_config(
route_name='goto_switcher_data', request_method='GET',
renderer='json_ext', xhr=True)
def goto_switcher_data(self):
c = self.load_default_context()
_ = self.request.translate
query = self.request.GET.get('query')
log.debug('generating goto switcher list, query %s', query)
res = []
repo_groups = self._get_repo_group_list(query)
if repo_groups:
res.append({
'text': _('Groups'),
'children': repo_groups
})
repos = self._get_repo_list(query)
if repos:
res.append({
'text': _('Repositories'),
'children': repos
})
commits = self._get_hash_commit_list(c.auth_user, query)
if commits:
unique_repos = {}
for commit in commits:
unique_repos.setdefault(commit['obj']['repo'], []
).append(commit)
for repo in unique_repos:
res.append({
'text': _('Commits in %(repo)s') % {'repo': repo},
'children': unique_repos[repo]
})
data = {
'more': False,
'results': res
}
return data
home: moved home and repo group views into pyramid....
r1774
def _get_groups_and_repos(self, repo_group_id=None):
# repo groups groups
repo_group_list = RepoGroup.get_all_repo_groups(group_id=repo_group_id)
_perms = ['group.read', 'group.write', '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=False)
# repositories
repo_list = Repository.get_all_repos(group_id=repo_group_id)
_perms = ['repository.read', 'repository.write', 'repository.admin']
repo_list_acl = RepoList(repo_list, perm_set=_perms)
repo_data = RepoModel().get_repos_as_dict(
repo_list=repo_list_acl, admin=False)
return repo_data, repo_group_data
@LoginRequired()
@view_config(
route_name='home', request_method='GET',
renderer='rhodecode:templates/index.mako')
def main_page(self):
c = self.load_default_context()
c.repo_group = None
repo_data, repo_group_data = self._get_groups_and_repos()
# json used to render the grids
c.repos_data = json.dumps(repo_data)
c.repo_groups_data = json.dumps(repo_group_data)
return self._get_template_context(c)
@LoginRequired()
@HasRepoGroupPermissionAnyDecorator(
'group.read', 'group.write', 'group.admin')
@view_config(
route_name='repo_group_home', request_method='GET',
renderer='rhodecode:templates/index_repo_group.mako')
@view_config(
route_name='repo_group_home_slash', request_method='GET',
renderer='rhodecode:templates/index_repo_group.mako')
def repo_group_main_page(self):
c = self.load_default_context()
c.repo_group = self.request.db_repo_group
repo_data, repo_group_data = self._get_groups_and_repos(
c.repo_group.group_id)
# json used to render the grids
c.repos_data = json.dumps(repo_data)
c.repo_groups_data = json.dumps(repo_group_data)
return self._get_template_context(c)