Changeset - 9c0f5d558789
[Not reviewed]
beta
0 21 0
Marcin Kuzminski - 14 years ago 2011-06-07 17:58:51
marcin@python-works.com
fixes #200, rewrote the whole caching mechanism to get rid of such problems. Now cached instances are attached
to db repository instance, and then fetched from cache. Also made all current test work.
21 files changed with 398 insertions and 238 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/repos.py
Show inline comments
 
@@ -80,28 +80,26 @@ class ReposController(BaseController):
 
        c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups)
 
        c.users_array = repo_model.get_users_js()
 
        c.users_groups_array = repo_model.get_users_groups_js()
 

	
 
    def __load_data(self, repo_name=None):
 
        """
 
        Load defaults settings for edit, and update
 

	
 
        :param repo_name:
 
        """
 
        self.__load_defaults()
 

	
 
        repo, dbrepo = ScmModel().get(repo_name, retval='repo')
 

	
 
        repo_model = RepoModel()
 
        c.repo_info = repo_model.get_by_repo_name(repo_name)
 
        c.repo_info = db_repo = Repository.by_repo_name(repo_name)
 
        repo = scm_repo = db_repo.scm_instance
 

	
 
        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)\
 
@@ -144,28 +142,27 @@ class ReposController(BaseController):
 
        #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"""
 
        # url('repos')
 

	
 
        all_repos = [r.repo_name for r in Repository.query().all()]
 

	
 
        cached_repo_list = ScmModel().get_repos(all_repos)
 
        c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort'))
 
        c.repos_list = ScmModel().get_repos(Repository.query()
 
                                            .order_by(Repository.repo_name)
 
                                            .all(), sort_key='name_sort')
 
        return render('admin/repos/repos.html')
 

	
 
    @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
 
    def create(self):
 
        """
 
        POST /repos: Create a new item"""
 
        # url('repos')
 
        repo_model = RepoModel()
 
        self.__load_defaults()
 
        form_result = {}
 
        try:
 
            form_result = RepoForm(repo_groups=c.repo_groups_choices)()\
rhodecode/controllers/admin/repos_groups.py
Show inline comments
 
@@ -172,56 +172,52 @@ class ReposGroupsController(BaseControll
 
            #TODO: in futureaction_logger(, '', '', '', self.sa)
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            h.flash(_('error occurred during deletion of repos group %s' % gr.group_name),
 
                    category='error')
 

	
 
        return redirect(url('repos_groups'))
 

	
 
    def show(self, id, format='html'):
 
        """GET /repos_groups/id: Show a specific item"""
 
        # url('repos_group', id=ID)
 

	
 
        c.group = Group.get(id)
 
        gr = c.group = Group.get(id)
 

	
 
        if c.group:
 
            c.group_repos = c.group.repositories.all()
 
        else:
 
            return redirect(url('repos_group'))
 

	
 

	
 
        sortables = ['name', 'description', 'last_change', 'tip', 'owner']
 
        current_sort = request.GET.get('sort', 'name')
 
        current_sort_slug = current_sort.replace('-', '')
 

	
 
        if current_sort_slug not in sortables:
 
            c.sort_by = 'name'
 
            current_sort_slug = c.sort_by
 
        else:
 
            c.sort_by = current_sort
 
        c.sort_slug = current_sort_slug
 

	
 
        sort_key = current_sort_slug + '_sort'
 

	
 
        #overwrite our cached list with current filter
 
        gr_filter = [r.repo_name for r in c.group_repos]
 
        gr_filter = c.group_repos
 
        c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter)
 

	
 
        if c.sort_by.startswith('-'):
 
            c.repos_list = sorted(c.cached_repo_list, key=itemgetter(sort_key),
 
                                  reverse=True)
 
        else:
 
            c.repos_list = sorted(c.cached_repo_list, key=itemgetter(sort_key),
 
                                  reverse=False)
 
        c.repos_list = c.cached_repo_list
 

	
 
        c.repo_cnt = len(c.repos_list)
 

	
 
        c.repo_cnt = 0
 

	
 
        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')
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
    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)
rhodecode/controllers/admin/settings.py
Show inline comments
 
@@ -249,27 +249,28 @@ class SettingsController(BaseController)
 
        GET /admin/settings/setting_id/edit: Form to
 
        edit an existing item"""
 
        # url('admin_edit_setting', setting_id=ID)
 

	
 
    @NotAnonymous()
 
    def my_account(self):
 
        """
 
        GET /_admin/my_account Displays info about my account
 
        """
 
        # url('admin_settings_my_account')
 

	
 
        c.user = UserModel().get(self.rhodecode_user.user_id, cache=False)
 
        all_repos = [r.repo_name for r in self.sa.query(Repository)\
 
        all_repos = self.sa.query(Repository)\
 
                     .filter(Repository.user_id == c.user.user_id)\
 
                     .order_by(func.lower(Repository.repo_name)).all()]
 
                     .order_by(func.lower(Repository.repo_name)).all()
 

	
 
        c.user_repos = ScmModel().get_repos(all_repos)
 

	
 
        if c.user.username == 'default':
 
            h.flash(_("You can't edit this user since it's"
 
              " crucial for entire application"), category='warning')
 
            return redirect(url('users'))
 

	
 
        defaults = c.user.get_dict()
 
        return htmlfill.render(
 
            render('admin/users/user_edit_my_account.html'),
 
            defaults=defaults,
 
            encoding="UTF-8",
rhodecode/controllers/home.py
Show inline comments
 
@@ -22,59 +22,55 @@
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import logging
 
from operator import itemgetter
 

	
 
from pylons import tmpl_context as c, request
 
from paste.httpexceptions import HTTPBadRequest
 

	
 
from rhodecode.lib.auth import LoginRequired
 
from rhodecode.lib.base import BaseController, render
 
from rhodecode.model.db import Group
 
from rhodecode.model.db import Group, Repository
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class HomeController(BaseController):
 

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

	
 
    def index(self):
 
        sortables = ['name', 'description', 'last_change', 'tip', 'owner']
 
        current_sort = request.GET.get('sort', 'name')
 
        current_sort_slug = current_sort.replace('-', '')
 

	
 
        if current_sort_slug not in sortables:
 
            c.sort_by = 'name'
 
            current_sort_slug = c.sort_by
 
        else:
 
            c.sort_by = current_sort
 
        c.sort_slug = current_sort_slug
 

	
 
        sort_key = current_sort_slug + '_sort'
 

	
 
        if c.sort_by.startswith('-'):
 
            c.repos_list = sorted(c.cached_repo_list, key=itemgetter(sort_key),
 
                                  reverse=True)
 
        else:
 
            c.repos_list = sorted(c.cached_repo_list, key=itemgetter(sort_key),
 
                                  reverse=False)
 

	
 
        c.repos_list = self.scm_model.get_repos(sort_key=sort_key)
 

	
 
        c.repo_cnt = len(c.repos_list)
 

	
 

	
 
        c.groups = Group.query().filter(Group.group_parent_id == None).all()
 

	
 

	
 
        return render('/index.html')
 

	
 
    def repo_switcher(self):
 
        if request.is_xhr:
 
            c.repos_list = sorted(c.cached_repo_list,
 
                                  key=itemgetter('name_sort'), reverse=False)
 
            all_repos = Repository.query().order_by(Repository.repo_name).all()
 
            c.repos_list = self.scm_model.get_repos(all_repos,
 
                                                    sort_key='name_sort')
 
            return render('/repo_switcher_list.html')
 
        else:
 
            return HTTPBadRequest()
rhodecode/controllers/settings.py
Show inline comments
 
@@ -146,24 +146,25 @@ class SettingsController(BaseRepoControl
 
                      ' please run the application again'
 
                      ' in order to rescan repositories') % repo_name,
 
                      category='error')
 

	
 
            return redirect(url('home'))
 
        try:
 
            action_logger(self.rhodecode_user, 'user_deleted_repo',
 
                              repo_name, '', self.sa)
 
            repo_model.delete(repo)
 
            invalidate_cache('get_repo_cached_%s' % repo_name)
 
            h.flash(_('deleted repository %s') % repo_name, category='success')
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            h.flash(_('An error occurred during deletion of %s') % repo_name,
 
                    category='error')
 

	
 
        return redirect(url('home'))
 

	
 
    @NotAnonymous()
 
    @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
 
                                   'repository.admin')
 
    def fork(self, repo_name):
 
        repo_model = RepoModel()
 
        c.repo_info = repo = repo_model.get_by_repo_name(repo_name)
 
        if not repo:
 
@@ -196,13 +197,18 @@ class SettingsController(BaseRepoControl
 
                          'user_forked_repo:%s' % form_result['fork_name'],
 
                           repo_name, '', self.sa)
 
        except formencode.Invalid, errors:
 
            c.new_repo = errors.value['fork_name']
 
            r = render('settings/repo_fork.html')
 

	
 
            return htmlfill.render(
 
                r,
 
                defaults=errors.value,
 
                errors=errors.error_dict or {},
 
                prefix_error=False,
 
                encoding="UTF-8")
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            h.flash(_('An error occurred during repository forking %s') %
 
                    repo_name, category='error')
 

	
 
        return redirect(url('home'))
rhodecode/lib/base.py
Show inline comments
 
@@ -3,39 +3,40 @@
 
Provides the BaseController class for subclassing.
 
"""
 
