# HG changeset patch # User Marcin Kuzminski # Date 2017-03-16 14:05:58 # Node ID 8ba8e3552609465bb912ea5d66d16bf06778c049 # Parent f3cfafeeed4889f5152d7f4135a3e1918c1917ca core: added supports of repo based views via pyramid. diff --git a/rhodecode/apps/_base/__init__.py b/rhodecode/apps/_base/__init__.py --- a/rhodecode/apps/_base/__init__.py +++ b/rhodecode/apps/_base/__init__.py @@ -25,7 +25,9 @@ from pyramid.httpexceptions import HTTPF from rhodecode.lib import helpers as h from rhodecode.lib.utils2 import StrictAttributeDict, safe_int +from rhodecode.model import repo from rhodecode.model.db import User +from rhodecode.model.scm import ScmModel log = logging.getLogger(__name__) @@ -110,3 +112,52 @@ class BaseAppView(object): """ raise NotImplementedError('Needs implementation in view class') + +class RepoAppView(BaseAppView): + + def __init__(self, context, request): + super(RepoAppView, self).__init__(context, request) + self.db_repo = request.db_repo + self.db_repo_name = self.db_repo.repo_name + self.db_repo_pull_requests = ScmModel().get_pull_requests(self.db_repo) + + def _get_local_tmpl_context(self): + c = super(RepoAppView, self)._get_local_tmpl_context() + # register common vars for this type of view + c.rhodecode_db_repo = self.db_repo + c.repo_name = self.db_repo_name + c.repository_pull_requests = self.db_repo_pull_requests + return c + + +class RepoRoutePredicate(object): + def __init__(self, val, config): + self.val = val + + def text(self): + return 'repo_route = %s' % self.val + + phash = text + + def __call__(self, info, request): + repo_name = info['match']['repo_name'] + repo_model = repo.RepoModel() + by_name_match = repo_model.get_by_repo_name(repo_name, cache=True) + # if we match quickly from database, short circuit the operation, + # and validate repo based on the type. + if by_name_match: + # register this as request object we can re-use later + request.db_repo = by_name_match + return True + + by_id_match = repo_model.get_repo_by_id(repo_name) + if by_id_match: + request.db_repo = by_id_match + return True + + return False + + +def includeme(config): + config.add_route_predicate( + 'repo_route', RepoRoutePredicate) diff --git a/rhodecode/config/middleware.py b/rhodecode/config/middleware.py --- a/rhodecode/config/middleware.py +++ b/rhodecode/config/middleware.py @@ -283,6 +283,8 @@ def includeme(config): config.include('rhodecode.integrations') # apps + config.include('rhodecode.apps._base') + config.include('rhodecode.apps.admin') config.include('rhodecode.apps.channelstream') config.include('rhodecode.apps.login')