Exemplo n.º 1
0
    def diff_2way(self, repo_name, f_path):
        diff1 = request.GET.get('diff1', '')
        diff2 = request.GET.get('diff2', '')

        nodes = []
        unknown_commits = []
        for commit in [diff1, diff2]:
            try:
                nodes.append(self._get_file_node(commit, f_path))
            except (RepositoryError, NodeError):
                log.exception('%(commit)s does not exist' % {'commit': commit})
                unknown_commits.append(commit)
                h.flash(h.literal(
                    _('Commit %(commit)s does not exist.') %
                    {'commit': commit}),
                        category='error')

        if unknown_commits:
            return redirect(
                url('files_home', repo_name=c.repo_name, f_path=f_path))

        if all(isinstance(node.commit, EmptyCommit) for node in nodes):
            raise HTTPNotFound

        node1, node2 = nodes

        f_gitdiff = diffs.get_gitdiff(node1, node2, ignore_whitespace=False)
        diff_processor = diffs.DiffProcessor(f_gitdiff, format='gitdiff')
        diff_data = diff_processor.prepare()

        if not diff_data or diff_data[0]['raw_diff'] == '':
            h.flash(h.literal(
                _('%(file_path)s has not changed '
                  'between %(commit_1)s and %(commit_2)s.') % {
                      'file_path': f_path,
                      'commit_1': node1.commit.id,
                      'commit_2': node2.commit.id
                  }),
                    category='error')
            return redirect(
                url('files_home', repo_name=c.repo_name, f_path=f_path))

        c.diff_data = diff_data[0]
        c.FID = h.FID(diff2, node2.path)
        # cleanup some unneeded data
        del c.diff_data['raw_diff']
        del c.diff_data['chunks']

        c.node1 = node1
        c.commit_1 = node1.commit
        c.node2 = node2
        c.commit_2 = node2.commit

        return render('files/diff_2way.html')
Exemplo n.º 2
0
    def fork_create(self, repo_name):
        self.__load_defaults()
        c.repo_info = Repository.get_by_repo_name(repo_name)
        _form = RepoForkForm(old_data={'repo_type': c.repo_info.repo_type},
                             repo_groups=c.repo_groups_choices,
                             landing_revs=c.landing_revs_choices)()
        form_result = {}
        try:
            form_result = _form.to_python(dict(request.POST))

            # an approximation that is better than nothing
            if not RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active:
                form_result['update_after_clone'] = False

            # create fork is done sometimes async on celery, db transaction
            # management is handled there.
            RepoModel().create_fork(form_result, self.rhodecode_user.user_id)
            fork_url = h.link_to(
                form_result['repo_name_full'],
                h.url('summary_home', repo_name=form_result['repo_name_full']))

            h.flash(h.literal(_('Forked repository %s as %s') \
                      % (repo_name, fork_url)),
                    category='success')
        except formencode.Invalid, errors:
            c.new_repo = errors.value['repo_name']

            return htmlfill.render(render('forks/fork.html'),
                                   defaults=errors.value,
                                   errors=errors.error_dict or {},
                                   prefix_error=False,
                                   encoding="UTF-8")
Exemplo n.º 3
0
    def create(self):
        """POST /users: Create a new item"""
        # url('users')
        c.default_extern_type = auth_rhodecode.RhodeCodeAuthPlugin.name
        user_model = UserModel()
        user_form = UserForm()()
        try:
            form_result = user_form.to_python(dict(request.POST))
            user = user_model.create(form_result)
            Session().flush()
            username = form_result['username']
            action_logger(c.rhodecode_user, 'admin_created_user:%s' % username,
                          None, self.ip_addr, self.sa)

            user_link = h.link_to(h.escape(username),
                                  url('edit_user', user_id=user.user_id))
            h.flash(h.literal(
                _('Created user %(user_link)s') % {'user_link': user_link}),
                    category='success')
            Session().commit()
        except formencode.Invalid as errors:
            return htmlfill.render(render('admin/users/user_add.html'),
                                   defaults=errors.value,
                                   errors=errors.error_dict or {},
                                   prefix_error=False,
                                   encoding="UTF-8",
                                   force_defaults=False)
        except UserCreationError as e:
            h.flash(e, 'error')
        except Exception:
            log.exception("Exception creation of user")
            h.flash(_('Error occurred during creation of user %s') %
                    request.POST.get('username'),
                    category='error')
        return redirect(url('users'))
Exemplo n.º 4
0
    def fork_create(self, repo_name):
        self.__load_defaults()
        c.repo_info = Repository.get_by_repo_name(repo_name)
        _form = RepoForkForm(old_data={'repo_type': c.repo_info.repo_type},
                             repo_groups=c.repo_groups_choices,
                             landing_revs=c.landing_revs_choices)()
        form_result = {}
        try:
            form_result = _form.to_python(dict(request.POST))

            # an approximation that is better than nothing
            if not RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active:
                form_result['update_after_clone'] = False

            # create fork is done sometimes async on celery, db transaction
            # management is handled there.
            RepoModel().create_fork(form_result, self.rhodecode_user.user_id)
            fork_url = h.link_to(form_result['repo_name_full'],
                    h.url('summary_home', repo_name=form_result['repo_name_full']))

            h.flash(h.literal(_('Forked repository %s as %s') \
                      % (repo_name, fork_url)),
                    category='success')
        except formencode.Invalid, errors:
            c.new_repo = errors.value['repo_name']

            return htmlfill.render(
                render('forks/fork.html'),
                defaults=errors.value,
                errors=errors.error_dict or {},
                prefix_error=False,
                encoding="UTF-8")
Exemplo n.º 5
0
    def __wrapper(self, func, *fargs, **fkwargs):
        cls = fargs[0]
        user = cls.rhodecode_user
        loc = "%s:%s" % (cls.__class__.__name__, func.__name__)

        #check IP
        ip_access_ok = True
        if not user.ip_allowed:
            from rhodecode.lib import helpers as h
            h.flash(h.literal(_('IP %s not allowed' % (user.ip_addr))),
                    category='warning')
            ip_access_ok = False

        api_access_ok = False
        if self.api_access:
            log.debug('Checking API KEY access for %s' % cls)
            if user.api_key == request.GET.get('api_key'):
                api_access_ok = True
            else:
                log.debug("API KEY token not valid")

        log.debug('Checking if %s is authenticated @ %s' %
                  (user.username, loc))
        if (user.is_authenticated or api_access_ok) and ip_access_ok:
            reason = 'RegularAuth' if user.is_authenticated else 'APIAuth'
            log.info('user %s is authenticated and granted access to %s '
                     'using %s' % (user.username, loc, reason))
            return func(*fargs, **fkwargs)
        else:
            log.warn('user %s NOT authenticated on func: %s' % (user, loc))
            p = url.current()

            log.debug('redirecting to login page with %s' % p)
            return redirect(url('login_home', came_from=p))
Exemplo n.º 6
0
    def _get_commit_or_redirect(self,
                                ref,
                                ref_type,
                                repo,
                                redirect_after=True,
                                partial=False):
        """
        This is a safe way to get a commit. If an error occurs it
        redirects to a commit with a proper message. If partial is set
        then it does not do redirect raise and throws an exception instead.
        """
        try:
            return get_commit_from_ref_name(repo, safe_str(ref), ref_type)
        except EmptyRepositoryError:
            if not redirect_after:
                return repo.scm_instance().EMPTY_COMMIT
            h.flash(h.literal(_('There are no commits yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=repo.repo_name))

        except RepositoryError as e:
            msg = safe_str(e)
            log.exception(msg)
            h.flash(msg, category='warning')
            if not partial:
                redirect(h.url('summary_home', repo_name=repo.repo_name))
            raise HTTPBadRequest()
Exemplo n.º 7
0
    def __get_rev_or_redirect(self, ref, repo, redirect_after=True,
                             partial=False):
        """
        Safe way to get changeset if error occur it redirects to changeset with
        proper message. If partial is set then don't do redirect raise Exception
        instead

        :param rev: revision to fetch
        :param repo: repo instance
        """

        rev = ref[1] # default and used for git
        if repo.scm_instance.alias == 'hg':
            # lookup up the exact node id
            _revset_predicates = {
                    'branch': 'branch',
                    'book': 'bookmark',
                    'tag': 'tag',
                    'rev': 'id',
                }
            rev_spec = "max(%s(%%s))" % _revset_predicates[ref[0]]
            revs = repo.scm_instance._repo.revs(rev_spec, safe_str(ref[1]))
            if revs:
                rev = revs[-1]
            # else: TODO: just report 'not found'

        try:
            return repo.scm_instance.get_changeset(rev).raw_id
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=repo.repo_name))
Exemplo n.º 8
0
    def __get_commit_or_redirect(self,
                                 commit_id,
                                 repo,
                                 redirect_after=True,
                                 partial=False):
        """
        This is a safe way to get a commit. If an error occurs it
        redirects to a commit with a proper message. If partial is set
        then it does not do redirect raise and throws an exception instead.

        :param commit_id: commit to fetch
        :param repo: repo instance
        """
        try:
            return c.rhodecode_repo.get_commit(commit_id)
        except EmptyRepositoryError:
            if not redirect_after:
                return None
            h.flash(h.literal(_('There are no commits yet')),
                    category='warning')
            redirect(url('changelog_home', repo_name=repo.repo_name))
        except RepositoryError as e:
            msg = safe_str(e)
            log.exception(msg)
            h.flash(msg, category='warning')
            if not partial:
                redirect(h.url('changelog_home', repo_name=repo.repo_name))
            raise HTTPBadRequest()
Exemplo n.º 9
0
    def index(self):
        org_repo = c.rhodecode_db_repo

        if org_repo.scm_instance.alias != 'hg':
            log.error('Review not available for GIT REPOS')
            raise HTTPNotFound

        try:
            org_repo.scm_instance.get_changeset()
        except EmptyRepositoryError, e:
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=org_repo.repo_name))
Exemplo n.º 10
0
    def index(self):
        org_repo = c.rhodecode_db_repo

        if org_repo.scm_instance.alias != 'hg':
            log.error('Review not available for GIT REPOS')
            raise HTTPNotFound

        try:
            org_repo.scm_instance.get_changeset()
        except EmptyRepositoryError, e:
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=org_repo.repo_name))
Exemplo n.º 11
0
    def index(self):
        source_repo = c.rhodecode_db_repo

        try:
            source_repo.scm_instance().get_commit()
        except EmptyRepositoryError:
            h.flash(h.literal(_('There are no commits yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=source_repo.repo_name))

        commit_id = request.GET.get('commit')
        branch_ref = request.GET.get('branch')
        bookmark_ref = request.GET.get('bookmark')

        try:
            source_repo_data = PullRequestModel().generate_repo_data(
                source_repo,
                commit_id=commit_id,
                branch=branch_ref,
                bookmark=bookmark_ref)
        except CommitDoesNotExistError as e:
            log.exception(e)
            h.flash(_('Commit does not exist'), 'error')
            redirect(url('pullrequest_home', repo_name=source_repo.repo_name))

        default_target_repo = source_repo
        if (source_repo.parent
                and not source_repo.parent.scm_instance().is_empty()):
            # change default if we have a parent repo
            default_target_repo = source_repo.parent

        target_repo_data = PullRequestModel().generate_repo_data(
            default_target_repo)

        selected_source_ref = source_repo_data['refs']['selected_ref']

        title_source_ref = selected_source_ref.split(':', 2)[1]
        c.default_title = PullRequestModel().generate_pullrequest_title(
            source=source_repo.repo_name,
            source_ref=title_source_ref,
            target=default_target_repo.repo_name)

        c.default_repo_data = {
            'source_repo_name': source_repo.repo_name,
            'source_refs_json': json.dumps(source_repo_data),
            'target_repo_name': default_target_repo.repo_name,
            'target_refs_json': json.dumps(target_repo_data),
        }
        c.default_source_ref = selected_source_ref

        return render('/pullrequests/pullrequest.html')
Exemplo n.º 12
0
    def repo_check(self, repo_name):
        c.repo = repo_name
        task_id = request.GET.get('task_id')

        if task_id and task_id not in ['None']:
            import rhodecode
            from celery.result import AsyncResult
            if rhodecode.CELERY_ENABLED:
                task = AsyncResult(task_id)
                if task.failed():
                    msg = self._log_creation_exception(task.result, c.repo)
                    h.flash(msg, category='error')
                    return redirect(url('home'), code=501)

        repo = Repository.get_by_repo_name(repo_name)
        if repo and repo.repo_state == Repository.STATE_CREATED:
            if repo.clone_uri:
                clone_uri = repo.clone_uri_hidden
                h.flash(_('Created repository %s from %s') %
                        (repo.repo_name, clone_uri),
                        category='success')
            else:
                repo_url = h.link_to(
                    repo.repo_name,
                    h.url('summary_home', repo_name=repo.repo_name))
                fork = repo.fork
                if fork:
                    fork_name = fork.repo_name
                    h.flash(h.literal(
                        _('Forked repository %s as %s') %
                        (fork_name, repo_url)),
                            category='success')
                else:
                    h.flash(h.literal(_('Created repository %s') % repo_url),
                            category='success')
            return {'result': True}
        return {'result': False}
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    def create(self):
        """POST /repo_groups: Create a new item"""
        # url('repo_groups')

        parent_group_id = safe_int(request.POST.get('group_parent_id'))
        can_create = self._can_create_repo_group(parent_group_id)

        self.__load_defaults()
        # permissions for can create group based on parent_id are checked
        # here in the Form
        available_groups = map(lambda k: unicode(k[0]), c.repo_groups)
        repo_group_form = RepoGroupForm(available_groups=available_groups,
                                        can_create_in_root=can_create)()
        try:
            owner = c.rhodecode_user
            form_result = repo_group_form.to_python(dict(request.POST))
            RepoGroupModel().create(
                group_name=form_result['group_name_full'],
                group_description=form_result['group_description'],
                owner=owner.user_id,
                copy_permissions=form_result['group_copy_permissions'])
            Session().commit()
            _new_group_name = form_result['group_name_full']
            repo_group_url = h.link_to(
                _new_group_name,
                h.url('repo_group_home', group_name=_new_group_name))
            h.flash(h.literal(
                _('Created repository group %s') % repo_group_url),
                    category='success')
            # TODO: in futureaction_logger(, '', '', '', self.sa)
        except formencode.Invalid as errors:
            return htmlfill.render(
                render('admin/repo_groups/repo_group_add.html'),
                defaults=errors.value,
                errors=errors.error_dict or {},
                prefix_error=False,
                encoding="UTF-8",
                force_defaults=False)
        except Exception:
            log.exception("Exception during creation of repository group")
            h.flash(
                _('Error occurred during creation of repository group %s') %
                request.POST.get('group_name'),
                category='error')

        # TODO: maybe we should get back to the main view, not the admin one
        return redirect(url('repo_groups', parent_group=parent_group_id))
Exemplo n.º 15
0
    def __get_cs_or_redirect(self, rev, repo_name, redirect_after=True):
        """
        Safe way to get changeset if error occur it redirects to tip with
        proper message

        :param rev: revision to fetch
        :param repo_name: repo name to redirect after
        """

        try:
            return c.rhodecode_repo.get_changeset(rev)
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            url_ = url("files_add_home", repo_name=c.repo_name, revision=0, f_path="")
            add_new = h.link_to(_("Click here to add new file"), url_)
            h.flash(h.literal(_("There are no files yet %s") % add_new), category="warning")
            redirect(h.url("summary_home", repo_name=repo_name))
Exemplo n.º 16
0
    def __get_cs_or_redirect(self, rev, repo, redirect_after=True,
                             partial=False):
        """
        Safe way to get changeset if error occur it redirects to changeset with
        proper message. If partial is set then don't do redirect raise Exception
        instead

        :param rev: revision to fetch
        :param repo: repo instance
        """

        try:
            return c.rhodecode_repo.get_changeset(rev)
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('changelog_home', repo_name=repo.repo_name))
Exemplo n.º 17
0
    def create(self):
        """
        POST /repos: Create a new item"""
        # url('repos')

        self.__load_defaults()
        form_result = {}
        try:
            form_result = RepoForm(repo_groups=c.repo_groups_choices,
                                   landing_revs=c.landing_revs_choices)()\
                            .to_python(dict(request.POST))

            new_repo = RepoModel().create(form_result,
                                          self.rhodecode_user.user_id)
            if form_result['clone_uri']:
                h.flash(_('Created repository %s from %s') \
                    % (form_result['repo_name'], form_result['clone_uri']),
                    category='success')
            else:
                repo_url = h.link_to(
                    form_result['repo_name'],
                    h.url('summary_home',
                          repo_name=form_result['repo_name_full']))
                h.flash(h.literal(_('Created repository %s') % repo_url),
                        category='success')

            if request.POST.get('user_created'):
                # created by regular non admin user
                action_logger(self.rhodecode_user, 'user_created_repo',
                              form_result['repo_name_full'], self.ip_addr,
                              self.sa)
            else:
                action_logger(self.rhodecode_user, 'admin_created_repo',
                              form_result['repo_name_full'], self.ip_addr,
                              self.sa)
            Session().commit()
        except formencode.Invalid, errors:
            return htmlfill.render(render('admin/repos/repo_add.html'),
                                   defaults=errors.value,
                                   errors=errors.error_dict or {},
                                   prefix_error=False,
                                   encoding="UTF-8")
Exemplo n.º 18
0
    def create(self):
        """POST /users_groups: Create a new item"""
        # url('users_groups')

        users_group_form = UserGroupForm()()
        try:
            form_result = users_group_form.to_python(dict(request.POST))
            user_group = UserGroupModel().create(
                name=form_result['users_group_name'],
                description=form_result['user_group_description'],
                owner=c.rhodecode_user.user_id,
                active=form_result['users_group_active'])
            Session().flush()

            user_group_name = form_result['users_group_name']
            action_logger(c.rhodecode_user,
                          'admin_created_users_group:%s' % user_group_name,
                          None, self.ip_addr, self.sa)
            user_group_link = h.link_to(
                h.escape(user_group_name),
                url('edit_users_group',
                    user_group_id=user_group.users_group_id))
            h.flash(h.literal(
                _('Created user group %(user_group_link)s') %
                {'user_group_link': user_group_link}),
                    category='success')
            Session().commit()
        except formencode.Invalid as errors:
            return htmlfill.render(
                render('admin/user_groups/user_group_add.html'),
                defaults=errors.value,
                errors=errors.error_dict or {},
                prefix_error=False,
                encoding="UTF-8",
                force_defaults=False)
        except Exception:
            log.exception("Exception creating user group")
            h.flash(_('Error occurred during creation of user group %s') \
                    % request.POST.get('users_group_name'), category='error')

        return redirect(
            url('edit_users_group', user_group_id=user_group.users_group_id))
Exemplo n.º 19
0
    def create_repository(self):
        """GET /_admin/create_repository: Form to create a new item"""

        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)

        new_repo = request.GET.get('repo', '')
        c.new_repo = repo_name_slug(new_repo)

        return render('admin/repos/repo_add_create_repository.html')
Exemplo n.º 20
0
    def create(self):
        """
        POST /repos: Create a new item"""
        # url('repos')

        self.__load_defaults()
        form_result = {}
        try:
            form_result = RepoForm(repo_groups=c.repo_groups_choices,
                                   landing_revs=c.landing_revs_choices)()\
                            .to_python(dict(request.POST))

            new_repo = RepoModel().create(form_result,
                                          self.rhodecode_user.user_id)
            if form_result['clone_uri']:
                h.flash(_('Created repository %s from %s') \
                    % (form_result['repo_name'], form_result['clone_uri']),
                    category='success')
            else:
                repo_url = h.link_to(form_result['repo_name'],
                    h.url('summary_home', repo_name=form_result['repo_name_full']))
                h.flash(h.literal(_('Created repository %s') % repo_url),
                        category='success')

            if request.POST.get('user_created'):
                # created by regular non admin user
                action_logger(self.rhodecode_user, 'user_created_repo',
                              form_result['repo_name_full'], self.ip_addr,
                              self.sa)
            else:
                action_logger(self.rhodecode_user, 'admin_created_repo',
                              form_result['repo_name_full'], self.ip_addr,
                              self.sa)
            Session().commit()
        except formencode.Invalid, errors:
            return htmlfill.render(
                render('admin/repos/repo_add.html'),
                defaults=errors.value,
                errors=errors.error_dict or {},
                prefix_error=False,
                encoding="UTF-8")
Exemplo n.º 21
0
    def __wrapper(self, func, *fargs, **fkwargs):
        cls = fargs[0]
        user = cls.rhodecode_user
        loc = "%s:%s" % (cls.__class__.__name__, func.__name__)
        # defined whitelist of controllers which API access will be enabled
        whitelist = aslist(config.get('api_access_controllers_whitelist'),
                           sep=',')
        api_access_whitelist = loc in whitelist
        log.debug('loc:%s is in API whitelist:%s:%s' % (loc, whitelist,
                                                        api_access_whitelist))
        #check IP
        ip_access_ok = True
        if not user.ip_allowed:
            from rhodecode.lib import helpers as h
            h.flash(h.literal(_('IP %s not allowed' % (user.ip_addr))),
                    category='warning')
            ip_access_ok = False

        api_access_ok = False
        if self.api_access or api_access_whitelist:
            log.debug('Checking API KEY access for %s' % cls)
            if user.api_key == request.GET.get('api_key'):
                api_access_ok = True
            else:
                log.debug("API KEY token not valid")

        log.debug('Checking if %s is authenticated @ %s' % (user.username, loc))
        if (user.is_authenticated or api_access_ok) and ip_access_ok:
            reason = 'RegularAuth' if user.is_authenticated else 'APIAuth'
            log.info('user %s is authenticated and granted access to %s '
                     'using %s' % (user.username, loc, reason)
            )
            return func(*fargs, **fkwargs)
        else:
            log.warn('user %s NOT authenticated on func: %s' % (
                user, loc)
            )
            p = url.current()

            log.debug('redirecting to login page with %s' % p)
            return redirect(url('login_home', came_from=p))
Exemplo n.º 22
0
    def __get_cs_or_redirect(self, rev, repo_name, redirect_after=True):
        """
        Safe way to get changeset if error occur it redirects to tip with
        proper message

        :param rev: revision to fetch
        :param repo_name: repo name to redirect after
        """

        try:
            return c.rhodecode_repo.get_changeset(rev)
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            url_ = url('files_add_home',
                       repo_name=c.repo_name,
                       revision=0, f_path='')
            add_new = '<a href="%s">[%s]</a>' % (url_, _('click here to add new file'))
            h.flash(h.literal(_('There are no files yet %s') % add_new),
                    category='warning')
            redirect(h.url('summary_home', repo_name=repo_name))
Exemplo n.º 23
0
    def __get_cs_or_redirect(self, rev, repo_name, redirect_after=True):
        """
        Safe way to get changeset if error occur it redirects to tip with
        proper message

        :param rev: revision to fetch
        :param repo_name: repo name to redirect after
        """

        try:
            return c.rhodecode_repo.get_changeset(rev)
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            url_ = url('files_add_home',
                       repo_name=c.repo_name,
                       revision=0, f_path='')
            add_new = h.link_to(_('Click here to add new file'), url_)
            h.flash(h.literal(_('There are no files yet %s') % add_new),
                    category='warning')
            redirect(h.url('summary_home', repo_name=repo_name))
Exemplo n.º 24
0
    def __get_commit_or_redirect(self,
                                 commit_id,
                                 repo_name,
                                 redirect_after=True):
        """
        This is a safe way to get commit. If an error occurs it redirects to
        tip with proper message

        :param commit_id: id of commit to fetch
        :param repo_name: repo name to redirect after
        :param redirect_after: toggle redirection
        """
        try:
            return c.rhodecode_repo.get_commit(commit_id)
        except EmptyRepositoryError:
            if not redirect_after:
                return None
            url_ = url('files_add_home',
                       repo_name=c.repo_name,
                       revision=0,
                       f_path='',
                       anchor='edit')
            if h.HasRepoPermissionAny('repository.write',
                                      'repository.admin')(c.repo_name):
                add_new = h.link_to(_('Click here to add a new file.'),
                                    url_,
                                    class_="alert-link")
            else:
                add_new = ""
            h.flash(h.literal(_('There are no files yet. %s') % add_new),
                    category='warning')
            redirect(h.url('summary_home', repo_name=repo_name))
        except (CommitDoesNotExistError, LookupError):
            msg = _('No such commit exists for this repository')
            h.flash(msg, category='error')
            raise HTTPNotFound()
        except RepositoryError as e:
            h.flash(safe_str(e), category='error')
            raise HTTPNotFound()
Exemplo n.º 25
0
    def __get_cs_or_redirect(self,
                             rev,
                             repo,
                             redirect_after=True,
                             partial=False):
        """
        Safe way to get changeset if error occur it redirects to changeset with
        proper message. If partial is set then don't do redirect raise Exception
        instead

        :param rev: revision to fetch
        :param repo: repo instance
        """

        try:
            return c.rhodecode_repo.get_changeset(rev)
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('changelog_home', repo_name=repo.repo_name))
Exemplo n.º 26
0
    def __get_rev_or_redirect(self,
                              ref,
                              repo,
                              redirect_after=True,
                              partial=False):
        """
        Safe way to get changeset if error occur it redirects to changeset with
        proper message. If partial is set then don't do redirect raise Exception
        instead

        :param rev: revision to fetch
        :param repo: repo instance
        """

        rev = ref[1]  # default and used for git
        if repo.scm_instance.alias == 'hg':
            # lookup up the exact node id
            _revset_predicates = {
                'branch': 'branch',
                'book': 'bookmark',
                'tag': 'tag',
                'rev': 'id',
            }
            rev_spec = "max(%s(%%s))" % _revset_predicates[ref[0]]
            revs = repo.scm_instance._repo.revs(rev_spec, safe_str(ref[1]))
            if revs:
                rev = revs[-1]
            # else: TODO: just report 'not found'

        try:
            return repo.scm_instance.get_changeset(rev).raw_id
        except EmptyRepositoryError, e:
            if not redirect_after:
                return None
            h.flash(h.literal(_('There are no changesets yet')),
                    category='warning')
            redirect(url('summary_home', repo_name=repo.repo_name))
