@@ -47,27 +47,27 @@ class ReposGroupsController(BaseControll
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):
@@ -131,25 +131,25 @@ class ReposGroupsController(BaseControll
% 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_edit.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') \
h.flash(_('error occurred during update of repos group %s') \
% request.POST.get('group_name'), category='error')
return redirect(url('repos_groups'))
def delete(self, id):
"""DELETE /repos_groups/id: Delete an existing item"""
# Forms posted to this method should contain a hidden field:
# <input type="hidden" name="_method" value="DELETE" />
# Or using helpers:
# h.form(url('repos_group', id=ID),
@@ -214,23 +214,29 @@ class ReposGroupsController(BaseControll
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)
c.repos_group = Group.get(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
)
@@ -292,60 +292,69 @@ class Repository(Base):
break
groups.insert(0, gr)
return groups
@property
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
def __repr__(self):
return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id,
self.group_name)
@classmethod
def url_sep(cls):
return '/'
def parents(self):
parents_limit = 5
groups = []
if self.parent_group is None:
cur_gr = self.parent_group
groups.insert(0, cur_gr)
cnt = 0
while 1:
cnt += 1
gr = getattr(cur_gr, 'parent_group', None)
cur_gr = cur_gr.parent_group
if gr is None:
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)
@@ -113,29 +113,36 @@ def ValidUsersGroup(edit, old_data):
'and must begin with alphanumeric '
'character'), value, state)
return _ValidUsersGroup
def ValidReposGroup(edit, old_data):
class _ValidReposGroup(formencode.validators.FancyValidator):
def validate_python(self, value, state):
#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
if edit:
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')}
@@ -65,38 +65,52 @@ class ReposGroupModel(BaseModel):
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)
else:
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)
@@ -117,34 +131,38 @@ class ReposGroupModel(BaseModel):
self.sa.commit()
except:
self.sa.rollback()
raise
def update(self, repos_group_id, form_data):
try:
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 = \
form_data['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: