comparison rhodecode/lib/base.py @ 1761:b265be1c6093 beta

Wrapped calls for git and hg middleware in extra block that clears db Session. tries to fix #318
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 07 Dec 2011 22:08:12 +0200
parents 8ecc6b8229a5
children d09c52be40e0
comparison
equal deleted inserted replaced
1760:9dda1146327c 1761:b265be1c6093
2 2
3 Provides the BaseController class for subclassing. 3 Provides the BaseController class for subclassing.
4 """ 4 """
5 import logging 5 import logging
6 import time 6 import time
7
8 from paste.auth.basic import AuthBasicAuthenticator
9
7 from pylons import config, tmpl_context as c, request, session, url 10 from pylons import config, tmpl_context as c, request, session, url
8 from pylons.controllers import WSGIController 11 from pylons.controllers import WSGIController
9 from pylons.controllers.util import redirect 12 from pylons.controllers.util import redirect
10 from pylons.templating import render_mako as render 13 from pylons.templating import render_mako as render
11 14
12 from rhodecode import __version__, BACKENDS 15 from rhodecode import __version__, BACKENDS
13 16
14 from rhodecode.lib import str2bool 17 from rhodecode.lib import str2bool
15 from rhodecode.lib.auth import AuthUser, get_container_username 18 from rhodecode.lib.auth import AuthUser, get_container_username, authfunc,\
16 from rhodecode.lib.utils import get_repo_slug 19 HasPermissionAnyMiddleware
20 from rhodecode.lib.utils import get_repo_slug, invalidate_cache
17 from rhodecode.model import meta 21 from rhodecode.model import meta
18 22
19 from rhodecode.model.db import Repository 23 from rhodecode.model.db import Repository
20 from rhodecode.model.notification import NotificationModel 24 from rhodecode.model.notification import NotificationModel
21 from rhodecode.model.scm import ScmModel 25 from rhodecode.model.scm import ScmModel
22 26
23 log = logging.getLogger(__name__) 27 log = logging.getLogger(__name__)
28
29 class BaseVCSController(object):
30
31 def __init__(self, application, config):
32 self.application = application
33 self.config = config
34 # base path of repo locations
35 self.basepath = self.config['base_path']
36 #authenticate this mercurial request using authfunc
37 self.authenticate = AuthBasicAuthenticator('', authfunc)
38 self.ipaddr = '0.0.0.0'
39
40 def _invalidate_cache(self, repo_name):
41 """
42 Set's cache for this repository for invalidation on next access
43
44 :param repo_name: full repo name, also a cache key
45 """
46 invalidate_cache('get_repo_cached_%s' % repo_name)
47
48 def _check_permission(self, action, user, repo_name):
49 """
50 Checks permissions using action (push/pull) user and repository
51 name
52
53 :param action: push or pull action
54 :param user: user instance
55 :param repo_name: repository name
56 """
57 if action == 'push':
58 if not HasPermissionAnyMiddleware('repository.write',
59 'repository.admin')(user,
60 repo_name):
61 return False
62
63 else:
64 #any other action need at least read permission
65 if not HasPermissionAnyMiddleware('repository.read',
66 'repository.write',
67 'repository.admin')(user,
68 repo_name):
69 return False
70
71 return True
72
73 def __call__(self, environ, start_response):
74 start = time.time()
75 try:
76 return self._handle_request(environ, start_response)
77 finally:
78 log = logging.getLogger(self.__class__.__name__)
79 log.debug('Request time: %.3fs' % (time.time() - start))
80 meta.Session.remove()
81
24 82
25 class BaseController(WSGIController): 83 class BaseController(WSGIController):
26 84
27 def __before__(self): 85 def __before__(self):
28 c.rhodecode_version = __version__ 86 c.rhodecode_version = __version__