def create_group(path, name, is_collection=False): hgweb = HGWeb(settings.HGWEB_CONFIG) groups = hgweb.get_groups() if not groups or not ( name in zip(*groups)[0] ): # zip(*groups)[0] - groups is a list of tuples, so unzip it and take list of keys try: if not is_collection: _path = path.rstrip('*') if not os.path.exists(_path): os.makedirs(_path) #may be OSError hgweb.add_paths(name, path) #may be IOError else: if not os.path.exists(path): os.makedirs(path) #may be OSError hgweb.add_collections(name, path) #may be IOError except (OSError, IOError) as e: raise RepositoryException("Error: %s" % str(e)) else: raise RepositoryException( _("There is already a group with such a name."))
def repo(request, repo_path): hgweb = HGWeb(settings.HGWEB_CONFIG) tree = prepare_tree( modhg.repository.get_tree(hgweb.get_paths(), hgweb.get_collections())) is_raw_mode = False full_repository_path = repository.get_absolute_repository_path(repo_path) hgrc_path = os.path.join(full_repository_path, ".hg", "hgrc") _check_access_local_hgrc(request, hgrc_path) groups = hgweb.get_groups() hgweb_cfg_hash = md5_for_file(settings.HGWEB_CONFIG) edit_repo_form = CreateRepoForm(default_groups=groups, file_hash=hgweb_cfg_hash, initial={ "name": os.path.split(full_repository_path)[1], "group": repository.get_group(repo_path), "file_hash": hgweb_cfg_hash }) hgrc = HGWeb(hgrc_path, True) def _hgweb_or_hgrc_list(hgweb, hgrc, param_name): """ checks if user list is not specified in the hgrc, tries to read it from hgweb """ _list = _split_users(hgrc.get_web_key(param_name)) if not _list: _list = _split_users(hgweb.get_web_key(param_name)) return _list allow_read_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'allow_read') allow_push_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'allow_push') deny_read_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'deny_read') deny_push_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'deny_push') user_list = users.login_list(settings.AUTH_FILE) add_to_allow_read_list = [ val for val in user_list if val.strip() not in allow_read_list ] add_to_allow_push_list = [ val for val in user_list if val.strip() not in allow_push_list ] add_to_deny_read_list = [ val for val in user_list if val.strip() not in deny_read_list ] add_to_deny_push_list = [ val for val in user_list if val.strip() not in deny_push_list ] model = { "tree": tree, "global": False, "repo_path": repo_path, "add_to_allow_read_list": add_to_allow_read_list, "add_to_allow_push_list": add_to_allow_push_list, "add_to_deny_read_list": add_to_deny_read_list, "add_to_deny_push_list": add_to_deny_push_list, "allow_read_list": allow_read_list, "allow_push_list": allow_push_list, "deny_read_list": deny_read_list, "deny_push_list": deny_push_list } local_hgrc_hash = md5_for_file(hgrc_path) delete_repo_form = FileHashForm(local_hgrc_hash) repo_field_delete_form = FileHashForm(local_hgrc_hash) form = RepositoryForm(local_hgrc_hash) with open(hgrc_path, 'r') as f: hgrc_content = f.read() raw_mode_form = RawModeForm(local_hgrc_hash, initial={ "hgrc": hgrc_content, "file_hash": local_hgrc_hash }) if request.method == 'POST' and hgrc is not None: if 'save' in request.POST: form = RepositoryForm(local_hgrc_hash, request.POST) if form.is_valid(): form.export_values(hgrc, request.POST) messages.success(request, _("Repository settings saved successfully.")) return HttpResponseRedirect( reverse("repository", args=[repo_path])) elif 'delete_field' in request.POST: repo_field_delete_form = FileHashForm(local_hgrc_hash, request.POST) if repo_field_delete_form.is_valid(): parameter = request.POST.get('parameter') hgrc.del_web_key(parameter) return HttpResponseRedirect( reverse("repository", args=[repo_path])) elif 'sure_delete' in request.POST: delete_repo_form = FileHashForm(local_hgrc_hash, request.POST) if delete_repo_form.is_valid(): try: repository.delete(full_repository_path, repo_path) messages.success( request, _("Repository '%s' deleted successfully.") % repo_path) except RepositoryException as e: messages.warning( request, _("Repository '%(repo)s' was not deleted, cause: %(cause)." ) % { "repo": repo_path, "cause": unicode(e) }) return HttpResponseRedirect(reverse("index")) elif 'save_repo' in request.POST: edit_repo_form = CreateRepoForm(groups, hgweb_cfg_hash, request.POST) if edit_repo_form.is_valid(): return edit_repo_form.rename(request, repo_path, groups, full_repository_path) elif 'raw_save' in request.POST: is_raw_mode = True raw_mode_form = RawModeForm(local_hgrc_hash, request.POST) if raw_mode_form.is_valid(): with open(hgrc_path, 'w') as f: f.write(raw_mode_form.cleaned_data['hgrc']) messages.success(request, _("Repository settings saved successfully.")) return HttpResponseRedirect( reverse("repository", args=[repo_path])) # finally # local hgrc might be changed, recreate dependence forms local_hgrc_hash = md5_for_file(hgrc_path) repo_field_delete_form = FileHashForm(local_hgrc_hash) delete_repo_form = FileHashForm(local_hgrc_hash) # raw_mode form may have _errors set, so just update data in it with open(hgrc_path, 'r') as f: hgrc_content = f.read() raw_mode_form.data['hgrc'] = hgrc_content raw_mode_form.data['file_hash'] = local_hgrc_hash # re-set errors if any occurs in the is_valid method. errors = form._errors form = RepositoryForm(local_hgrc_hash) form._errors = errors form.set_default(hgweb, hgrc) model["form"] = form model["repo_field_delete_form"] = repo_field_delete_form model["delete_repo_form"] = delete_repo_form model["repo_form"] = edit_repo_form model["raw_mode_form"] = raw_mode_form model["is_raw_mode"] = is_raw_mode model["hgweb_repo_url"] = _hgweb_repo_url( full_repository_path) if _is_collection( repo_path, hgweb) else _hgweb_repo_url(repo_path) return model
def index(request): hgweb = HGWeb(settings.HGWEB_CONFIG) collections = hgweb.get_collections() _tree = modhg.repository.get_tree(hgweb.get_paths(), collections) tree = prepare_tree(_tree) groups = hgweb.get_groups() # unzipping collections unzipped_collections = zip(*hgweb.get_collections()) if unzipped_collections: collection_names = unzipped_collections[0] else: collection_names = [] hgweb_cfg_hash = md5_for_file(settings.HGWEB_CONFIG) create_repo_form = CreateRepoForm(groups, hgweb_cfg_hash) groups_form = ManageGroupsForm(hgweb_cfg_hash) delete_group_form = DeleteGroupForm(hgweb_cfg_hash) EDIT_GROUP_FORM_PREFIX = "edit_group" edit_group_form = ManageGroupsForm(hgweb_cfg_hash, prefix=EDIT_GROUP_FORM_PREFIX) model = { "tree": tree, "groups": _ext_groups_with_amount_of_repos_and_collection_flag( groups, collection_names, _tree), "default_path": settings.REPOSITORIES_ROOT } if request.method == 'POST': if "create_group" in request.POST: groups_form = ManageGroupsForm(hgweb_cfg_hash, request.POST) if groups_form.is_valid(): return groups_form.create_group(request) elif "create_repo" in request.POST: create_repo_form = CreateRepoForm(groups, hgweb_cfg_hash, request.POST) if create_repo_form.is_valid(): return create_repo_form.create_repository(request, groups) elif "delete_group" in request.POST: delete_group_form = DeleteGroupForm(hgweb_cfg_hash, request.POST) if delete_group_form.is_valid(): return delete_group_form.delete_group(request, groups) elif "old_group_name" in request.POST: # edit group request edit_group_form = ManageGroupsForm(hgweb_cfg_hash, request.POST, prefix=EDIT_GROUP_FORM_PREFIX) if edit_group_form.is_valid(): return edit_group_form.edit_group(request, groups, hgweb) else: old_name = request.POST.get("old_group_name") old_path = request.POST.get("old_group_path") model["old_group_name"] = old_name model["old_group_path"] = old_path model["groups_form"] = groups_form model["edit_group_form"] = edit_group_form model["repo_form"] = create_repo_form model["delete_group_form"] = delete_group_form return model