Mercurial > kallithea
annotate rhodecode/lib/base.py @ 1634:1d904d972c47 beta
User usermodel instead of db model to manage accounts
- initial refactoring of models to handle rhodecode+api
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 02 Nov 2011 16:19:51 +0200 |
parents | 25d8e4836bc2 |
children | 8cb7f5c4d494 |
rev | line source |
---|---|
0 | 1 """The base Controller API |
2 | |
3 Provides the BaseController class for subclassing. | |
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 |
1373
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
7 from pylons import config, tmpl_context as c, request, session, url |
0 | 8 from pylons.controllers import WSGIController |
1373
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
9 from pylons.controllers.util import redirect |
0 | 10 from pylons.templating import render_mako as render |
1304
5a96551ee9c0
gui-improvments
Marcin Kuzminski <marcin@python-works.com>
parents:
1282
diff
changeset
|
11 |
547
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
537
diff
changeset
|
12 from rhodecode import __version__ |
1629
2196aa27954b
implements #293 gravatar link should be disabled when use_gravatar = false
Marcin Kuzminski <marcin@python-works.com>
parents:
1628
diff
changeset
|
13 from rhodecode.lib import str2bool |
1617
cf128ced8c85
Improved container-based auth implementation and added support for a reverse-proxy setup (using the X-Forwarded-User header)
Liad Shani <liadff@gmail.com>
parents:
1614
diff
changeset
|
14 from rhodecode.lib.auth import AuthUser, get_container_username |
547
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
537
diff
changeset
|
15 from rhodecode.lib.utils import get_repo_slug |
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
537
diff
changeset
|
16 from rhodecode.model import meta |
691
7486da5f0628
Refactor codes for scm model
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
17 from rhodecode.model.scm import ScmModel |
710
e2f3c8e6939d
Disable git support due to large problems with dulwich.
Marcin Kuzminski <marcin@python-works.com>
parents:
691
diff
changeset
|
18 from rhodecode import BACKENDS |
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
|
19 from rhodecode.model.db import Repository |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
659
diff
changeset
|
20 |
1373
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
21 log = logging.getLogger(__name__) |
1307 | 22 |
0 | 23 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
|
24 |
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
|
25 def __before__(self): |
548
b75b77ef649d
renamed hg_app to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
26 c.rhodecode_version = __version__ |
890
042d38683d42
implemented #89 google analytics code
Marcin Kuzminski <marcin@python-works.com>
parents:
812
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 c.backends = BACKENDS.keys() |
890
042d38683d42
implemented #89 google analytics code
Marcin Kuzminski <marcin@python-works.com>
parents:
812
diff
changeset
|
32 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
|
33 |
784
30d3161c6683
Implemented fancier top menu for logged and anonymous users
Marcin Kuzminski <marcin@python-works.com>
parents:
747
diff
changeset
|
34 self.sa = meta.Session() |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
35 self.scm_model = ScmModel(self.sa) |
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
|
36 |
0 | 37 def __call__(self, environ, start_response): |
38 """Invoke the Controller""" | |
39 # WSGIController.__call__ dispatches to the Controller method | |
40 # the request is routed to. This routing information is | |
41 # available in environ['pylons.routes_dict'] | |
1601
6f06daeed294
Added Request time tracking
Marcin Kuzminski <marcin@python-works.com>
parents:
1530
diff
changeset
|
42 start = time.time() |
0 | 43 try: |
1628
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
44 # 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
|
45 api_key = request.GET.get('api_key') |
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
|
46 user_id = getattr(session.get('rhodecode_user'), 'user_id', None) |
1630
25d8e4836bc2
Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents:
1629
diff
changeset
|
47 username = get_container_username(environ, config) |
25d8e4836bc2
Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents:
1629
diff
changeset
|
48 |
1628
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
49 auth_user = AuthUser(user_id, api_key, username) |
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
50 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
|
51 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
|
52 self.rhodecode_user.user_id is not None: |
1613
6cab36e31f09
Added container-based authentication support
Liad Shani <liadff@gmail.com>
parents:
1373
diff
changeset
|
53 self.rhodecode_user.set_authenticated( |
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
|
54 getattr(session.get('rhodecode_user'), |
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
|
55 'is_authenticated', False)) |
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
|
56 session['rhodecode_user'] = self.rhodecode_user |
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
|
57 session.save() |
0 | 58 return WSGIController.__call__(self, environ, start_response) |
59 finally: | |
1601
6f06daeed294
Added Request time tracking
Marcin Kuzminski <marcin@python-works.com>
parents:
1530
diff
changeset
|
60 log.debug('Request time: %.3fs' % (time.time()-start)) |
51 | 61 meta.Session.remove() |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
62 |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
63 |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
64 class BaseRepoController(BaseController): |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
65 """ |
1628
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
66 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
|
67 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
|
68 |
1628
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
69 c.rhodecode_repo: instance of scm repository |
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
70 c.rhodecode_db_repo: instance of db |
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
71 c.repository_followers: number of followers |
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
72 c.repository_forks: number of forks |
1045
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
73 """ |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
74 |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
75 def __before__(self): |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
76 super(BaseRepoController, self).__before__() |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
77 if c.repo_name: |
3fc9183e05dd
another major codes rewrite:
Marcin Kuzminski <marcin@python-works.com>
parents:
1038
diff
changeset
|
78 |
1530
04027bdb876c
Refactoring of model get functions
Marcin Kuzminski <marcin@python-works.com>
parents:
1373
diff
changeset
|
79 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
|
80 c.rhodecode_repo = c.rhodecode_db_repo.scm_instance |
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
81 |
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
82 if c.rhodecode_repo is None: |
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
83 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
|
84 '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
|
85 |
1373
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
86 redirect(url('home')) |
1304
5a96551ee9c0
gui-improvments
Marcin Kuzminski <marcin@python-works.com>
parents:
1282
diff
changeset
|
87 |
1628
de71a4bde097
Some code cleanups and fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1622
diff
changeset
|
88 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
|
89 c.repository_forks = self.scm_model.get_forks(c.repo_name) |
66f03a87141c
Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents:
1366
diff
changeset
|
90 |