from pylons import config, tmpl_context as c, request, session
 
from pylons.controllers import WSGIController
 
from pylons.templating import render_mako as render
 

	
 
from rhodecode import __version__
 
from rhodecode.lib.auth import AuthUser
 
from rhodecode.lib.utils import get_repo_slug
 
from rhodecode.model import meta
 
from rhodecode.model.scm import ScmModel
 
from rhodecode import BACKENDS
 
from rhodecode.model.db import Repository
 

	
 

	
 
class BaseController(WSGIController):
 

	
 
    def __before__(self):
 
        c.rhodecode_version = __version__
 
        c.rhodecode_name = config.get('rhodecode_title')
 
        c.ga_code = config.get('rhodecode_ga_code')
 
        c.repo_name = get_repo_slug(request)
 
        c.backends = BACKENDS.keys()
 
        self.cut_off_limit = int(config.get('cut_off_limit'))
 

	
 
        self.sa = meta.Session()
 
        self.scm_model = ScmModel(self.sa)
 
        c.cached_repo_list = self.scm_model.get_repos()
 

	
 
        #c.unread_journal = scm_model.get_unread_journal()
 

	
 
    def __call__(self, environ, start_response):
 
        """Invoke the Controller"""
 
        # WSGIController.__call__ dispatches to the Controller method
 
        # the request is routed to. This routing information is
 
        # available in environ['pylons.routes_dict']
 
        try:
 
            # putting this here makes sure that we update permissions each time
 
            api_key = request.GET.get('api_key')
 
            user_id = getattr(session.get('rhodecode_user'), 'user_id', None)
 
            self.rhodecode_user = c.rhodecode_user = AuthUser(user_id, api_key)
 
@@ -53,23 +54,22 @@ class BaseRepoController(BaseController)
 
    """
 
    Base class for controllers responsible for loading all needed data
 
    for those controllers, loaded items are
 

	
 
    c.rhodecode_repo: instance of scm repository (taken from cache)
 

	
 
    """
 

	
 
    def __before__(self):
 
        super(BaseRepoController, self).__before__()
 
        if c.repo_name:
 

	
 
            c.rhodecode_repo, dbrepo = self.scm_model.get(c.repo_name,
 
                                                          retval='repo')
 
            c.rhodecode_repo = Repository.by_repo_name(c.repo_name).scm_instance
 

	
 
            if c.rhodecode_repo is not None:
 
                c.repository_followers = \
 
                    self.scm_model.get_followers(c.repo_name)
 
                c.repository_forks = self.scm_model.get_forks(c.repo_name)
 
            else:
 
                c.repository_followers = 0
 
                c.repository_forks = 0
 

	
rhodecode/lib/celerylib/tasks.py
Show inline comments
 
