@@ -11,226 +11,232 @@ from pylons.i18n.translation import _
from rhodecode.lib import helpers as h
from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \
HasPermissionAnyDecorator
from rhodecode.lib.base import BaseController, render
from rhodecode.model.db import Group
from rhodecode.model.repos_group import ReposGroupModel
from rhodecode.model.forms import ReposGroupForm
log = logging.getLogger(__name__)
class ReposGroupsController(BaseController):
"""REST Controller styled on the Atom Publishing Protocol"""
# To properly map this controller, ensure your config/routing.py
# file has a resource setup:
# map.resource('repos_group', 'repos_groups')
@LoginRequired()
def __before__(self):
super(ReposGroupsController, self).__before__()
def __load_defaults(self):
c.repo_groups = [('', '')]
parents_link = lambda k: h.literal('»'.join(
map(lambda k: k.group_name,
k.parents + [k])
)
c.repo_groups.extend([(x.group_id, parents_link(x)) for \
x in self.sa.query(Group).all()])
c.repo_groups = sorted(c.repo_groups,
key=lambda t: t[1].split('»')[0])
c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups)
def __load_data(self, group_id):
"""
Load defaults settings for edit, and update
:param group_id:
self.__load_defaults()
repo_group = Group.get(group_id)
defaults = repo_group.get_dict()
data = repo_group.get_dict()
return defaults
return data
@HasPermissionAnyDecorator('hg.admin')
def index(self, format='html'):
"""GET /repos_groups: All items in the collection"""
# url('repos_groups')
sk = lambda g:g.parents[0].group_name if g.parents else g.group_name
c.groups = sorted(Group.query().all(), key=sk)
return render('admin/repos_groups/repos_groups_show.html')
def create(self):
"""POST /repos_groups: Create a new item"""
repos_group_model = ReposGroupModel()
repos_group_form = ReposGroupForm(available_groups=
c.repo_groups_choices)()
try:
form_result = repos_group_form.to_python(dict(request.POST))
repos_group_model.create(form_result)
h.flash(_('created repos group %s') \
% form_result['group_name'], category='success')
#TODO: in futureaction_logger(, '', '', '', self.sa)
except formencode.Invalid, errors:
return htmlfill.render(
render('admin/repos_groups/repos_groups_add.html'),
defaults=errors.value,
errors=errors.error_dict or {},
prefix_error=False,
encoding="UTF-8")
except Exception:
log.error(traceback.format_exc())
h.flash(_('error occurred during creation of repos group %s') \
% request.POST.get('group_name'), category='error')
return redirect(url('repos_groups'))
def new(self, format='html'):
"""GET /repos_groups/new: Form to create a new item"""
# url('new_repos_group')
return render('admin/repos_groups/repos_groups_add.html')
def update(self, id):
"""PUT /repos_groups/id: Update an existing item"""
# Forms posted to this method should contain a hidden field:
# <input type="hidden" name="_method" value="PUT" />
# Or using helpers:
# h.form(url('repos_group', id=ID),
# method='put')
# url('repos_group', id=ID)
c.repos_group = Group.get(id)
repos_group_form = ReposGroupForm(edit=True,
old_data=c.repos_group.get_dict(),
available_groups=
repos_group_model.update(id, form_result)
h.flash(_('updated repos group %s') \
render('admin/repos_groups/repos_groups_edit.html'),
h.flash(_('error occurred during update of repos group %s') \
def delete(self, id):
"""DELETE /repos_groups/id: Delete an existing item"""
# <input type="hidden" name="_method" value="DELETE" />
# method='delete')
gr = Group.get(id)
repos = gr.repositories.all()
if repos:
h.flash(_('This group contains %s repositores and cannot be '
'deleted' % len(repos)),
category='error')
repos_group_model.delete(id)
h.flash(_('removed repos group %s' % gr.group_name), category='success')
h.flash(_('error occurred during deletion of repos group %s' % gr.group_name),
def show(self, id, format='html'):
"""GET /repos_groups/id: Show a specific item"""
c.group = Group.get(id)
if c.group:
c.group_repos = c.group.repositories.all()
else:
return redirect(url('repos_group'))
sortables = ['name', 'description', 'last_change', 'tip', 'owner']
current_sort = request.GET.get('sort', 'name')
current_sort_slug = current_sort.replace('-', '')
if current_sort_slug not in sortables:
c.sort_by = 'name'
current_sort_slug = c.sort_by
c.sort_by = current_sort
c.sort_slug = current_sort_slug
sort_key = current_sort_slug + '_sort'
#overwrite our cached list with current filter
gr_filter = [r.repo_name for r in c.group_repos]
c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter)
if c.sort_by.startswith('-'):
c.repos_list = sorted(c.cached_repo_list, key=itemgetter(sort_key),
reverse=True)
reverse=False)
c.repo_cnt = len(c.repos_list)
c.groups = self.sa.query(Group).order_by(Group.group_name)\
.filter(Group.group_parent_id == id).all()
return render('admin/repos_groups/repos_groups.html')
def edit(self, id, format='html'):
"""GET /repos_groups/id/edit: Form to edit an existing item"""
# url('edit_repos_group', id=ID)
id = int(id)
defaults = self.__load_data(id)
# we need to exclude this group from the group list for editing
c.repo_groups = filter(lambda x:x[0] != id, c.repo_groups)
defaults=defaults,
encoding="UTF-8",
force_defaults=False
@@ -256,132 +256,141 @@ class Repository(Base):
users_group_to_perm = relationship('UsersGroupRepoToPerm', cascade='all')
stats = relationship('Statistics', cascade='all', uselist=False)
followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
logs = relationship('UserLog', cascade='all')
def __repr__(self):
return "<%s('%s:%s')>" % (self.__class__.__name__,
self.repo_id, self.repo_name)
@classmethod
def by_repo_name(cls, repo_name):
return Session.query(cls).filter(cls.repo_name == repo_name).one()
def get_repo_forks(cls, repo_id):
return Session.query(cls).filter(Repository.fork_id == repo_id)
@property
def just_name(self):
return self.repo_name.split(os.sep)[-1]
def groups_with_parents(self):
groups = []
if self.group is None:
return groups
cur_gr = self.group
groups.insert(0, cur_gr)
while 1:
gr = getattr(cur_gr, 'parent_group', None)
cur_gr = cur_gr.parent_group
if gr is None:
break
groups.insert(0, gr)
def groups_and_repo(self):
return self.groups_with_parents, self.just_name
class Group(Base):
__tablename__ = 'groups'
__table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), {'useexisting':True},)
__table_args__ = (UniqueConstraint('group_name', 'group_parent_id'),
CheckConstraint('group_id != group_parent_id'), {'useexisting':True},)
__mapper_args__ = {'order_by':'group_name'}
group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
group_name = Column("group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
group_description = Column("group_description", String(length=10000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
parent_group = relationship('Group', remote_side=group_id)
def __init__(self, group_name='', parent_group=None):
self.group_name = group_name
self.parent_group = parent_group
return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id,
self.group_name)
def url_sep(cls):
return '/'
def parents(self):
parents_limit = 5
if self.parent_group is None:
cur_gr = self.parent_group
cnt = 0
cnt += 1
if cnt == parents_limit:
# this will prevent accidental infinit loops
log.error('group nested more than %s' %
parents_limit)
def full_path(self):
return Group.url_sep().join([g.group_name for g in self.parents] +
[self.group_name])
def repositories(self):
return Session.query(Repository).filter(Repository.group == self)
class Permission(Base):
__tablename__ = 'permissions'
__table_args__ = {'useexisting':True}
permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
permission_name = Column("permission_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
permission_longname = Column("permission_longname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
self.permission_id, self.permission_name)
def get_by_key(cls, key):
return Session.query(cls).filter(cls.permission_name == key).scalar()
class RepoToPerm(Base):
__tablename__ = 'repo_to_perm'
__table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
user = relationship('User')
permission = relationship('Permission')
repository = relationship('Repository')
class UserToPerm(Base):
__tablename__ = 'user_to_perm'
__table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
@@ -77,101 +77,108 @@ def ValidUsername(edit, old_data):
if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None:
raise formencode.Invalid(_('Username may only contain '
'alphanumeric characters '
'underscores, periods or dashes '
'and must begin with alphanumeric '
'character'), value, state)
return _ValidUsername
def ValidUsersGroup(edit, old_data):
class _ValidUsersGroup(formencode.validators.FancyValidator):
def validate_python(self, value, state):
if value in ['default']:
raise formencode.Invalid(_('Invalid group name'), value, state)
#check if group is unique
old_ugname = None
if edit:
old_ugname = UsersGroup.get(
old_data.get('users_group_id')).users_group_name
if old_ugname != value or not edit:
if UsersGroup.get_by_group_name(value, cache=False,
case_insensitive=True):
raise formencode.Invalid(_('This users group '
'already exists') , value,
state)
raise formencode.Invalid(_('Group name may only contain '
return _ValidUsersGroup
def ValidReposGroup(edit, old_data):
class _ValidReposGroup(formencode.validators.FancyValidator):
#TODO WRITE VALIDATIONS
group_name = value.get('group_name')
group_parent_id = value.get('group_parent_id')
group_parent_id = int(value.get('group_parent_id') or - 1)
# slugify repo group just in case :)
slug = repo_name_slug(group_name)
# check for parent of self
if old_data['group_id'] == group_parent_id:
e_dict = {'group_parent_id':_('Cannot assign this group '
'as parent')}
raise formencode.Invalid('', value, state,
error_dict=e_dict)
old_gname = None
old_gname = Group.get(
old_data.get('group_id')).group_name
if old_gname != group_name or not edit:
# check filesystem
gr = Group.query().filter(Group.group_name == slug)\
.filter(Group.group_parent_id == group_parent_id).scalar()
if gr:
e_dict = {'group_name':_('This group already exists')}
return _ValidReposGroup
class ValidPassword(formencode.validators.FancyValidator):
def to_python(self, value, state):
if value:
if value.get('password'):
value['password'] = get_crypt_password(value['password'])
except UnicodeEncodeError:
e_dict = {'password':_('Invalid characters in password')}
raise formencode.Invalid('', value, state, error_dict=e_dict)
if value.get('password_confirmation'):
value['password_confirmation'] = \
get_crypt_password(value['password_confirmation'])
e_dict = {'password_confirmation':_('Invalid characters in password')}
if value.get('new_password'):
value['new_password'] = \
get_crypt_password(value['new_password'])
e_dict = {'new_password':_('Invalid characters in password')}
return value
@@ -29,127 +29,145 @@ import traceback
import shutil
from pylons.i18n.translation import _
from vcs.utils.lazy import LazyProperty
from rhodecode.model import BaseModel
from rhodecode.model.caching_query import FromCache
from rhodecode.model.db import Group, RhodeCodeUi
class ReposGroupModel(BaseModel):
@LazyProperty
def repos_path(self):
Get's the repositories root path from database
q = RhodeCodeUi.get_by_key('/').one()
return q.ui_value
def __create_group(self, group_name, parent_id):
makes repositories group on filesystem
:param repo_name:
:param parent_id:
if parent_id:
paths = Group.get(parent_id).full_path.split(Group.url_sep())
parent_path = os.sep.join(paths)
parent_path = ''
create_path = os.path.join(self.repos_path, parent_path, group_name)
log.debug('creating new group in %s', create_path)
if os.path.isdir(create_path):
raise Exception('That directory already exists !')
os.makedirs(create_path)
def __rename_group(self, old, new):
def __rename_group(self, old, old_parent_id, new, new_parent_id):
Renames a group on filesystem
:param group_name:
log.info('renaming repos group from %s to %s', old,
old)
log.debug('renaming repos group from %s to %s', old, new)
if new_parent_id:
paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
new_parent_path = os.sep.join(paths)
new_parent_path = ''
old_path = os.path.join(self.repos_path, old)
new_path = os.path.join(self.repos_path, new)
if old_parent_id:
paths = Group.get(old_parent_id).full_path.split(Group.url_sep())
old_parent_path = os.sep.join(paths)
old_parent_path = ''
old_path = os.path.join(self.repos_path, old_parent_path, old)
new_path = os.path.join(self.repos_path, new_parent_path, new)
log.debug('renaming repos paths from %s to %s', old_path, new_path)
if os.path.isdir(new_path):
raise Exception('Was trying to rename to already existing dir %s',
new_path)
raise Exception('Was trying to rename to already '
'existing dir %s' % new_path)
shutil.move(old_path, new_path)
def __delete_group(self, group):
Deletes a group from a filesystem
:param group: instance of group from database
paths = group.full_path.split(Group.url_sep())
paths = os.sep.join(paths)
rm_path = os.path.join(self.repos_path, paths)
os.rmdir(rm_path)
def create(self, form_data):
new_repos_group = Group()
new_repos_group.group_name = form_data['group_name']
new_repos_group.group_description = \
form_data['group_description']
new_repos_group.group_parent_id = form_data['group_parent_id']
self.sa.add(new_repos_group)
self.__create_group(form_data['group_name'],
form_data['group_parent_id'])
self.sa.commit()
except:
self.sa.rollback()
raise
def update(self, repos_group_id, form_data):
repos_group = Group.get(repos_group_id)
old_name = repos_group.group_name
old_parent_id = repos_group.group_parent_id
repos_group.group_name = form_data['group_name']
repos_group.group_description = \
repos_group.group_parent_id = form_data['group_parent_id']
self.sa.add(repos_group)
if old_name != form_data['group_name']:
self.__rename_group(old=old_name, new=form_data['group_name'])
if old_name != form_data['group_name'] or (old_parent_id !=
form_data['group_parent_id']):
self.__rename_group(old=old_name, old_parent_id=old_parent_id,
new=form_data['group_name'],
new_parent_id=form_data['group_parent_id'])
def delete(self, users_group_id):
users_group = Group.get(users_group_id)
self.sa.delete(users_group)
self.__delete_group(users_group)
Status change: