view kallithea/controllers/home.py @ 6553:e9ac5698281d

tg: minimize future diff by some mocking and replacing some pylons imports with tg No actual tg dependency yet, just a temporary hack faking tg as an alias for pylons. Based on work by Alessandro Molina.
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 24 Dec 2016 01:27:47 +0100
parents 33b71a130b16
children 2f9313074853
line wrap: on
line source

# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.controllers.home
~~~~~~~~~~~~~~~~~~~~~~~~~~

Home controller for Kallithea

This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: Feb 18, 2010
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.

"""

import logging

from tg import tmpl_context as c, request
from tg.i18n import ugettext as _
from webob.exc import HTTPBadRequest
from sqlalchemy.sql.expression import func

from kallithea.lib.utils import conditional_cache
from kallithea.lib.auth import LoginRequired, HasRepoPermissionLevelDecorator
from kallithea.lib.base import BaseController, render, jsonify
from kallithea.model.db import Repository, RepoGroup
from kallithea.model.repo import RepoModel


log = logging.getLogger(__name__)


class HomeController(BaseController):

    def __before__(self):
        super(HomeController, self).__before__()

    def about(self):
        return render('/about.html')

    @LoginRequired()
    def index(self):
        c.groups = self.scm_model.get_repo_groups()
        c.group = None

        repos_list = Repository.query(sorted=True).filter_by(group=None).all()

        repos_data = RepoModel().get_repos_as_dict(repos_list=repos_list,
                                                   admin=False, short_name=True)
        #data used to render the grid
        c.data = repos_data

        return render('/index.html')

    @LoginRequired()
    @jsonify
    def repo_switcher_data(self):
        #wrapper for conditional cache
        def _c():
            log.debug('generating switcher repo/groups list')
            all_repos = Repository.query(sorted=True).all()
            repo_iter = self.scm_model.get_repos(all_repos)
            all_groups = RepoGroup.query(sorted=True).all()
            repo_groups_iter = self.scm_model.get_repo_groups(all_groups)

            res = [{
                    'text': _('Groups'),
                    'children': [
                       {'id': obj.group_name,
                        'text': obj.group_name,
                        'type': 'group',
                        'obj': {}}
                       for obj in repo_groups_iter
                    ],
                   },
                   {
                    'text': _('Repositories'),
                    'children': [
                       {'id': obj.repo_name,
                        'text': obj.repo_name,
                        'type': 'repo',
                        'obj': obj.get_dict()}
                       for obj in repo_iter
                    ],
                   }]

            data = {
                'more': False,
                'results': res,
            }
            return data

        if request.is_xhr:
            condition = False
            compute = conditional_cache('short_term', 'cache_desc',
                                        condition=condition, func=_c)
            return compute()
        else:
            raise HTTPBadRequest()

    @LoginRequired()
    @HasRepoPermissionLevelDecorator('read')
    @jsonify
    def repo_refs_data(self, repo_name):
        repo = Repository.get_by_repo_name(repo_name).scm_instance
        res = []
        _branches = repo.branches.items()
        if _branches:
            res.append({
                'text': _('Branch'),
                'children': [{'id': rev, 'text': name, 'type': 'branch'} for name, rev in _branches]
            })
        _closed_branches = repo.closed_branches.items()
        if _closed_branches:
            res.append({
                'text': _('Closed Branches'),
                'children': [{'id': rev, 'text': name, 'type': 'closed-branch'} for name, rev in _closed_branches]
            })
        _tags = repo.tags.items()
        if _tags:
            res.append({
                'text': _('Tag'),
                'children': [{'id': rev, 'text': name, 'type': 'tag'} for name, rev in _tags]
            })
        _bookmarks = repo.bookmarks.items()
        if _bookmarks:
            res.append({
                'text': _('Bookmark'),
                'children': [{'id': rev, 'text': name, 'type': 'book'} for name, rev in _bookmarks]
            })
        data = {
            'more': False,
            'results': res
        }
        return data