@@ -93,25 +93,24 @@ def whoosh_index(repo_location, full_ind
 

	
 

	
 
@task(ignore_result=True)
 
def get_commits_stats(repo_name, ts_min_y, ts_max_y):
 
    try:
 
        log = get_commits_stats.get_logger()
 
    except:
 
        log = logging.getLogger(__name__)
 

	
 
    lockkey = __get_lockkey('get_commits_stats', repo_name, ts_min_y,
 
                            ts_max_y)
 
    lockkey_path = dn(dn(dn(dn(os.path.abspath(__file__)))))
 
    print jn(lockkey_path, lockkey)
 
    log.info('running task with lockkey %s', lockkey)
 
    try:
 
        lock = l = DaemonLock(jn(lockkey_path, lockkey))
 

	
 
        #for js data compatibilty cleans the key for person from '
 
        akc = lambda k: person(k).replace('"', "")
 

	
 
        co_day_auth_aggr = {}
 
        commits_by_day_aggregate = {}
 
        repos_path = get_repos_path()
 
        p = os.path.join(repos_path, repo_name)
 
        repo = get_repo(p)
rhodecode/lib/helpers.py
Show inline comments
 
@@ -363,26 +363,25 @@ def action_parser(user_log, feed=False):
 
    x = action.split(':')
 

	
 
    if len(x) > 1:
 
        action, action_params = x
 

	
 
    def get_cs_links():
 
        revs_limit = 5 #display this amount always
 
        revs_top_limit = 50 #show upto this amount of changesets hidden
 
        revs = action_params.split(',')
 
        repo_name = user_log.repository.repo_name
 

	
 
        from rhodecode.model.scm import ScmModel
 
        repo, dbrepo = ScmModel().get(repo_name, retval='repo',
 
                                      invalidation_list=[])
 
        repo = user_log.repository.scm_instance
 

	
 
        message = lambda rev: get_changeset_safe(repo, rev).message
 
        cs_links = []
 
        cs_links.append(" " + ', '.join ([link_to(rev,
 
                url('changeset_home',
 
                repo_name=repo_name,
 
                revision=rev), title=tooltip(message(rev)),
 
                class_='tooltip') for rev in revs[:revs_limit] ]))
 

	
 
        compare_view = (' <div class="compare_view tooltip" title="%s">'
 
                        '<a href="%s">%s</a> '
 
                        '</div>' % (_('Show all combined changesets %s->%s' \
rhodecode/lib/utils.py
Show inline comments
 
@@ -463,25 +463,25 @@ def create_test_index(repo_location, ful
 
    :param repo_location:
 
    :param full_index:
 
    """
 
    from rhodecode.lib.indexers.daemon import WhooshIndexingDaemon
 
    from rhodecode.lib.pidlock import DaemonLock, LockHeld
 
    import shutil
 

	
 
    index_location = os.path.join(repo_location, 'index')
 
    if os.path.exists(index_location):
 
        shutil.rmtree(index_location)
 

	
 
    try:
 
        l = DaemonLock(file=jn(dn(dn(index_location)), 'make_index.lock'))
 
        l = DaemonLock(file=jn(dn(index_location), 'make_index.lock'))
 
        WhooshIndexingDaemon(index_location=index_location,
 
                             repo_location=repo_location)\
 
            .run(full_index=full_index)
 
        l.release()
 
    except LockHeld:
 
        pass
 

	
 

	
 
def create_test_env(repos_test_path, config):
 
    """Makes a fresh database and
 
    install test repository into tmp dir
 
    """
rhodecode/model/db.py
Show inline comments
 
@@ -17,31 +17,41 @@
 
#
 
# 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/>.
 

	
 
import os
 
import logging
 
import datetime
 
import traceback
 
from datetime import date
 

	
 
from sqlalchemy import *
 
from sqlalchemy.exc import DatabaseError
 
from sqlalchemy.orm import relationship, backref
 
from sqlalchemy.orm import relationship, backref, joinedload
 
from sqlalchemy.orm.interfaces import MapperExtension
 

	
 
from beaker.cache import cache_region, region_invalidate
 

	
 

	
 
from vcs import get_backend
 
from vcs.utils.helpers import get_scm
 
from vcs.exceptions import RepositoryError, VCSError
 
from vcs.utils.lazy import LazyProperty
 
from vcs.nodes import FileNode
 

	
 
from rhodecode.lib import str2bool
 
from rhodecode.model.meta import Base, Session
 
from rhodecode.model.caching_query import FromCache
 

	
 
log = logging.getLogger(__name__)
 

	
 
#==============================================================================
 
# MAPPER EXTENSIONS
 
#==============================================================================
 

	
 
class RepositoryMapper(MapperExtension):
 
    def after_update(self, mapper, connection, instance):
 
@@ -141,24 +151,25 @@ class User(Base):
 
        return '%s %s <%s>' % (self.name, self.lastname, self.email)
 

	
 
    @property
 
    def short_contact(self):
 
        return '%s %s' % (self.name, self.lastname)
 

	
 

	
 
    @property
 
    def is_admin(self):
 
        return self.admin
 

	
 
    def __repr__(self):
 
        return 'ahmmm'
 
        return "<%s('id:%s:%s')>" % (self.__class__.__name__,
 
                                     self.user_id, self.username)
 

	
 
    @classmethod
 
    def by_username(cls, username):
 
        return Session.query(cls).filter(cls.username == username).one()
 

	
 

	
 
    def update_lastlogin(self):
 
        """Update user lastlogin"""
 

	
 
        try:
 
@@ -257,26 +268,31 @@ class Repository(Base):
 
    stats = relationship('Statistics', cascade='all', uselist=False)
 

	
 
    followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
 

	
 
    logs = relationship('UserLog', cascade='all')
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (self.__class__.__name__,
 
                                  self.repo_id, self.repo_name)
 

	
 
    @classmethod
 
    def by_repo_name(cls, repo_name):
 
        return Session.query(cls).filter(cls.repo_name == repo_name).one()
 
        q = Session.query(cls).filter(cls.repo_name == repo_name)
 

	
 
        q = q.options(joinedload(Repository.fork))\
 
            .options(joinedload(Repository.user))\
 
            .options(joinedload(Repository.group))\
 

	
 
        return q.one()
 

	
 
    @classmethod
 
    def get_repo_forks(cls, repo_id):
 
        return Session.query(cls).filter(Repository.fork_id == repo_id)
 

	
 
    @property
 
    def just_name(self):
 
        return self.repo_name.split(os.sep)[-1]
 

	
 
    @property
 
    def groups_with_parents(self):
 
        groups = []
 
@@ -289,24 +305,145 @@ class Repository(Base):
 
            gr = getattr(cur_gr, 'parent_group', None)
 
            cur_gr = cur_gr.parent_group
 
            if gr is None:
 
                break
 
            groups.insert(0, gr)
 

	
 
        return groups
 

	
 
    @property
 
    def groups_and_repo(self):
 
        return self.groups_with_parents, self.just_name
 

	
 
    @LazyProperty
 
    def repo_path(self):
 
        """
 
        Returns base full path for that repository means where it actually
 
        exists on a filesystem
 
        """
 

	
 
        q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
 
        return q.ui_value
 

	
 
    @property
 
    def repo_full_path(self):
 
        p = [self.repo_path]
 
        # we need to split the name by / since this is how we store the
 
        # names in the database, but that eventually needs to be converted
 
        # into a valid system path
 
        p += self.repo_name.split('/')
 
        return os.path.join(*p)
 

	
 
    @property
 
    def _ui(self):
 
        """
 
        Creates an db based ui object for this repository
 
        """
 
        from mercurial import ui
 
        from mercurial import config
 
        baseui = ui.ui()
 

	
 
        #clean the baseui object
 
        baseui._ocfg = config.config()
 
        baseui._ucfg = config.config()
 
        baseui._tcfg = config.config()
 

	
 

	
 
        ret = Session.query(RhodeCodeUi)\
 
            .options(FromCache("sql_cache_short",
 
                               "repository_repo_ui")).all()
 

	
 
        hg_ui = ret
 
        for ui_ in hg_ui:
 
            if ui_.ui_active:
 
                log.debug('settings ui from db[%s]%s:%s', ui_.ui_section,
 
                          ui_.ui_key, ui_.ui_value)
 
                baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value)
 

	
 
        return baseui
 

	
 
    #==========================================================================
 
    # SCM CACHE INSTANCE
 
    #==========================================================================
 

	
 
    @property
 
    def invalidate(self):
 
        """
 
        Returns Invalidation object if this repo should be invalidated
 
        None otherwise. `cache_active = False` means that this cache
 
        state is not valid and needs to be invalidated
 
        """
 
        return Session.query(CacheInvalidation)\
 
            .filter(CacheInvalidation.cache_key == self.repo_name)\
 
            .filter(CacheInvalidation.cache_active == False)\
 
            .scalar()
 

	
 
    @property
 
    def set_invalidate(self):
 
        """
 
        set a cache for invalidation for this instance
 
        """
 
        inv = Session.query(CacheInvalidation)\
 
            .filter(CacheInvalidation.cache_key == self.repo_name)\
 
            .scalar()
 

	
 
        if inv is None:
 
            inv = CacheInvalidation(self.repo_name)
 
        inv.cache_active = True
 
        Session.add(inv)
 
        Session.commit()
 

	
 
    @property
 
    def scm_instance(self):
 
        return self.__get_instance(self.repo_name)
 

	
 
    @property
 
    def scm_instance_cached(self):
 
        @cache_region('long_term')
 
        def _c(repo_name):
 
            return self.__get_instance(repo_name)
 

	
 
        inv = self.invalidate
 
        if inv:
 
            region_invalidate(_c, None, self.repo_name)
 
            #update our cache
 
            inv.cache_key.cache_active = True
 
            Session.add(inv)
 
            Session.commit()
 

	
 
        return _c(self.repo_name)
 

	
 
    def __get_instance(self, repo_name):
 
        try:
 
            alias = get_scm(self.repo_full_path)[0]
 
            log.debug('Creating instance of %s repository', alias)
 
            backend = get_backend(alias)
 
        except VCSError:
 
            log.error(traceback.format_exc())
 
            log.error('Perhaps this repository is in db and not in '
 
                      'filesystem run rescan repositories with '
 
                      '"destroy old data " option from admin panel')
 
            return
 

	
 
        if alias == 'hg':
 
            repo = backend(self.repo_full_path, create=False,
 
                           baseui=self._ui)
 
            #skip hidden web repository
 
            if repo._get_hidden():
 
                return
 
        else:
 
            repo = backend(self.repo_full_path, create=False)
 

	
 
        return repo
 

	
 

	
 
class Group(Base):
 
    __tablename__ = 'groups'
 
    __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'),
 
                      CheckConstraint('group_id != group_parent_id'), {'extend_existing':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)
 

	
rhodecode/model/forms.py
Show inline comments
 
@@ -271,24 +271,31 @@ def ValidRepoName(edit, old_data):
 

	
 
                else:
 
                    if RepoModel().get_by_repo_name(repo_name_full):
 
                        e_dict = {'repo_name':_('This repository '
 
                                                'already exists')}
 
                        raise formencode.Invalid('', value, state,
 
                                                 error_dict=e_dict)
 
            return value
 

	
 

	
 
    return _ValidRepoName
 

	
 
def ValidForkName():
 
    class _ValidForkName(formencode.validators.FancyValidator):
 
        def to_python(self, value, state):
 
            return value
 
    return _ValidForkName
 

	
 

	
 
def SlugifyName():
 
    class _SlugifyName(formencode.validators.FancyValidator):
 

	
 
        def to_python(self, value, state):
 
            return repo_name_slug(value)
 

	
 
    return _SlugifyName
 

	
 
def ValidCloneUri():
 
    from mercurial.httprepo import httprepository, httpsrepository
 
    from rhodecode.lib.utils import make_ui
 

	
 
@@ -317,24 +324,25 @@ def ValidCloneUri():
 
                                         state)
 
            return value
 

	
 
    return _ValidCloneUri
 

	
 
def ValidForkType(old_data):
 
    class _ValidForkType(formencode.validators.FancyValidator):
 

	
 
        def to_python(self, value, state):
 
            if old_data['repo_type'] != value:
 
                raise formencode.Invalid(_('Fork have to be the same '
 
                                           'type as original'), value, state)
 

	
 
            return value
 
    return _ValidForkType
 

	
 
class ValidPerms(formencode.validators.FancyValidator):
 
    messages = {'perm_new_member_name':_('This username or users group name'
 
                                         ' is not valid')}
 

	
 
    def to_python(self, value, state):
 
        perms_update = []
 
        perms_new = []
 
        #build a list of permission to update and new permission to create
 
        for k, v in value.items():
 
@@ -574,24 +582,27 @@ def RepoForm(edit=False, old_data={}, su
 
        chained_validators = [ValidRepoName(edit, old_data), ValidPerms]
 
    return _RepoForm
 

	
 
def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()):
 
    class _RepoForkForm(formencode.Schema):
 
        allow_extra_fields = True
 
        filter_extra_fields = False
 
        fork_name = All(UnicodeString(strip=True, min=1, not_empty=True),
 
                        SlugifyName())
 
        description = UnicodeString(strip=True, min=1, not_empty=True)
 
        private = StringBoolean(if_missing=False)
 
        repo_type = All(ValidForkType(old_data), OneOf(supported_backends))
 

	
 
        chained_validators = [ValidForkName()]
 

	
 
    return _RepoForkForm
 

	
 
def RepoSettingsForm(edit=False, old_data={}):
 
    class _RepoForm(formencode.Schema):
 
        allow_extra_fields = True
 
        filter_extra_fields = False
 
        repo_name = All(UnicodeString(strip=True, min=1, not_empty=True),
 
                        SlugifyName())
 
        description = UnicodeString(strip=True, min=1, not_empty=True)
 
        private = StringBoolean(if_missing=False)
 

	
 
        chained_validators = [ValidRepoName(edit, old_data), ValidPerms, ValidSettings]
rhodecode/model/repo.py
Show inline comments
 
@@ -61,46 +61,24 @@ class RepoModel(BaseModel):
 
                                          "get_repo_%s" % repo_id))
 
        return repo.scalar()
 

	
 
    def get_by_repo_name(self, repo_name, cache=False):
 
        repo = self.sa.query(Repository)\
 
            .filter(Repository.repo_name == repo_name)
 

	
 
        if cache:
 
            repo = repo.options(FromCache("sql_cache_short",
 
                                          "get_repo_%s" % repo_name))
 
        return repo.scalar()
 

	
 
    def get_full(self, repo_name, cache=False, invalidate=False):
 
        repo = self.sa.query(Repository)\
 
            .options(joinedload(Repository.fork))\
 
            .options(joinedload(Repository.user))\
 
            .options(joinedload(Repository.group))\
 
            .filter(Repository.repo_name == repo_name)\
 

	
 
        if cache:
 
            repo = repo.options(FromCache("sql_cache_long",
 
                                          "get_repo_full_%s" % repo_name))
 
        if invalidate and cache:
 
            repo.invalidate()
 

	
 
        ret = repo.scalar()
 

	
 
        #make transient for sake of errors
 
        make_transient(ret)
 
        for k in ['fork', 'user', 'group']:
 
            attr = getattr(ret, k, False)
 
            if attr:
 
                make_transient(attr)
 
        return ret
 

	
 
    def get_users_js(self):
 

	
 
        users = self.sa.query(User).filter(User.active == True).all()
 
        u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},'''
 
        users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name,
 
                                                    u.lastname, u.username)
 
                                        for u in users])
 
        return users_array
 

	
 
    def get_users_groups_js(self):
 
        users_groups = self.sa.query(UsersGroup)\
 
@@ -184,48 +162,52 @@ class RepoModel(BaseModel):
 
            if repo_name != form_data['repo_name_full']:
 
                # rename repository
 
                self.__rename_repo(old=repo_name,
 
                                   new=form_data['repo_name_full'])
 

	
 
            self.sa.commit()
 
        except:
 
            log.error(traceback.format_exc())
 
            self.sa.rollback()
 
            raise
 

	
 
    def create(self, form_data, cur_user, just_db=False, fork=False):
 

	
 
        try:
 
            if fork:
 
                #force str since hg doesn't go with unicode
 
                repo_name = str(form_data['fork_name'])
 
                org_name = str(form_data['repo_name'])
 
                org_full_name = str(form_data['repo_name_full'])
 
                org_full_name = org_name#str(form_data['fork_name_full'])
 

	
 
            else:
 
                org_name = repo_name = str(form_data['repo_name'])
 
                repo_name_full = form_data['repo_name_full']
 

	
 
            new_repo = Repository()
 
            new_repo.enable_statistics = False
 
            for k, v in form_data.items():
 
                if k == 'repo_name':
 
                    v = repo_name_full
 
                    if fork:
 
                        v = repo_name
 
                    else:
 
                        v = repo_name_full
 
                if k == 'repo_group':
 
                    k = 'group_id'
 

	
 
                setattr(new_repo, k, v)
 

	
 
            if fork:
 
                parent_repo = self.sa.query(Repository)\
 
                        .filter(Repository.repo_name == org_full_name).scalar()
 
                        .filter(Repository.repo_name == org_full_name).one()
 
                new_repo.fork = parent_repo
 

	
 
            new_repo.user_id = cur_user.user_id
 
            self.sa.add(new_repo)
 

	
 
            #create default permission
 
            repo_to_perm = RepoToPerm()
 
            default = 'repository.read'
 
            for p in UserModel(self.sa).get_by_username('default',
 
                                                    cache=False).user_perms:
 
                if p.permission.permission_name.startswith('repository.'):
 
                    default = p.permission.permission_name
rhodecode/model/scm.py
Show inline comments
 
@@ -61,24 +61,93 @@ class UserTemp(object):
 

	
 
    def __repr__(self):
 
        return "<%s('id:%s')>" % (self.__class__.__name__, self.user_id)
 

	
 

	
 
class RepoTemp(object):
 
    def __init__(self, repo_id):
 
        self.repo_id = repo_id
 

	
 
    def __repr__(self):
 
        return "<%s('id:%s')>" % (self.__class__.__name__, self.repo_id)
 

	
 
class CachedRepoList(object):
 

	
 
    def __init__(self, db_repo_list, invalidation_list, repos_path,
 
                 order_by=None):
 
        self.db_repo_list = db_repo_list
 
        self.invalidation_list = invalidation_list
 
        self.repos_path = repos_path
 
        self.order_by = order_by
 
        self.reversed = (order_by or '').startswith('-')
 

	
 
    def __len__(self):
 
        return len(self.db_repo_list)
 

	
 
    def __repr__(self):
 
        return '<%s (%s)>' % (self.__class__.__name__, self.__len__())
 

	
 
    def __iter__(self):
 
        for db_repo in self.db_repo_list:
 
            dbr = db_repo
 

	
 
            # invalidate the repo cache if needed before getting the 
 
            # scm instance
 

	
 
            scm_invalidate = False
 
            if self.invalidation_list is not None:
 
                scm_invalidate = dbr.repo_name in self.invalidation_list
 

	
 
            if scm_invalidate:
 
                log.info('invalidating cache for repository %s',
 
                         dbr.repo_name)
 
                db_repo.set_invalidate
 

	
 
            scmr = db_repo.scm_instance_cached
 

	
 
            #check permission at this level
 
            if not HasRepoPermissionAny('repository.read',
 
                                        'repository.write',
 
                                        'repository.admin')(dbr.repo_name,
 
                                                            'get repo check'):
 
                continue
 

	
 

	
 

	
 

	
 

	
 
            last_change = scmr.last_change
 
            tip = h.get_changeset_safe(scmr, 'tip')
 

	
 
            tmp_d = {}
 
            tmp_d['name'] = dbr.repo_name
 
            tmp_d['name_sort'] = tmp_d['name'].lower()
 
            tmp_d['description'] = dbr.description
 
            tmp_d['description_sort'] = tmp_d['description']
 
            tmp_d['last_change'] = last_change
 
            tmp_d['last_change_sort'] = time.mktime(last_change \
 
                                                    .timetuple())
 
            tmp_d['tip'] = tip.raw_id
 
            tmp_d['tip_sort'] = tip.revision
 
            tmp_d['rev'] = tip.revision
 
            tmp_d['contact'] = dbr.user.full_contact
 
            tmp_d['contact_sort'] = tmp_d['contact']
 
            tmp_d['owner_sort'] = tmp_d['contact']
 
            tmp_d['repo_archives'] = list(scmr._get_archives())
 
            tmp_d['last_msg'] = tip.message
 
            tmp_d['repo'] = scmr
 
            tmp_d['dbrepo'] = dbr.get_dict()
 
            tmp_d['dbrepo_fork'] = dbr.fork.get_dict() if dbr.fork \
 
                                                                    else {}
 
            yield tmp_d
 

	
 
class ScmModel(BaseModel):
 
    """Generic Scm Model
 
    """
 

	
 
    @LazyProperty
 
    def repos_path(self):
 
        """Get's the repositories root path from database
 
        """
 

	
 
        q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
 

	
 
@@ -109,147 +178,48 @@ class ScmModel(BaseModel):
 
                    klass = get_backend(path[0])
 

	
 
                    if path[0] == 'hg' and path[0] in BACKENDS.keys():
 
                        repos_list[name] = klass(path[1], baseui=baseui)
 

	
 
                    if path[0] == 'git' and path[0] in BACKENDS.keys():
 
                        repos_list[name] = klass(path[1])
 
            except OSError:
 
                continue
 

	
 
        return repos_list
 

	
 
    def get_repos(self, all_repos=None):
 
    def get_repos(self, all_repos=None, sort_key=None):
 
        """
 
        Get all repos from db and for each repo create it's
 
        backend instance and fill that backed with information from database
 

	
 
        :param all_repos: list of repository names as strings
 
            give specific repositories list, good for filtering
 
        """
 
        if all_repos is None:
 
            repos = self.sa.query(Repository)\
 
            all_repos = self.sa.query(Repository)\
 
                        .filter(Repository.group_id == None)\
 
                        .order_by(Repository.repo_name).all()
 
            all_repos = [r.repo_name for r in repos]
 

	
 
        #get the repositories that should be invalidated
 
        invalidation_list = [str(x.cache_key) for x in \
 
                             self.sa.query(CacheInvalidation.cache_key)\
 
                             .filter(CacheInvalidation.cache_active == False)\
 
                             .all()]
 
        for r_name in all_repos:
 
            r_dbr = self.get(r_name, invalidation_list)
 
            if r_dbr is not None:
 
                repo, dbrepo = r_dbr
 

	
 
                if repo is None or dbrepo is None:
 
                    log.error('Repository "%s" looks somehow corrupted '
 
                              'fs-repo:%s,db-repo:%s both values should be '
 
                              'present', r_name, repo, dbrepo)
 
                    continue
 
                last_change = repo.last_change
 
                tip = h.get_changeset_safe(repo, 'tip')
 

	
 
                tmp_d = {}
 
                tmp_d['name'] = dbrepo.repo_name
 
                tmp_d['name_sort'] = tmp_d['name'].lower()
 
                tmp_d['description'] = dbrepo.description
 
                tmp_d['description_sort'] = tmp_d['description']
 
                tmp_d['last_change'] = last_change
 
                tmp_d['last_change_sort'] = time.mktime(last_change \
 
                                                        .timetuple())
 
                tmp_d['tip'] = tip.raw_id
 
                tmp_d['tip_sort'] = tip.revision
 
                tmp_d['rev'] = tip.revision
 
                tmp_d['contact'] = dbrepo.user.full_contact
 
                tmp_d['contact_sort'] = tmp_d['contact']
 
                tmp_d['owner_sort'] = tmp_d['contact']
 
                tmp_d['repo_archives'] = list(repo._get_archives())
 
                tmp_d['last_msg'] = tip.message
 
                tmp_d['repo'] = repo
 
                tmp_d['dbrepo'] = dbrepo.get_dict()
 
                tmp_d['dbrepo_fork'] = dbrepo.fork.get_dict() if dbrepo.fork \
 
                                                                        else {}
 
                yield tmp_d
 

	
 
    def get(self, repo_name, invalidation_list=None, retval='all'):
 
        """Returns a tuple of Repository,DbRepository,
 
        Get's repository from given name, creates BackendInstance and
 
        propagates it's data from database with all additional information
 

	
 
        :param repo_name:
 
        :param invalidation_list: if a invalidation list is given the get
 
            method should not manually check if this repository needs
 
            invalidation and just invalidate the repositories in list
 
        :param retval: string specifing what to return one of 'repo','dbrepo',
 
            'all'if repo or dbrepo is given it'll just lazy load chosen type
 
            and return None as the second
 
        """
 
        if not HasRepoPermissionAny('repository.read', 'repository.write',
 
                            'repository.admin')(repo_name, 'get repo check'):
 
            return
 

	
 
        #======================================================================
 
        # CACHE FUNCTION
 
        #======================================================================
 
        @cache_region('long_term')
 
        def _get_repo(repo_name):
 

	
 
            repo_path = os.path.join(self.repos_path, repo_name)
 

	
 
            try:
 
                alias = get_scm(repo_path)[0]
 
                log.debug('Creating instance of %s repository', alias)
 
                backend = get_backend(alias)
 
            except VCSError:
 
                log.error(traceback.format_exc())
 
                log.error('Perhaps this repository is in db and not in '
 
                          'filesystem run rescan repositories with '
 
                          '"destroy old data " option from admin panel')
 
                return
 
        repo_iter = CachedRepoList(all_repos, invalidation_list,
 
                                   repos_path=self.repos_path,
 
                                   order_by=sort_key)
 

	
 
            if alias == 'hg':
 
                repo = backend(repo_path, create=False, baseui=make_ui('db'))
 
                #skip hidden web repository
 
                if repo._get_hidden():
 
                    return
 
            else:
 
                repo = backend(repo_path, create=False)
 

	
 
            return repo
 

	
 
        pre_invalidate = True
 
        dbinvalidate = False
 

	
 
        if invalidation_list is not None:
 
            pre_invalidate = repo_name in invalidation_list
 

	
 
        if pre_invalidate:
 
            #this returns object to invalidate
 
            invalidate = self._should_invalidate(repo_name)
 
            if invalidate:
 
                log.info('invalidating cache for repository %s', repo_name)
 
                region_invalidate(_get_repo, None, repo_name)
 
                self._mark_invalidated(invalidate)
 
                dbinvalidate = True
 

	
 
        r, dbr = None, None
 
        if retval == 'repo' or 'all':
 
            r = _get_repo(repo_name)
 
        if retval == 'dbrepo' or 'all':
 
            dbr = RepoModel().get_full(repo_name, cache=True,
 
                                          invalidate=dbinvalidate)
 

	
 
        return r, dbr
 
        return repo_iter
 

	
 
    def mark_for_invalidation(self, repo_name):
 
        """Puts cache invalidation task into db for
 
        further global cache invalidation
 

	
 
        :param repo_name: this repo that should invalidation take place
 
        """
 

	
 
        log.debug('marking %s for invalidation', repo_name)
 
        cache = self.sa.query(CacheInvalidation)\
 
            .filter(CacheInvalidation.cache_key == repo_name).scalar()
 

	
rhodecode/templates/admin/users/user_edit.html
Show inline comments
 
@@ -56,25 +56,25 @@
 
                    <label for="ldap_dn">${_('LDAP DN')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('ldap_dn',class_='medium')}
 
                </div>
 
             </div>
 
            
 
             <div class="field">
 
                <div class="label">
 
                    <label for="new_password">${_('New password')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.password('new_password',class_='medium')}
 
                    ${h.password('new_password',class_='medium',autocomplete="off")}
 
                </div>
 
             </div>
 
            
 
             <div class="field">
 
                <div class="label">
 
                    <label for="name">${_('First Name')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('name',class_='medium')}
 
                </div>
 
             </div>
 
            
rhodecode/templates/admin/users/user_edit_my_account.html
Show inline comments
 
@@ -45,25 +45,25 @@
 
	                    <label for="username">${_('Username')}:</label>
 
	                </div>
 
	                <div class="input">
 
	                    ${h.text('username',class_="medium")}
 
	                </div>
 
	             </div>
 
	            
 
	             <div class="field">
 
	                <div class="label">
 
	                    <label for="new_password">${_('New password')}:</label>
 
	                </div>
 
	                <div class="input">
 
	                    ${h.password('new_password',class_="medium")}
 
	                    ${h.password('new_password',class_="medium",autocomplete="off")}
 
	                </div>
 
	             </div>
 
	            
 
	             <div class="field">
 
	                <div class="label">
 
	                    <label for="name">${_('First Name')}:</label>
 
	                </div>
 
	                <div class="input">
 
	                    ${h.text('name',class_="medium")}
 
	                </div>
 
	             </div>
 
	            
rhodecode/tests/__init__.py
Show inline comments
 
"""Pylons application test package
 

	
 
This package assumes the Pylons environment is already loaded, such as
 
when this script is imported from the `nosetests --with-pylons=test.ini`
 
command.
 

	
 
This module initializes the application via ``websetup`` (`paster
 
setup-app`) and provides the base testing objects.
 
"""
 
import os
 
from os.path import join as jn
 

	
 
from unittest import TestCase
 

	
 
from paste.deploy import loadapp
 
from paste.script.appinstall import SetupCommand
 
from pylons import config, url
 
from routes.util import URLGenerator
 
from webtest import TestApp
 
import os
 

	
 
from rhodecode.model import meta
 
import logging
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 
import pylons.test
 

	
 
__all__ = ['environ', 'url', 'TestController', 'TESTS_TMP_PATH', 'HG_REPO',
 
           'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO', 'HG_FORK', 'GIT_FORK', ]
 

	
 
# Invoke websetup with the current config file
 
#SetupCommand('setup-app').run([config_file])
 

	
 
##RUNNING DESIRED TESTS
 
#nosetests -x rhodecode.tests.functional.test_admin_settings:TestSettingsController.test_my_account
 

	
 
environ = {}
 

	
 
#SOME GLOBALS FOR TESTS
 
TESTS_TMP_PATH = '/tmp'
 
TESTS_TMP_PATH = jn('/', 'tmp')
 

	
 
HG_REPO = 'vcs_test_hg'
 
GIT_REPO = 'vcs_test_git'
 

	
 
NEW_HG_REPO = 'vcs_test_hg_new'
 
NEW_GIT_REPO = 'vcs_test_git_new'
 

	
 
HG_FORK = 'vcs_test_hg_fork'
 
GIT_FORK = 'vcs_test_git_fork'
 

	
 
class TestController(TestCase):
 

	
 
@@ -55,17 +58,17 @@ class TestController(TestCase):
 
        self.app = TestApp(wsgiapp)
 
        url._push_object(URLGenerator(config['routes.map'], environ))
 
        self.sa = meta.Session
 
        self.index_location = config['app_conf']['index_dir']
 
        TestCase.__init__(self, *args, **kwargs)
 

	
 
    def log_user(self, username='test_admin', password='test12'):
 
        response = self.app.post(url(controller='login', action='index'),
 
                                 {'username':username,
 
                                  'password':password})
 

	
 
        if 'invalid user name' in response.body:
 
            assert False, 'could not login using %s %s' % (username, password)
 
            self.fail('could not login using %s %s' % (username, password))
 

	
 
        assert response.status == '302 Found', 'Wrong response code from login got %s' % response.status
 
        assert response.session['rhodecode_user'].username == username, 'wrong logged in user got %s expected %s' % (response.session['rhodecode_user'].username, username)
 
        self.assertEqual(response.status, '302 Found')
 
        self.assertEqual(response.session['rhodecode_user'].username, username)
 
        return response.follow()
rhodecode/tests/functional/test_admin_repos.py
Show inline comments
 
import os
 
import vcs
 

	
 
from rhodecode.model.db import Repository
 
from rhodecode.tests import *
 

	
 
class TestAdminReposController(TestController):
 

	
 

	
 
    def __make_repo(self):
 
        pass
 

	
 

	
 
    def test_index(self):
 
        self.log_user()
 
        response = self.app.get(url('repos'))
 
        # Test response...
 

	
 
    def test_index_as_xml(self):
 
        response = self.app.get(url('formatted_repos', format='xml'))
 

	
 
    def test_create_hg(self):
 
        self.log_user()
 
        repo_name = NEW_HG_REPO
 
        description = 'description for newly created repo'
 
        private = False
 
        response = self.app.post(url('repos'), {'repo_name':repo_name,
 
                                                'repo_type':'hg',
 
                                                'clone_uri':'',
 
                                                'repo_group':'',
 
                                                'description':description,
 
                                                'private':private})
 

	
 
        self.assertTrue('flash' in response.session)
 

	
 
        #test if we have a message for that repository
 
        assert '''created repository %s''' % (repo_name) in response.session['flash'][0], 'No flash message about new repo'
 
        self.assertTrue('''created repository %s''' % (repo_name) in
 
                        response.session['flash'][0])
 

	
 
        #test if the fork was created in the database
 
        new_repo = self.sa.query(Repository).filter(Repository.repo_name == repo_name).one()
 
        #test if the repo was created in the database
 
        new_repo = self.sa.query(Repository).filter(Repository.repo_name ==
 
                                                    repo_name).one()
 

	
 
        assert new_repo.repo_name == repo_name, 'wrong name of repo name in db'
 
        assert new_repo.description == description, 'wrong description'
 
        self.assertEqual(new_repo.repo_name, repo_name)
 
        self.assertEqual(new_repo.description, description)
 

	
 
        #test if repository is visible in the list ?
 
        response = response.follow()
 

	
 
        assert repo_name in response.body, 'missing new repo from the main repos list'
 
        self.assertTrue(repo_name in response.body)
 

	
 

	
 
        #test if repository was created on filesystem
 
        try:
 
            vcs.get_repo(os.path.join(TESTS_TMP_PATH, repo_name))
 
        except:
 
            assert False , 'no repo in filesystem'
 
            self.fail('no repo in filesystem')
 

	
 

	
 
    def test_create_hg_in_group(self):
 
        #TODO: write test !
 
        pass
 

	
 
    def test_create_git(self):
 
        return
 
        self.log_user()
 
        repo_name = NEW_GIT_REPO
 
        description = 'description for newly created repo'
 
        private = False
 
        response = self.app.post(url('repos'), {'repo_name':repo_name,
 
                                                'repo_type':'git',
 
                                                'clone_uri':'',
 
                                                'repo_group':'',
 
                                                'description':description,
 
                                                'private':private})
 

	
 

	
 
        #test if we have a message for that repository
 
        assert '''created repository %s''' % (repo_name) in response.session['flash'][0], 'No flash message about new repo'
 

	
 
        #test if the fork was created in the database
 
        new_repo = self.sa.query(Repository).filter(Repository.repo_name == repo_name).one()
 

	
 
        assert new_repo.repo_name == repo_name, 'wrong name of repo name in db'
 
        assert new_repo.description == description, 'wrong description'
 
@@ -81,67 +96,83 @@ class TestAdminReposController(TestContr
 

	
 
    def test_new(self):
 
        self.log_user()
 
        response = self.app.get(url('new_repo'))
 

	
 
    def test_new_as_xml(self):
 
        response = self.app.get(url('formatted_new_repo', format='xml'))
 

	
 
    def test_update(self):
 
        response = self.app.put(url('repo', repo_name=HG_REPO))
 

	
 
    def test_update_browser_fakeout(self):
 
        response = self.app.post(url('repo', repo_name=HG_REPO), params=dict(_method='put'))
 
        response = self.app.post(url('repo', repo_name=HG_REPO),
 
                                 params=dict(_method='put'))
 

	
 
    def test_delete(self):
 
        self.log_user()
 
        repo_name = 'vcs_test_new_to_delete'
 
        description = 'description for newly created repo'
 
        private = False
 

	
 
        response = self.app.post(url('repos'), {'repo_name':repo_name,
 
                                                'repo_type':'hg',
 
                                               'description':description,
 
                                               'private':private})
 

	
 
                                                'clone_uri':'',
 
                                                'repo_group':'',
 
                                                'description':description,
 
                                                'private':private})
 
        self.assertTrue('flash' in response.session)
 

	
 
        #test if we have a message for that repository
 
        assert '''created repository %s''' % (repo_name) in response.session['flash'][0], 'No flash message about new repo'
 
        self.assertTrue('''created repository %s''' % (repo_name) in
 
                        response.session['flash'][0])
 

	
 
        #test if the repo was created in the database
 
        new_repo = self.sa.query(Repository).filter(Repository.repo_name == repo_name).one()
 
        new_repo = self.sa.query(Repository).filter(Repository.repo_name ==
 
                                                    repo_name).one()
 

	
 
        assert new_repo.repo_name == repo_name, 'wrong name of repo name in db'
 
        assert new_repo.description == description, 'wrong description'
 
        self.assertEqual(new_repo.repo_name, repo_name)
 
        self.assertEqual(new_repo.description, description)
 

	
 
        #test if repository is visible in the list ?
 
        response = response.follow()
 

	
 
        assert repo_name in response.body, 'missing new repo from the main repos list'
 
        self.assertTrue(repo_name in response.body)
 

	
 

	
 
        response = self.app.delete(url('repo', repo_name=repo_name))
 

	
 
        assert '''deleted repository %s''' % (repo_name) in response.session['flash'][0], 'No flash message about delete repo'
 
        self.assertTrue('''deleted repository %s''' % (repo_name) in
 
                        response.session['flash'][0])
 

	
 
        response.follow()
 

	
 
        #check if repo was deleted from db
 
        deleted_repo = self.sa.query(Repository).filter(Repository.repo_name == repo_name).scalar()
 
        deleted_repo = self.sa.query(Repository).filter(Repository.repo_name
 
                                                        == repo_name).scalar()
 

	
 
        self.assertEqual(deleted_repo, None)
 

	
 
        assert deleted_repo is None, 'Deleted repository was found in db'
 

	
 
    def test_delete_repo_with_group(self):
 
        #TODO:
 
        pass
 

	
 

	
 
    def test_delete_browser_fakeout(self):
 
        response = self.app.post(url('repo', repo_name=HG_REPO), params=dict(_method='delete'))
 
        response = self.app.post(url('repo', repo_name=HG_REPO),
 
                                 params=dict(_method='delete'))
 

	
 
    def test_show(self):
 
        self.log_user()
 
        response = self.app.get(url('repo', repo_name=HG_REPO))
 

	
 
    def test_show_as_xml(self):
 
        response = self.app.get(url('formatted_repo', repo_name=HG_REPO, format='xml'))
 
        response = self.app.get(url('formatted_repo', repo_name=HG_REPO,
 
                                    format='xml'))
 

	
 
    def test_edit(self):
 
        response = self.app.get(url('edit_repo', repo_name=HG_REPO))
 

	
 
    def test_edit_as_xml(self):
 
        response = self.app.get(url('formatted_edit_repo', repo_name=HG_REPO, format='xml'))
 
        response = self.app.get(url('formatted_edit_repo', repo_name=HG_REPO,
 
                                    format='xml'))
rhodecode/tests/functional/test_admin_settings.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
from rhodecode.lib.auth import get_crypt_password, check_password
 
from rhodecode.model.db import User, RhodeCodeSettings
 
from rhodecode.tests import *
 

	
 
class TestAdminSettingsController(TestController):
 

	
 
    def test_index(self):
 
        response = self.app.get(url('admin_settings'))
 
        # Test response...
 

	
 
    def test_index_as_xml(self):
 
        response = self.app.get(url('formatted_admin_settings', format='xml'))
 
@@ -33,110 +35,123 @@ class TestAdminSettingsController(TestCo
 
        response = self.app.post(url('admin_setting', setting_id=1), params=dict(_method='delete'))
 

	
 
    def test_show(self):
 
        response = self.app.get(url('admin_setting', setting_id=1))
 

	
 
    def test_show_as_xml(self):
 
        response = self.app.get(url('formatted_admin_setting', setting_id=1, format='xml'))
 

	
 
    def test_edit(self):
 
        response = self.app.get(url('admin_edit_setting', setting_id=1))
 

	
 
    def test_edit_as_xml(self):
 
        response = self.app.get(url('formatted_admin_edit_setting', setting_id=1, format='xml'))
 
        response = self.app.get(url('formatted_admin_edit_setting',
 
                                    setting_id=1, format='xml'))
 

	
 

	
 
    def test_ga_code_active(self):
 
        self.log_user()
 
        old_title = 'RhodeCode'
 
        old_realm = 'RhodeCode authentication'
 
        new_ga_code = 'ga-test-123456789'
 
        response = self.app.post(url('admin_setting', setting_id='global'),
 
                                     params=dict(
 
                                                 _method='put',
 
                                                 rhodecode_title=old_title,
 
                                                 rhodecode_realm=old_realm,
 
                                                 rhodecode_ga_code=new_ga_code
 
                                                 ))
 

	
 
        assert 'Updated application settings' in response.session['flash'][0][1], 'no flash message about success of change'
 
        assert RhodeCodeSettings.get_app_settings()['rhodecode_ga_code'] == new_ga_code, 'change not in database'
 
        self.assertTrue('Updated application settings' in
 
                        response.session['flash'][0][1])
 
        self.assertEqual(RhodeCodeSettings
 
                         .get_app_settings()['rhodecode_ga_code'], new_ga_code)
 

	
 
        response = response.follow()
 
        assert """_gaq.push(['_setAccount', '%s']);""" % new_ga_code in response.body
 
        self.assertTrue("""_gaq.push(['_setAccount', '%s']);""" % new_ga_code
 
                        in response.body)
 

	
 
    def test_ga_code_inactive(self):
 
        self.log_user()
 
        old_title = 'RhodeCode'
 
        old_realm = 'RhodeCode authentication'
 
        new_ga_code = ''
 
        response = self.app.post(url('admin_setting', setting_id='global'),
 
                                     params=dict(
 
                                                 _method='put',
 
                                                 rhodecode_title=old_title,
 
                                                 rhodecode_realm=old_realm,
 
                                                 rhodecode_ga_code=new_ga_code
 
                                                 ))
 

	
 
        assert 'Updated application settings' in response.session['flash'][0][1], 'no flash message about success of change'
 
        assert RhodeCodeSettings.get_app_settings()['rhodecode_ga_code'] == new_ga_code, 'change not in database'
 
        self.assertTrue('Updated application settings' in
 
                        response.session['flash'][0][1])
 
        self.assertEqual(RhodeCodeSettings
 
                        .get_app_settings()['rhodecode_ga_code'], new_ga_code)
 

	
 
        response = response.follow()
 
        assert """_gaq.push(['_setAccount', '%s']);""" % new_ga_code not in response.body
 
        self.assertTrue("""_gaq.push(['_setAccount', '%s']);""" % new_ga_code
 
                        not in response.body)
 

	
 

	
 
    def test_title_change(self):
 
        self.log_user()
 
        old_title = 'RhodeCode'
 
        new_title = old_title + '_changed'
 
        old_realm = 'RhodeCode authentication'
 
        response = self.app.post(url('admin_setting', setting_id='global'),
 
                                     params=dict(
 
                                                 _method='put',
 
                                                 rhodecode_title=new_title,
 
                                                 rhodecode_realm=old_realm,
 
                                                 rhodecode_ga_code=''
 
                                                 ))
 

	
 
        for new_title in ['Changed', 'Żółwik', old_title]:
 
            response = self.app.post(url('admin_setting', setting_id='global'),
 
                                         params=dict(
 
                                                     _method='put',
 
                                                     rhodecode_title=new_title,
 
                                                     rhodecode_realm=old_realm,
 
                                                     rhodecode_ga_code=''
 
                                                     ))
 

	
 

	
 
        assert 'Updated application settings' in response.session['flash'][0][1], 'no flash message about success of change'
 
        assert RhodeCodeSettings.get_app_settings()['rhodecode_title'] == new_title, 'change not in database'
 
            self.assertTrue('Updated application settings' in
 
                            response.session['flash'][0][1])
 
            self.assertEqual(RhodeCodeSettings
 
                             .get_app_settings()['rhodecode_title'],
 
                             new_title.decode('utf-8'))
 

	
 
        response = response.follow()
 
        assert """<h1><a href="/">%s</a></h1>""" % new_title in response.body
 
            response = response.follow()
 
            self.assertTrue("""<h1><a href="/">%s</a></h1>""" % new_title
 
                        in response.body)
 

	
 

	
 
    def test_my_account(self):
 
        self.log_user()
 
        response = self.app.get(url('admin_settings_my_account'))
 
        print response
 
        assert 'value="test_admin' in response.body
 

	
 
        self.assertTrue('value="test_admin' in response.body)
 

	
 
    def test_my_account_update(self):
 
        self.log_user()
 

	
 
        new_email = 'new@mail.pl'
 
        new_name = 'NewName'
 
        new_lastname = 'NewLastname'
 
        new_password = 'test123'
 

	
 

	
 
        response = self.app.post(url('admin_settings_my_account_update'), params=dict(
 
                                                            _method='put',
 
                                                            username='test_admin',
 
                                                            new_password=new_password,
 
                                                            password='',
 
                                                            name=new_name,
 
                                                            lastname=new_lastname,
 
                                                            email=new_email,))
 
        response = self.app.post(url('admin_settings_my_account_update'),
 
                                 params=dict(_method='put',
 
                                             username='test_admin',
 
                                             new_password=new_password,
 
                                             password='',
 
                                             name=new_name,
 
                                             lastname=new_lastname,
 
                                             email=new_email,))
 
        response.follow()
 

	
 
        assert 'Your account was updated successfully' in response.session['flash'][0][1], 'no flash message about success of change'
 
        user = self.sa.query(User).filter(User.username == 'test_admin').one()
 
        assert user.email == new_email , 'incorrect user email after update got %s vs %s' % (user.email, new_email)
 
        assert user.name == new_name, 'updated field mismatch %s vs %s' % (user.name, new_name)
 
        assert user.lastname == new_lastname, 'updated field mismatch %s vs %s' % (user.lastname, new_lastname)
 
        assert check_password(new_password, user.password) is True, 'password field mismatch %s vs %s' % (user.password, new_password)
 

	
 
        #bring back the admin settings
 
        old_email = 'test_admin@mail.com'
 
        old_name = 'RhodeCode'
rhodecode/tests/functional/test_login.py
Show inline comments
 
@@ -36,29 +36,29 @@ class TestLoginController(TestController
 
        response = self.app.post(url(controller='login', action='index', came_from=test_came_from),
 
                                 {'username':'test_admin',
 
                                  'password':'test12'})
 
        assert response.status == '302 Found', 'Wrong response code from came from redirection'
 
        response = response.follow()
 

	
 
        assert response.status == '200 OK', 'Wrong response from login page got %s' % response.status
 
        assert 'Users administration' in response.body, 'No proper title in response'
 

	
 

	
 
    def test_login_short_password(self):
 
        response = self.app.post(url(controller='login', action='index'),
 
                                 {'username':'error',
 
                                  'password':'test'})
 
        assert response.status == '200 OK', 'Wrong response from login page'
 
                                 {'username':'test_admin',
 
                                  'password':'as'})
 
        self.assertEqual(response.status, '200 OK')
 
        print response.body
 
        assert 'Enter 6 characters or more' in response.body, 'No error password message in response'
 
        self.assertTrue('Enter 3 characters or more' in response.body)
 

	
 
    def test_login_wrong_username_password(self):
 
        response = self.app.post(url(controller='login', action='index'),
 
                                 {'username':'error',
 
                                  'password':'test12'})
 
        assert response.status == '200 OK', 'Wrong response from login page'
 

	
 
        assert 'invalid user name' in response.body, 'No error username message in response'
 
        assert 'invalid password' in response.body, 'No error password message in response'
 

	
 
    #==========================================================================
 
    # REGISTRATIONS
rhodecode/tests/functional/test_summary.py
Show inline comments
 
from rhodecode.tests import *
 
from rhodecode.model.db import Repository
 
from rhodecode.lib.utils import invalidate_cache
 

	
 
class TestSummaryController(TestController):
 

	
 
    def test_index(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='summary', action='index', repo_name=HG_REPO))
 
        response = self.app.get(url(controller='summary',
 
                                    action='index', repo_name=HG_REPO))
 

	
 
        #repo type
 
        assert """<img style="margin-bottom:2px" class="icon" title="Mercurial repository" alt="Mercurial repository" src="/images/icons/hgicon.png"/>""" in response.body
 
        assert """<img style="margin-bottom:2px" class="icon" title="public repository" alt="public repository" src="/images/icons/lock_open.png"/>""" in response.body
 
        self.assertTrue("""<img style="margin-bottom:2px" class="icon" """
 
                        """title="Mercurial repository" alt="Mercurial """
 
                        """repository" src="/images/icons/hgicon.png"/>"""
 
                        in response.body)
 
        self.assertTrue("""<img style="margin-bottom:2px" class="icon" """
 
                        """title="public repository" alt="public """
 
                        """repository" src="/images/icons/lock_open.png"/>"""
 
                        in response.body)
 

	
 
        #codes stats
 
        self._enable_stats()
 

	
 

	
 
        self._enable_stats()
 
        invalidate_cache('get_repo_cached_%s' % HG_REPO)
 
        response = self.app.get(url(controller='summary', action='index', repo_name=HG_REPO))
 
        assert """var data = {"Python": 42, "Rst": 11, "Bash": 2, "Makefile": 1, "Batch": 1, "Ini": 1, "Css": 1};""" in response.body, 'wrong info about % of codes stats'
 
        response = self.app.get(url(controller='summary', action='index',
 
                                    repo_name=HG_REPO))
 

	
 
        self.assertTrue("""var data = {"py": {"count": 42, "desc": """
 
                        """["Python"]}, "rst": {"count": 11, "desc": """
 
                        """["Rst"]}, "sh": {"count": 2, "desc": ["Bash"]}, """
 
                        """"makefile": {"count": 1, "desc": ["Makefile", """
 
                        """"Makefile"]}, "cfg": {"count": 1, "desc": ["Ini"]},"""
 
                        """ "css": {"count": 1, "desc": ["Css"]}, "bat": """
 
                        """{"count": 1, "desc": ["Batch"]}};"""
 
                        in response.body)
 

	
 
        # clone url...
 
        assert """<input type="text" id="clone_url" readonly="readonly" value="hg clone http://test_admin@localhost:80/%s" size="70"/>""" % HG_REPO in response.body
 
        self.assertTrue("""<input type="text" id="clone_url" readonly="readonly" value="hg clone http://test_admin@localhost:80/%s" size="70"/>""" % HG_REPO in response.body)
 

	
 

	
 
    def _enable_stats(self):
 
        r = Repository.by_repo_name(HG_REPO)
 
        r.enable_statistics = True
 
        self.sa.add(r)
 
        self.sa.commit()
test.ini
Show inline comments
 
################################################################################
 
################################################################################
 
# RhodeCode - Pylons environment configuration                                 #
 
#                                                                              # 
 
# The %(here)s variable will be replaced with the parent directory of this file#
 
################################################################################
 

	
 
[DEFAULT]
 
debug = true
 
pdebug = false
 
################################################################################
 
## Uncomment and replace with the address which should receive                ## 
 
## any error reports after application crash                                  ##
 
## Additionally those settings will be used by RhodeCode mailing system       ##
 
################################################################################
 
#email_to = admin@localhost
 
#error_email_from = paste_error@localhost
 
#app_email_from = rhodecode-noreply@localhost
 
#error_message =
 

	
 
#smtp_server = mail.server.com
 
#smtp_username = 
0 comments (0 inline, 0 general)