annotate rhodecode/lib/base.py @ 2374:be2163ef127e beta

Add ip reference into BaseController
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 03 Jun 2012 00:37:12 +0200
parents 79e4d6b9c1f0
children 1bc579bcd67a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 """The base Controller API
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 Provides the BaseController class for subclassing.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
4 """
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
5 import logging
1601
6f06daeed294 Added Request time tracking
Marcin Kuzminski <marcin@python-works.com>
parents: 1530
diff changeset
6 import time
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
7 import traceback
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
8
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
9 from paste.auth.basic import AuthBasicAuthenticator
2132
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
10 from paste.httpexceptions import HTTPUnauthorized, HTTPForbidden
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
11 from paste.httpheaders import WWW_AUTHENTICATE
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
12
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
13 from pylons import config, tmpl_context as c, request, session, url
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14 from pylons.controllers import WSGIController
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
15 from pylons.controllers.util import redirect
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
16 from pylons.templating import render_mako as render
1304
5a96551ee9c0 gui-improvments
Marcin Kuzminski <marcin@python-works.com>
parents: 1282
diff changeset
17
1718
f78bee8eec78 reduce cookie size for better support of client side sessions
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
18 from rhodecode import __version__, BACKENDS
f78bee8eec78 reduce cookie size for better support of client side sessions
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
19
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents: 2045
diff changeset
20 from rhodecode.lib.utils2 import str2bool, safe_unicode
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
21 from rhodecode.lib.auth import AuthUser, get_container_username, authfunc,\
2030
61f9aeb2129e Added session wrapper, for rc 1.2.X compatibility. Adds backwards compatability
Marcin Kuzminski <marcin@python-works.com>
parents: 2027
diff changeset
22 HasPermissionAnyMiddleware, CookieStoreWrapper
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
23 from rhodecode.lib.utils import get_repo_slug, invalidate_cache
547
1e757ac98988 renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents: 537
diff changeset
24 from rhodecode.model import meta
1718
f78bee8eec78 reduce cookie size for better support of client side sessions
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
25
1366
9c0f5d558789 fixes #200, rewrote the whole caching mechanism to get rid of such problems. Now cached instances are attached
Marcin Kuzminski <marcin@python-works.com>
parents: 1351
diff changeset
26 from rhodecode.model.db import Repository
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
27 from rhodecode.model.notification import NotificationModel
1718
f78bee8eec78 reduce cookie size for better support of client side sessions
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
28 from rhodecode.model.scm import ScmModel
665
070f32743632 Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents: 659
diff changeset
29
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
30 log = logging.getLogger(__name__)
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1304
diff changeset
31
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
32
2374
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
33 def _get_ip_addr(environ):
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
34 proxy_key = 'HTTP_X_REAL_IP'
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
35 proxy_key2 = 'HTTP_X_FORWARDED_FOR'
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
36 def_key = 'REMOTE_ADDR'
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
37
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
38 return environ.get(proxy_key2,
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
39 environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
40 )
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
41
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
42
2132
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
43 class BasicAuth(AuthBasicAuthenticator):
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
44
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
45 def __init__(self, realm, authfunc, auth_http_code=None):
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
46 self.realm = realm
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
47 self.authfunc = authfunc
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
48 self._rc_auth_http_code = auth_http_code
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
49
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
50 def build_authentication(self):
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
51 head = WWW_AUTHENTICATE.tuples('Basic realm="%s"' % self.realm)
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
52 if self._rc_auth_http_code and self._rc_auth_http_code == '403':
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
53 # return 403 if alternative http return code is specified in
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
54 # RhodeCode config
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
55 return HTTPForbidden(headers=head)
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
56 return HTTPUnauthorized(headers=head)
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
57
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
58
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
59 class BaseVCSController(object):
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
60
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
61 def __init__(self, application, config):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
62 self.application = application
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
63 self.config = config
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
64 # base path of repo locations
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
65 self.basepath = self.config['base_path']
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
66 #authenticate this mercurial request using authfunc
2132
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
67 self.authenticate = BasicAuth('', authfunc,
9f5582151d53 Alternative HTTP response codes when client failed to Authenticate correctly
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
68 config.get('auth_ret_code'))
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
69 self.ipaddr = '0.0.0.0'
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
70
1978
164199e476e9 code garden, pep8
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
71 def _handle_request(self, environ, start_response):
164199e476e9 code garden, pep8
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
72 raise NotImplementedError()
164199e476e9 code garden, pep8
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
73
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
74 def _get_by_id(self, repo_name):
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
75 """
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
76 Get's a special pattern _<ID> from clone url and tries to replace it
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
77 with a repository_name for support of _<ID> non changable urls
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
78
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
79 :param repo_name:
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
80 """
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
81 try:
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
82 data = repo_name.split('/')
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
83 if len(data) >= 2:
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
84 by_id = data[1].split('_')
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
85 if len(by_id) == 2 and by_id[1].isdigit():
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
86 _repo_name = Repository.get(by_id[1]).repo_name
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
87 data[1] = _repo_name
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
88 except:
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
89 log.debug('Failed to extract repo_name from id %s' % (
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
90 traceback.format_exc()
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
91 )
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
92 )
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
93
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
94 return '/'.join(data)
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
95
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
96 def _invalidate_cache(self, repo_name):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
97 """
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
98 Set's cache for this repository for invalidation on next access
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
99
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
100 :param repo_name: full repo name, also a cache key
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
101 """
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
102 invalidate_cache('get_repo_cached_%s' % repo_name)
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
103
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
104 def _check_permission(self, action, user, repo_name):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
105 """
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
106 Checks permissions using action (push/pull) user and repository
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
107 name
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
108
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
109 :param action: push or pull action
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
110 :param user: user instance
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
111 :param repo_name: repository name
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
112 """
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
113 if action == 'push':
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
114 if not HasPermissionAnyMiddleware('repository.write',
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
115 'repository.admin')(user,
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
116 repo_name):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
117 return False
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
118
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
119 else:
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
120 #any other action need at least read permission
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
121 if not HasPermissionAnyMiddleware('repository.read',
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
122 'repository.write',
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
123 'repository.admin')(user,
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
124 repo_name):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
125 return False
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
126
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
127 return True
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1763
diff changeset
128
2184
79e4d6b9c1f0 Added HTTP_X_FORWARDED_FOR as another method of extracting IP for pull/push logs.
Marcin Kuzminski <marcin@python-works.com>
parents: 2132
diff changeset
129 def _get_ip_addr(self, environ):
2374
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
130 return _get_ip_addr(environ)
2184
79e4d6b9c1f0 Added HTTP_X_FORWARDED_FOR as another method of extracting IP for pull/push logs.
Marcin Kuzminski <marcin@python-works.com>
parents: 2132
diff changeset
131
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
132 def __call__(self, environ, start_response):
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
133 start = time.time()
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
134 try:
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
135 return self._handle_request(environ, start_response)
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
136 finally:
1763
d09c52be40e0 logger name update
Marcin Kuzminski <marcin@python-works.com>
parents: 1761
diff changeset
137 log = logging.getLogger('rhodecode.' + self.__class__.__name__)
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
138 log.debug('Request time: %.3fs' % (time.time() - start))
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
139 meta.Session.remove()
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
140
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
141
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
142 class BaseController(WSGIController):
659
758f64f3fbda extended repo creation by repo type. fixed fork creation to maintain repo type.
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
143
185
3380ca40cdba added version generation to pylons_app and showed it into template. Propagated baseController with some data for acces into each controller. Fixed simplehg middleware to get proper name of application
Marcin Kuzminski <marcin@python-works.com>
parents: 169
diff changeset
144 def __before__(self):
548
b75b77ef649d renamed hg_app to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
145 c.rhodecode_version = __version__
2016
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 1978
diff changeset
146 c.rhodecode_instanceid = config.get('instance_id')
890
042d38683d42 implemented #89 google analytics code
Marcin Kuzminski <marcin@python-works.com>
parents: 812
diff changeset
147 c.rhodecode_name = config.get('rhodecode_title')
1629
2196aa27954b implements #293 gravatar link should be disabled when use_gravatar = false
Marcin Kuzminski <marcin@python-works.com>
parents: 1628
diff changeset
148 c.use_gravatar = str2bool(config.get('use_gravatar'))
891
cca7286401b3 fixes for #89 ga code
Marcin Kuzminski <marcin@python-works.com>
parents: 890
diff changeset
149 c.ga_code = config.get('rhodecode_ga_code')
185
3380ca40cdba added version generation to pylons_app and showed it into template. Propagated baseController with some data for acces into each controller. Fixed simplehg middleware to get proper name of application
Marcin Kuzminski <marcin@python-works.com>
parents: 169
diff changeset
150 c.repo_name = get_repo_slug(request)
659
758f64f3fbda extended repo creation by repo type. fixed fork creation to maintain repo type.
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
151 c.backends = BACKENDS.keys()
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
152 c.unread_notifications = NotificationModel()\
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
153 .get_unread_cnt_for_user(c.rhodecode_user.user_id)
890
042d38683d42 implemented #89 google analytics code
Marcin Kuzminski <marcin@python-works.com>
parents: 812
diff changeset
154 self.cut_off_limit = int(config.get('cut_off_limit'))
1036
405b80e4ccd5 Major refactoring, removed when possible calls to app globals.
Marcin Kuzminski <marcin@python-works.com>
parents: 891
diff changeset
155
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1728
diff changeset
156 self.sa = meta.Session
1045
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
157 self.scm_model = ScmModel(self.sa)
2374
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
158 self.ip_addr = ''
1366
9c0f5d558789 fixes #200, rewrote the whole caching mechanism to get rid of such problems. Now cached instances are attached
Marcin Kuzminski <marcin@python-works.com>
parents: 1351
diff changeset
159
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
160 def __call__(self, environ, start_response):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
161 """Invoke the Controller"""
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
162 # WSGIController.__call__ dispatches to the Controller method
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
163 # the request is routed to. This routing information is
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
164 # available in environ['pylons.routes_dict']
1601
6f06daeed294 Added Request time tracking
Marcin Kuzminski <marcin@python-works.com>
parents: 1530
diff changeset
165 start = time.time()
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
166 try:
2374
be2163ef127e Add ip reference into BaseController
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
167 self.ip_addr = _get_ip_addr(environ)
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
168 # make sure that we update permissions each time we call controller
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1045
diff changeset
169 api_key = request.GET.get('api_key')
2030
61f9aeb2129e Added session wrapper, for rc 1.2.X compatibility. Adds backwards compatability
Marcin Kuzminski <marcin@python-works.com>
parents: 2027
diff changeset
170 cookie_store = CookieStoreWrapper(session.get('rhodecode_user'))
1718
f78bee8eec78 reduce cookie size for better support of client side sessions
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
171 user_id = cookie_store.get('user_id', None)
1630
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1629
diff changeset
172 username = get_container_username(environ, config)
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
173 auth_user = AuthUser(user_id, api_key, username)
1728
07e56179633e - fixes celery sqlalchemy session issues for async forking
Marcin Kuzminski <marcin@python-works.com>
parents: 1718
diff changeset
174 request.user = auth_user
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
175 self.rhodecode_user = c.rhodecode_user = auth_user
1618
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1617
diff changeset
176 if not self.rhodecode_user.is_authenticated and \
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1617
diff changeset
177 self.rhodecode_user.user_id is not None:
2030
61f9aeb2129e Added session wrapper, for rc 1.2.X compatibility. Adds backwards compatability
Marcin Kuzminski <marcin@python-works.com>
parents: 2027
diff changeset
178 self.rhodecode_user.set_authenticated(
61f9aeb2129e Added session wrapper, for rc 1.2.X compatibility. Adds backwards compatability
Marcin Kuzminski <marcin@python-works.com>
parents: 2027
diff changeset
179 cookie_store.get('is_authenticated')
61f9aeb2129e Added session wrapper, for rc 1.2.X compatibility. Adds backwards compatability
Marcin Kuzminski <marcin@python-works.com>
parents: 2027
diff changeset
180 )
2027
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
181 log.info('User: %s accessed %s' % (
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
182 auth_user, safe_unicode(environ.get('PATH_INFO')))
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
183 )
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
184 return WSGIController.__call__(self, environ, start_response)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
185 finally:
2027
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
186 log.info('Request to %s time: %.3fs' % (
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
187 safe_unicode(environ.get('PATH_INFO')), time.time() - start)
88d5e42a66c3 fixed logging issue on non-ascii repos
Marcin Kuzminski <marcin@python-works.com>
parents: 2025
diff changeset
188 )
51
a699c0088344 fixed sqlalchemy session bug,
marcink
parents: 17
diff changeset
189 meta.Session.remove()
1045
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
190
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
191
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
192 class BaseRepoController(BaseController):
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
193 """
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
194 Base class for controllers responsible for loading all needed data for
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
195 repository loaded items are
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1117
diff changeset
196
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
197 c.rhodecode_repo: instance of scm repository
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
198 c.rhodecode_db_repo: instance of db
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
199 c.repository_followers: number of followers
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
200 c.repository_forks: number of forks
1045
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
201 """
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
202
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
203 def __before__(self):
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
204 super(BaseRepoController, self).__before__()
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
205 if c.repo_name:
3fc9183e05dd another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents: 1038
diff changeset
206
1530
04027bdb876c Refactoring of model get functions
Marcin Kuzminski <marcin@python-works.com>
parents: 1373
diff changeset
207 c.rhodecode_db_repo = Repository.get_by_repo_name(c.repo_name)
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
208 c.rhodecode_repo = c.rhodecode_db_repo.scm_instance
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
209
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
210 if c.rhodecode_repo is None:
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
211 log.error('%s this repository is present in database but it '
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
212 'cannot be created as an scm instance', c.repo_name)
1282
faaadfc3c359 fixed condition evaluated for gitrepo that returned null, simplified scm functions
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
213
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
214 redirect(url('home'))
1304
5a96551ee9c0 gui-improvments
Marcin Kuzminski <marcin@python-works.com>
parents: 1282
diff changeset
215
1628
de71a4bde097 Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1622
diff changeset
216 c.repository_followers = self.scm_model.get_followers(c.repo_name)
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1366
diff changeset
217 c.repository_forks = self.scm_model.get_forks(c.repo_name)