##// END OF EJS Templates
- refactoring to overcome poor usage of global pylons config...
marcink -
r1723:64e91067 beta
parent child Browse files
Show More
@@ -54,3 +54,10 b' BACKENDS = {'
54 'hg': 'Mercurial repository',
54 'hg': 'Mercurial repository',
55 #'git': 'Git repository',
55 #'git': 'Git repository',
56 }
56 }
57
58 CELERY_ON = False
59
60 # link to config for pylons
61 CONFIG = None
62
63
@@ -7,13 +7,14 b' from mako.lookup import TemplateLookup'
7 from pylons.configuration import PylonsConfig
7 from pylons.configuration import PylonsConfig
8 from pylons.error import handle_mako_error
8 from pylons.error import handle_mako_error
9
9
10 import rhodecode
10 import rhodecode.lib.app_globals as app_globals
11 import rhodecode.lib.app_globals as app_globals
11 import rhodecode.lib.helpers
12 import rhodecode.lib.helpers
12
13
13 from rhodecode.config.routing import make_map
14 from rhodecode.config.routing import make_map
14 from rhodecode.lib import celerypylons
15 # don't remove this import it does magic for celery
16 from rhodecode.lib import celerypylons, str2bool
15 from rhodecode.lib import engine_from_config
17 from rhodecode.lib import engine_from_config
16 from rhodecode.lib.timerproxy import TimerProxy
17 from rhodecode.lib.auth import set_available_permissions
18 from rhodecode.lib.auth import set_available_permissions
18 from rhodecode.lib.utils import repo2db_mapper, make_ui, set_rhodecode_config
19 from rhodecode.lib.utils import repo2db_mapper, make_ui, set_rhodecode_config
19 from rhodecode.model import init_model
20 from rhodecode.model import init_model
@@ -38,6 +39,10 b' def load_environment(global_conf, app_co'
38 # Initialize config with the basic options
39 # Initialize config with the basic options
39 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
40 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
40
41
42 # store some globals into our main isntance
43 rhodecode.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
44 rhodecode.CONFIG = config
45
41 config['routes.map'] = make_map(config)
46 config['routes.map'] = make_map(config)
42 config['pylons.app_globals'] = app_globals.Globals(config)
47 config['pylons.app_globals'] = app_globals.Globals(config)
43 config['pylons.h'] = rhodecode.lib.helpers
48 config['pylons.h'] = rhodecode.lib.helpers
@@ -61,7 +66,7 b' def load_environment(global_conf, app_co'
61 from rhodecode.lib.utils import create_test_env, create_test_index
66 from rhodecode.lib.utils import create_test_env, create_test_index
62 from rhodecode.tests import TESTS_TMP_PATH
67 from rhodecode.tests import TESTS_TMP_PATH
63 create_test_env(TESTS_TMP_PATH, config)
68 create_test_env(TESTS_TMP_PATH, config)
64 create_test_index(TESTS_TMP_PATH, config, True)
69 #create_test_index(TESTS_TMP_PATH, config, True)
65
70
66 #MULTIPLE DB configs
71 #MULTIPLE DB configs
67 # Setup the SQLAlchemy database engine
72 # Setup the SQLAlchemy database engine
@@ -2,6 +2,7 b' import logging'
2 import traceback
2 import traceback
3
3
4 from pylons import tmpl_context as c, url
4 from pylons import tmpl_context as c, url
5 from pylons.controllers.util import redirect
5
6
6 from rhodecode.lib.base import BaseController, render
7 from rhodecode.lib.base import BaseController, render
7 from rhodecode.model.db import Notification
8 from rhodecode.model.db import Notification
@@ -10,7 +11,7 b' from rhodecode.model.notification import'
10 from rhodecode.lib.auth import LoginRequired
11 from rhodecode.lib.auth import LoginRequired
11 from rhodecode.lib import helpers as h
12 from rhodecode.lib import helpers as h
12 from rhodecode.model.meta import Session
13 from rhodecode.model.meta import Session
13 from pylons.controllers.util import redirect
14
14
15
15 log = logging.getLogger(__name__)
16 log = logging.getLogger(__name__)
16
17
@@ -32,10 +32,9 b' from os.path import dirname as dn, join '
32
32
33 from hashlib import md5
33 from hashlib import md5
34 from decorator import decorator
34 from decorator import decorator
35 from pylons import config
36
35
37 from vcs.utils.lazy import LazyProperty
36 from vcs.utils.lazy import LazyProperty
38
37 from rhodecode import CELERY_ON
39 from rhodecode.lib import str2bool, safe_str
38 from rhodecode.lib import str2bool, safe_str
40 from rhodecode.lib.pidlock import DaemonLock, LockHeld
39 from rhodecode.lib.pidlock import DaemonLock, LockHeld
41
40
@@ -44,10 +43,7 b' from celery.messaging import establish_c'
44
43
45 log = logging.getLogger(__name__)
44 log = logging.getLogger(__name__)
46
45
47 try:
46
48 CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
49 except KeyError:
50 CELERY_ON = False
51
47
52
48
53 class ResultWrapper(object):
49 class ResultWrapper(object):
@@ -39,6 +39,7 b' from pylons.i18n.translation import _'
39
39
40 from vcs import get_backend
40 from vcs import get_backend
41
41
42 from rhodecode import CELERY_ON
42 from rhodecode.lib import LANGUAGES_EXTENSIONS_MAP, safe_str
43 from rhodecode.lib import LANGUAGES_EXTENSIONS_MAP, safe_str
43 from rhodecode.lib.celerylib import run_task, locked_task, str2bool, \
44 from rhodecode.lib.celerylib import run_task, locked_task, str2bool, \
44 __get_lockkey, LockHeld, DaemonLock
45 __get_lockkey, LockHeld, DaemonLock
@@ -59,9 +60,6 b' add_cache(config)'
59 'reset_user_password', 'send_email']
60 'reset_user_password', 'send_email']
60
61
61
62
62 CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
63
64
65 def get_session():
63 def get_session():
66 if CELERY_ON:
64 if CELERY_ON:
67 engine = engine_from_config(config, 'sqlalchemy.db1.')
65 engine = engine_from_config(config, 'sqlalchemy.db1.')
@@ -414,7 +414,7 b' def repo2db_mapper(initial_repo_list, re'
414 'group_id': getattr(group, 'group_id', None)
414 'group_id': getattr(group, 'group_id', None)
415 }
415 }
416 rm.create(form_data, user, just_db=True)
416 rm.create(form_data, user, just_db=True)
417
417 sa.commit()
418 removed = []
418 removed = []
419 if remove_obsolete:
419 if remove_obsolete:
420 #remove from database those repositories that are not in the filesystem
420 #remove from database those repositories that are not in the filesystem
@@ -27,10 +27,8 b' import os'
27 import logging
27 import logging
28 import datetime
28 import datetime
29 import traceback
29 import traceback
30 from datetime import date
31
30
32 from sqlalchemy import *
31 from sqlalchemy import *
33 from sqlalchemy.exc import DatabaseError
34 from sqlalchemy.ext.hybrid import hybrid_property
32 from sqlalchemy.ext.hybrid import hybrid_property
35 from sqlalchemy.orm import relationship, joinedload, class_mapper, validates
33 from sqlalchemy.orm import relationship, joinedload, class_mapper, validates
36 from beaker.cache import cache_region, region_invalidate
34 from beaker.cache import cache_region, region_invalidate
@@ -40,8 +38,7 b' from vcs.utils.helpers import get_scm'
40 from vcs.exceptions import VCSError
38 from vcs.exceptions import VCSError
41 from vcs.utils.lazy import LazyProperty
39 from vcs.utils.lazy import LazyProperty
42
40
43 from rhodecode.lib import str2bool, safe_str, get_changeset_safe, \
41 from rhodecode.lib import str2bool, safe_str, get_changeset_safe, safe_unicode
44 generate_api_key, safe_unicode
45 from rhodecode.lib.exceptions import UsersGroupsAssignedException
42 from rhodecode.lib.exceptions import UsersGroupsAssignedException
46 from rhodecode.lib.compat import json
43 from rhodecode.lib.compat import json
47 from rhodecode.lib.caching_query import FromCache
44 from rhodecode.lib.caching_query import FromCache
@@ -354,7 +351,7 b' class UserLog(Base, BaseModel):'
354
351
355 @property
352 @property
356 def action_as_day(self):
353 def action_as_day(self):
357 return date(*self.action_date.timetuple()[:3])
354 return datetime.date(*self.action_date.timetuple()[:3])
358
355
359 user = relationship('User')
356 user = relationship('User')
360 repository = relationship('Repository')
357 repository = relationship('Repository')
@@ -29,9 +29,9 b' import logging'
29 import traceback
29 import traceback
30 import datetime
30 import datetime
31
31
32 from pylons import config
33 from pylons.i18n.translation import _
32 from pylons.i18n.translation import _
34
33
34 import rhodecode
35 from rhodecode.lib import helpers as h
35 from rhodecode.lib import helpers as h
36 from rhodecode.model import BaseModel
36 from rhodecode.model import BaseModel
37 from rhodecode.model.db import Notification, User, UserNotification
37 from rhodecode.model.db import Notification, User, UserNotification
@@ -41,7 +41,6 b' log = logging.getLogger(__name__)'
41
41
42 class NotificationModel(BaseModel):
42 class NotificationModel(BaseModel):
43
43
44
45 def __get_user(self, user):
44 def __get_user(self, user):
46 if isinstance(user, basestring):
45 if isinstance(user, basestring):
47 return User.get_by_username(username=user)
46 return User.get_by_username(username=user)
@@ -58,7 +57,6 b' class NotificationModel(BaseModel):'
58 raise Exception('notification must be int or Instance'
57 raise Exception('notification must be int or Instance'
59 ' of Notification got %s' % type(notification))
58 ' of Notification got %s' % type(notification))
60
59
61
62 def create(self, created_by, subject, body, recipients,
60 def create(self, created_by, subject, body, recipients,
63 type_=Notification.TYPE_MESSAGE):
61 type_=Notification.TYPE_MESSAGE):
64 """
62 """
@@ -90,7 +88,6 b' class NotificationModel(BaseModel):'
90 body=body, recipients=recipients_objs,
88 body=body, recipients=recipients_objs,
91 type_=type_)
89 type_=type_)
92
90
93
94 # send email with notification
91 # send email with notification
95 for rec in recipients_objs:
92 for rec in recipients_objs:
96 email_subject = NotificationModel().make_description(notif, False)
93 email_subject = NotificationModel().make_description(notif, False)
@@ -176,7 +173,8 b' class EmailNotificationModel(BaseModel):'
176 TYPE_DEFAULT = 'default'
173 TYPE_DEFAULT = 'default'
177
174
178 def __init__(self):
175 def __init__(self):
179 self._template_root = config['pylons.paths']['templates'][0]
176 self._template_root = rhodecode.CONFIG['pylons.paths']['templates'][0]
177 self._tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup
180
178
181 self.email_types = {
179 self.email_types = {
182 self.TYPE_CHANGESET_COMMENT:'email_templates/changeset_comment.html',
180 self.TYPE_CHANGESET_COMMENT:'email_templates/changeset_comment.html',
@@ -191,10 +189,9 b' class EmailNotificationModel(BaseModel):'
191
189
192 :param type_:
190 :param type_:
193 """
191 """
192
194 base = self.email_types.get(type_, self.TYPE_DEFAULT)
193 base = self.email_types.get(type_, self.TYPE_DEFAULT)
195
194 email_template = self._tmpl_lookup.get_template(base)
196 lookup = config['pylons.app_globals'].mako_lookup
197 email_template = lookup.get_template(base)
198 # translator inject
195 # translator inject
199 _kwargs = {'_':_}
196 _kwargs = {'_':_}
200 _kwargs.update(kwargs)
197 _kwargs.update(kwargs)
@@ -13,6 +13,7 b' import logging'
13 from os.path import join as jn
13 from os.path import join as jn
14
14
15 from unittest import TestCase
15 from unittest import TestCase
16 from tempfile import _RandomNameSequence
16
17
17 from paste.deploy import loadapp
18 from paste.deploy import loadapp
18 from paste.script.appinstall import SetupCommand
19 from paste.script.appinstall import SetupCommand
@@ -20,7 +21,7 b' from pylons import config, url'
20 from routes.util import URLGenerator
21 from routes.util import URLGenerator
21 from webtest import TestApp
22 from webtest import TestApp
22
23
23 from rhodecode.model import meta
24 from rhodecode.model.meta import Session
24 from rhodecode.model.db import User
25 from rhodecode.model.db import User
25
26
26 import pylons.test
27 import pylons.test
@@ -43,7 +44,7 b' log = logging.getLogger(__name__)'
43 environ = {}
44 environ = {}
44
45
45 #SOME GLOBALS FOR TESTS
46 #SOME GLOBALS FOR TESTS
46 from tempfile import _RandomNameSequence
47
47 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
48 TESTS_TMP_PATH = jn('/', 'tmp', 'rc_test_%s' % _RandomNameSequence().next())
48 TEST_USER_ADMIN_LOGIN = 'test_admin'
49 TEST_USER_ADMIN_LOGIN = 'test_admin'
49 TEST_USER_ADMIN_PASS = 'test12'
50 TEST_USER_ADMIN_PASS = 'test12'
@@ -64,7 +65,7 b' class TestController(TestCase):'
64
65
65 self.app = TestApp(wsgiapp)
66 self.app = TestApp(wsgiapp)
66 url._push_object(URLGenerator(config['routes.map'], environ))
67 url._push_object(URLGenerator(config['routes.map'], environ))
67 self.Session = meta.Session
68 self.Session = Session
68 self.index_location = config['app_conf']['index_dir']
69 self.index_location = config['app_conf']['index_dir']
69 TestCase.__init__(self, *args, **kwargs)
70 TestCase.__init__(self, *args, **kwargs)
70
71
@@ -79,14 +80,16 b' class TestController(TestCase):'
79 self.fail('could not login using %s %s' % (username, password))
80 self.fail('could not login using %s %s' % (username, password))
80
81
81 self.assertEqual(response.status, '302 Found')
82 self.assertEqual(response.status, '302 Found')
82 self.assertEqual(response.session['rhodecode_user'].get('username'),
83 ses = response.session['rhodecode_user']
83 username)
84 self.assertEqual(ses.get('username'), username)
84 return response.follow()
85 response = response.follow()
86 self.assertEqual(ses.get('is_authenticated'), True)
87
88 return response.session['rhodecode_user']
85
89
86 def _get_logged_user(self):
90 def _get_logged_user(self):
87 return User.get_by_username(self._logged_username)
91 return User.get_by_username(self._logged_username)
88
92
89
90 def checkSessionFlash(self, response, msg):
93 def checkSessionFlash(self, response, msg):
91 self.assertTrue('flash' in response.session)
94 self.assertTrue('flash' in response.session)
92 self.assertTrue(msg in response.session['flash'][0][1])
95 self.assertTrue(msg in response.session['flash'][0][1])
@@ -102,7 +102,7 b' class TestNotificationsController(TestCo'
102 name='u2', lastname='u2')
102 name='u2', lastname='u2')
103
103
104 notification = NotificationModel().create(created_by=cur_user,
104 notification = NotificationModel().create(created_by=cur_user,
105 subject='test',
105 subject=u'test',
106 body=u'hi there',
106 body=u'hi there',
107 recipients=[cur_user, u1, u2])
107 recipients=[cur_user, u1, u2])
108
108
@@ -4,15 +4,15 b' class TestBranchesController(TestControl'
4
4
5 def test_index(self):
5 def test_index(self):
6 self.log_user()
6 self.log_user()
7 response = self.app.get(url(controller='branches', action='index', repo_name=HG_REPO))
7 response = self.app.get(url(controller='branches',
8 action='index', repo_name=HG_REPO))
8
9
9 assert """<a href="/%s/changeset/27cd5cce30c96924232dffcd24178a07ffeb5dfc">default</a>""" % HG_REPO in response.body, 'wrong info about default branch'
10 self.assertTrue("""<a href="/%s/changeset/27cd5cce30c96924232dffcd24178a07ffeb5dfc">default</a>""" % HG_REPO in response.body)
10 assert """<a href="/%s/changeset/97e8b885c04894463c51898e14387d80c30ed1ee">git</a>""" % HG_REPO in response.body, 'wrong info about default git'
11 self.assertTrue("""<a href="/%s/changeset/97e8b885c04894463c51898e14387d80c30ed1ee">git</a>""" % HG_REPO in response.body)
11 assert """<a href="/%s/changeset/2e6a2bf9356ca56df08807f4ad86d480da72a8f4">web</a>""" % HG_REPO in response.body, 'wrong info about default web'
12 self.assertTrue("""<a href="/%s/changeset/2e6a2bf9356ca56df08807f4ad86d480da72a8f4">web</a>""" % HG_REPO in response.body)
12
13
13
14
14
15
15
16
16
17
17
18
18 # Test response...
@@ -108,7 +108,7 b' class TestChangeSetCommentrController(Te'
108 users = [x.user.username for x in UserNotification.query().all()]
108 users = [x.user.username for x in UserNotification.query().all()]
109
109
110 # test_regular get's notification by @mention
110 # test_regular get's notification by @mention
111 self.assertEqual(users, [u'test_admin', u'test_regular'])
111 self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
112
112
113 def test_delete(self):
113 def test_delete(self):
114 self.log_user()
114 self.log_user()
@@ -128,7 +128,7 b' class TestChangeSetCommentrController(Te'
128 self.app.delete(url(controller='changeset',
128 self.app.delete(url(controller='changeset',
129 action='delete_comment',
129 action='delete_comment',
130 repo_name=HG_REPO,
130 repo_name=HG_REPO,
131 comment_id = comment_id))
131 comment_id=comment_id))
132
132
133 comments = ChangesetComment.query().all()
133 comments = ChangesetComment.query().all()
134 self.assertEqual(len(comments), 0)
134 self.assertEqual(len(comments), 0)
@@ -20,10 +20,13 b' class TestForksController(TestController'
20 fork_name = HG_FORK
20 fork_name = HG_FORK
21 description = 'fork of vcs test'
21 description = 'fork of vcs test'
22 repo_name = HG_REPO
22 repo_name = HG_REPO
23 response = self.app.post(url(controller='settings',
23 org_repo = Repository.get_by_repo_name(repo_name)
24 response = self.app.post(url(controller='forks',
24 action='fork_create',
25 action='fork_create',
25 repo_name=repo_name),
26 repo_name=repo_name),
26 {'fork_name':fork_name,
27 {'repo_name':fork_name,
28 'repo_group':'',
29 'fork_parent_id':org_repo.repo_id,
27 'repo_type':'hg',
30 'repo_type':'hg',
28 'description':description,
31 'description':description,
29 'private':'False'})
32 'private':'False'})
@@ -39,3 +42,42 b' class TestForksController(TestController'
39 #remove this fork
42 #remove this fork
40 response = self.app.delete(url('repo', repo_name=fork_name))
43 response = self.app.delete(url('repo', repo_name=fork_name))
41
44
45
46
47
48 def test_z_fork_create(self):
49 self.log_user()
50 fork_name = HG_FORK
51 description = 'fork of vcs test'
52 repo_name = HG_REPO
53 org_repo = Repository.get_by_repo_name(repo_name)
54 response = self.app.post(url(controller='forks', action='fork_create',
55 repo_name=repo_name),
56 {'repo_name':fork_name,
57 'repo_group':'',
58 'fork_parent_id':org_repo.repo_id,
59 'repo_type':'hg',
60 'description':description,
61 'private':'False'})
62
63 #test if we have a message that fork is ok
64 self.assertTrue('forked %s repository as %s' \
65 % (repo_name, fork_name) in response.session['flash'][0])
66
67 #test if the fork was created in the database
68 fork_repo = self.Session().query(Repository)\
69 .filter(Repository.repo_name == fork_name).one()
70
71 self.assertEqual(fork_repo.repo_name, fork_name)
72 self.assertEqual(fork_repo.fork.repo_name, repo_name)
73
74
75 #test if fork is visible in the list ?
76 response = response.follow()
77
78
79 #check if fork is marked as fork
80 response = self.app.get(url(controller='summary', action='index',
81 repo_name=fork_name))
82
83 self.assertTrue('Fork of %s' % repo_name in response.body)
@@ -8,42 +8,3 b' class TestSettingsController(TestControl'
8 response = self.app.get(url(controller='settings', action='index',
8 response = self.app.get(url(controller='settings', action='index',
9 repo_name=HG_REPO))
9 repo_name=HG_REPO))
10 # Test response...
10 # Test response...
11
12 def test_fork(self):
13 self.log_user()
14 response = self.app.get(url(controller='settings', action='fork',
15 repo_name=HG_REPO))
16
17
18 def test_fork_create(self):
19 self.log_user()
20 fork_name = HG_FORK
21 description = 'fork of vcs test'
22 repo_name = HG_REPO
23 response = self.app.post(url(controller='settings', action='fork_create',
24 repo_name=repo_name),
25 {'fork_name':fork_name,
26 'repo_type':'hg',
27 'description':description,
28 'private':'False'})
29
30 #test if we have a message that fork is ok
31 assert 'forked %s repository as %s' \
32 % (repo_name, fork_name) in response.session['flash'][0], 'No flash message about fork'
33
34 #test if the fork was created in the database
35 fork_repo = self.Session().query(Repository).filter(Repository.repo_name == fork_name).one()
36
37 assert fork_repo.repo_name == fork_name, 'wrong name of repo name in new db fork repo'
38 assert fork_repo.fork.repo_name == repo_name, 'wrong fork parrent'
39
40
41 #test if fork is visible in the list ?
42 response = response.follow()
43
44
45 #check if fork is marked as fork
46 response = self.app.get(url(controller='summary', action='index',
47 repo_name=fork_name))
48
49 assert 'Fork of %s' % repo_name in response.body, 'no message about that this repo is a fork'
General Comments 0
You need to be logged in to leave comments. Login now