diff --git a/rhodecode/controllers/admin/repos.py b/rhodecode/controllers/admin/repos.py --- a/rhodecode/controllers/admin/repos.py +++ b/rhodecode/controllers/admin/repos.py @@ -42,12 +42,11 @@ from rhodecode.lib.auth import LoginRequ from rhodecode.lib.base import BaseController, render from rhodecode.lib.utils import invalidate_cache, action_logger, repo_name_slug from rhodecode.lib.helpers import get_token -from rhodecode.model.db import User, Repository, UserFollowing +from rhodecode.model.db import User, Repository, UserFollowing, Group from rhodecode.model.forms import RepoForm from rhodecode.model.scm import ScmModel from rhodecode.model.repo import RepoModel - log = logging.getLogger(__name__) class ReposController(BaseController): @@ -64,6 +63,82 @@ class ReposController(BaseController): c.admin_username = session.get('admin_username') super(ReposController, self).__before__() + + + def __load_data(self, repo_name): + """ + Load defaults settings for edit, and update + + :param repo_name: + """ + repo, dbrepo = ScmModel().get(repo_name, retval='repo') + + repo_model = RepoModel() + c.repo_info = repo_model.get_by_repo_name(repo_name) + + if c.repo_info is None: + h.flash(_('%s repository is not mapped to db perhaps' + ' it was created or renamed from the filesystem' + ' please run the application again' + ' in order to rescan repositories') % repo_name, + category='error') + + return redirect(url('repos')) + + + + c.repo_groups = [('', '')] + c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) + + c.default_user_id = User.by_username('default').user_id + c.in_public_journal = self.sa.query(UserFollowing)\ + .filter(UserFollowing.user_id == c.default_user_id)\ + .filter(UserFollowing.follows_repository == c.repo_info).scalar() + + if c.repo_info.stats: + last_rev = c.repo_info.stats.stat_on_revision + else: + last_rev = 0 + c.stats_revision = last_rev + + c.repo_last_rev = repo.count() - 1 if repo.revisions else 0 + + if last_rev == 0 or c.repo_last_rev == 0: + c.stats_percentage = 0 + else: + c.stats_percentage = '%.2f' % ((float((last_rev)) / + c.repo_last_rev) * 100) + + c.users_array = repo_model.get_users_js() + c.users_groups_array = repo_model.get_users_groups_js() + + defaults = c.repo_info.get_dict() + group, repo_name = c.repo_info.groups_and_repo + defaults['repo_name'] = repo_name + defaults['repo_group'] = getattr(group, 'group_id', None) + #fill owner + if c.repo_info.user: + defaults.update({'user':c.repo_info.user.username}) + else: + replacement_user = self.sa.query(User)\ + .filter(User.admin == True).first().username + defaults.update({'user':replacement_user}) + + + #fill repository users + for p in c.repo_info.repo_to_perm: + defaults.update({'u_perm_%s' % p.user.username: + p.permission.permission_name}) + + #fill repository groups + for p in c.repo_info.users_group_to_perm: + defaults.update({'g_perm_%s' % p.users_group.users_group_name: + p.permission.permission_name}) + + + return defaults + + @HasPermissionAllDecorator('hg.admin') def index(self, format='html'): """GET /repos: All items in the collection""" @@ -78,12 +153,18 @@ class ReposController(BaseController): POST /repos: Create a new item""" # url('repos') repo_model = RepoModel() - _form = RepoForm()() + c.repo_groups = [('', '')] + c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) form_result = {} try: - form_result = _form.to_python(dict(request.POST)) + form_result = RepoForm()(repo_groups=c.repo_groups).to_python(dict(request.POST)) repo_model.create(form_result, c.rhodecode_user) - h.flash(_('created repository %s') % form_result['repo_name'], + if form_result['clone_uri']: + h.flash(_('created repository %s from %s') \ + % (form_result['repo_name'], form_result['clone_uri']), + category='success') + else: + h.flash(_('created repository %s') % form_result['repo_name'], category='success') if request.POST.get('user_created'): @@ -94,7 +175,10 @@ class ReposController(BaseController): form_result['repo_name'], '', self.sa) except formencode.Invalid, errors: + c.new_repo = errors.value['repo_name'] + c.repo_groups = [('', '')] + c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) if request.POST.get('user_created'): r = render('admin/repos/repo_add_create_repository.html') @@ -122,7 +206,8 @@ class ReposController(BaseController): """GET /repos/new: Form to create a new item""" new_repo = request.GET.get('repo', '') c.new_repo = repo_name_slug(new_repo) - + c.repo_groups = [('', '')] + c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) return render('admin/repos/repo_add.html') @HasPermissionAllDecorator('hg.admin') @@ -138,7 +223,6 @@ class ReposController(BaseController): repo_model = RepoModel() changed_name = repo_name _form = RepoForm(edit=True, old_data={'repo_name':repo_name})() - try: form_result = _form.to_python(dict(request.POST)) repo_model.update(repo_name, form_result) @@ -150,34 +234,11 @@ class ReposController(BaseController): changed_name, '', self.sa) except formencode.Invalid, errors: - c.repo_info = repo_model.get_by_repo_name(repo_name) - - if c.repo_info.stats: - last_rev = c.repo_info.stats.stat_on_revision - else: - last_rev = 0 - c.stats_revision = last_rev - repo, dbrepo = ScmModel().get(repo_name, retval='repo') - c.repo_last_rev = repo.count() - 1 if repo.revisions else 0 - - c.default_user_id = User.by_username('default').user_id - c.in_public_journal = self.sa.query(UserFollowing)\ - .filter(UserFollowing.user_id == c.default_user_id)\ - .filter(UserFollowing.follows_repository == c.repo_info).scalar() - - if last_rev == 0: - c.stats_percentage = 0 - else: - c.stats_percentage = '%.2f' % ((float((last_rev)) / - c.repo_last_rev) * 100) - - c.users_array = repo_model.get_users_js() - c.users_groups_array = repo_model.get_users_groups_js() - - errors.value.update({'user':c.repo_info.user.username}) + defaults = self.__load_data(repo_name) + defaults.update(errors.value) return htmlfill.render( render('admin/repos/repo_edit.html'), - defaults=errors.value, + defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8") @@ -186,7 +247,6 @@ class ReposController(BaseController): log.error(traceback.format_exc()) h.flash(_('error occurred during update of repository %s') \ % repo_name, category='error') - return redirect(url('edit_repo', repo_name=changed_name)) @HasPermissionAllDecorator('hg.admin') @@ -325,62 +385,7 @@ class ReposController(BaseController): def edit(self, repo_name, format='html'): """GET /repos/repo_name/edit: Form to edit an existing item""" # url('edit_repo', repo_name=ID) - repo, dbrepo = ScmModel().get(repo_name, retval='repo') - - repo_model = RepoModel() - c.repo_info = repo_model.get_by_repo_name(repo_name) - - if c.repo_info is None: - h.flash(_('%s repository is not mapped to db perhaps' - ' it was created or renamed from the filesystem' - ' please run the application again' - ' in order to rescan repositories') % repo_name, - category='error') - - return redirect(url('repos')) - - c.default_user_id = User.by_username('default').user_id - c.in_public_journal = self.sa.query(UserFollowing)\ - .filter(UserFollowing.user_id == c.default_user_id)\ - .filter(UserFollowing.follows_repository == c.repo_info).scalar() - - if c.repo_info.stats: - last_rev = c.repo_info.stats.stat_on_revision - else: - last_rev = 0 - c.stats_revision = last_rev - - c.repo_last_rev = repo.count() - 1 if repo.revisions else 0 - - if last_rev == 0 or c.repo_last_rev == 0: - c.stats_percentage = 0 - else: - c.stats_percentage = '%.2f' % ((float((last_rev)) / - c.repo_last_rev) * 100) - - c.users_array = repo_model.get_users_js() - c.users_groups_array = repo_model.get_users_groups_js() - - defaults = c.repo_info.get_dict() - - #fill owner - if c.repo_info.user: - defaults.update({'user':c.repo_info.user.username}) - else: - replacement_user = self.sa.query(User)\ - .filter(User.admin == True).first().username - defaults.update({'user':replacement_user}) - - - #fill repository users - for p in c.repo_info.repo_to_perm: - defaults.update({'u_perm_%s' % p.user.username: - p.permission.permission_name}) - - #fill repository groups - for p in c.repo_info.users_group_to_perm: - defaults.update({'g_perm_%s' % p.users_group.users_group_name: - p.permission.permission_name}) + defaults = self.__load_data(repo_name) return htmlfill.render( render('admin/repos/repo_edit.html'), diff --git a/rhodecode/controllers/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -42,7 +42,7 @@ from rhodecode.lib.base import BaseContr from rhodecode.lib.celerylib import tasks, run_task from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \ set_rhodecode_config, repo_name_slug -from rhodecode.model.db import RhodeCodeUi, Repository +from rhodecode.model.db import RhodeCodeUi, Repository, Group from rhodecode.model.forms import UserForm, ApplicationSettingsForm, \ ApplicationUiSettingsForm from rhodecode.model.scm import ScmModel @@ -321,7 +321,8 @@ class SettingsController(BaseController) """GET /_admin/create_repository: Form to create a new item""" new_repo = request.GET.get('repo', '') c.new_repo = repo_name_slug(new_repo) - + c.repo_groups = [('', '')] + c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) return render('admin/repos/repo_add_create_repository.html') def get_hg_ui_settings(self): diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -450,17 +450,21 @@ def PasswordResetForm(): email = All(ValidSystemEmail(), Email(not_empty=True)) return _PasswordResetForm -def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): +def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), + repo_groups=[]): class _RepoForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = False repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), ValidRepoName(edit, old_data)) + clone_uri = UnicodeString(strip=True, min=1, not_empty=False) + repo_group = OneOf(repo_groups) + repo_type = OneOf(supported_backends) description = UnicodeString(strip=True, min=1, not_empty=True) private = StringBoolean(if_missing=False) enable_statistics = StringBoolean(if_missing=False) enable_downloads = StringBoolean(if_missing=False) - repo_type = OneOf(supported_backends) + if edit: #this is repo owner user = All(Int(not_empty=True), ValidRepoUser) diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -234,7 +234,8 @@ class RepoModel(BaseModel): self.sa.add(repo_to_perm) if not just_db: - self.__create_repo(repo_name, form_data['repo_type']) + self.__create_repo(repo_name, form_data['repo_type'], + form_data['clone_uri']) self.sa.commit() @@ -299,7 +300,7 @@ class RepoModel(BaseModel): raise - def __create_repo(self, repo_name, alias): + def __create_repo(self, repo_name, alias, clone_uri=False): """ makes repository on filesystem :param repo_name: @@ -308,9 +309,10 @@ class RepoModel(BaseModel): from rhodecode.lib.utils import check_repo repo_path = os.path.join(self.repos_path, repo_name) if check_repo(repo_name, self.repos_path): - log.info('creating repo %s in %s', repo_name, repo_path) + log.info('creating repo %s in %s @ %s', repo_name, repo_path, + clone_uri) backend = get_backend(alias) - backend(repo_path, create=True) + backend(repo_path, create=True, src_url=clone_uri) def __rename_repo(self, old, new): """ diff --git a/rhodecode/templates/admin/repos/repo_add_base.html b/rhodecode/templates/admin/repos/repo_add_base.html --- a/rhodecode/templates/admin/repos/repo_add_base.html +++ b/rhodecode/templates/admin/repos/repo_add_base.html @@ -14,12 +14,21 @@
- +
- ${h.text('clone_uri',c.new_repo,class_="small")} + ${h.text('clone_uri',class_="small")}
+
+
+ +
+
+ ${h.select('repo_group','',c.repo_groups,class_="medium")} + ${h.link_to(_('add new group'),h.url(''))} +
+
diff --git a/rhodecode/templates/admin/repos/repo_edit.html b/rhodecode/templates/admin/repos/repo_edit.html --- a/rhodecode/templates/admin/repos/repo_edit.html +++ b/rhodecode/templates/admin/repos/repo_edit.html @@ -34,7 +34,24 @@
${h.text('repo_name',class_="medium")}
-
+
+
+
+ +
+
+ ${h.text('clone_uri',class_="small")} +
+
+
+
+ +
+
+ ${h.select('repo_group','',c.repo_groups,class_="medium")} + ${h.link_to(_('add new group'),h.url(''))} +
+
@@ -42,7 +59,7 @@
${h.select('repo_type','hg',c.backends,class_="medium")}
-
+
diff --git a/rhodecode/templates/summary/summary.html b/rhodecode/templates/summary/summary.html --- a/rhodecode/templates/summary/summary.html +++ b/rhodecode/templates/summary/summary.html @@ -65,7 +65,17 @@ ${_('Fork of')} ${c.dbrepo.fork.repo_name} - %endif + %endif + %if c.dbrepo.clone_uri: + + + ${_('remote clone')} + ${_('Clone from')} ${c.dbrepo.clone_uri} + + + %endif