Exemplo n.º 27
0
    def settings_system(self):
        """GET /admin/settings/system: All items in the collection"""
        # url('admin_settings_system')
        snapshot = str2bool(request.GET.get('snapshot'))
        c.active = 'system'

        defaults = self._form_defaults()
        c.rhodecode_ini = rhodecode.CONFIG
        c.rhodecode_update_url = defaults.get('rhodecode_update_url')
        server_info = ScmModel().get_server_info(request.environ)
        for key, val in server_info.iteritems():
            setattr(c, key, val)

        if c.disk['percent'] > 90:
            h.flash(
                h.literal(
                    _('Critical: your disk space is very low <b>%s%%</b> used'
                      % c.disk['percent'])), 'error')
        elif c.disk['percent'] > 70:
            h.flash(
                h.literal(
                    _('Warning: your disk space is running low <b>%s%%</b> used'
                      % c.disk['percent'])), 'warning')

        try:
            c.uptime_age = h._age(h.time_to_datetime(c.boot_time),
                                  False,
                                  show_suffix=False)
        except TypeError:
            c.uptime_age = c.boot_time

        try:
            c.system_memory = '%s/%s, %s%% (%s%%) used%s' % (
                h.format_byte_size_binary(c.memory['used']),
                h.format_byte_size_binary(c.memory['total']),
                c.memory['percent2'], c.memory['percent'],
                ' %s' % c.memory['error'] if 'error' in c.memory else '')
        except TypeError:
            c.system_memory = 'NOT AVAILABLE'

        rhodecode_ini_safe = rhodecode.CONFIG.copy()
        blacklist = [
            'rhodecode_license_key', 'routes.map', 'pylons.h',
            'pylons.app_globals', 'pylons.environ_config',
            'sqlalchemy.db1.url', ('app_conf', 'sqlalchemy.db1.url')
        ]
        for k in blacklist:
            if isinstance(k, tuple):
                section, key = k
                if section in rhodecode_ini_safe:
                    rhodecode_ini_safe[section].pop(key, None)
            else:
                rhodecode_ini_safe.pop(k, None)

        c.rhodecode_ini_safe = rhodecode_ini_safe

        # TODO: marcink, figure out how to allow only selected users to do this
        c.allowed_to_snapshot = False

        if snapshot:
            if c.allowed_to_snapshot:
                return render('admin/settings/settings_system_snapshot.html')
            else:
                h.flash('You are not allowed to do this', category='warning')

        return htmlfill.render(render('admin/settings/settings.html'),
                               defaults=defaults,
                               encoding="UTF-8",
                               force_defaults=False)