# HG changeset patch # User Marcin Kuzminski # Date 2010-11-25 00:57:37 # Node ID 01be209b9828b4f8be1f0be4a361a084e7e21029 # Parent 99ece4c484e195d8a74234fcc864df3ab1507bf2 project refactoring, cleaned up lib.utils from rarly used functions, and place them in proper controllers moves is_git is_hg functions to the middleware classes diff --git a/rhodecode/controllers/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -33,7 +33,7 @@ from rhodecode.lib.auth import LoginRequ from rhodecode.lib.base import BaseController, render from rhodecode.lib.celerylib import tasks, run_task from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \ - set_rhodecode_config, get_hg_settings, get_hg_ui_settings + set_rhodecode_config from rhodecode.model.db import RhodeCodeUi, Repository from rhodecode.model.forms import UserForm, ApplicationSettingsForm, \ ApplicationUiSettingsForm @@ -68,8 +68,8 @@ class SettingsController(BaseController) """GET /admin/settings: All items in the collection""" # url('admin_settings') - defaults = get_hg_settings() - defaults.update(get_hg_ui_settings()) + defaults = SettingsModel().get_app_settings() + defaults.update(self.get_hg_ui_settings()) return htmlfill.render( render('admin/settings/settings.html'), defaults=defaults, @@ -109,7 +109,7 @@ class SettingsController(BaseController) h.flash(_('Repositories successfully rescanned'), category='success') if setting_id == 'whoosh': - repo_location = get_hg_ui_settings()['paths_root_path'] + repo_location = self.get_hg_ui_settings()['paths_root_path'] full_index = request.POST.get('full_index', False) task = run_task(tasks.whoosh_index, repo_location, full_index) @@ -312,3 +312,24 @@ class SettingsController(BaseController) return render('admin/repos/repo_add_create_repository.html') + def get_hg_ui_settings(self): + ret = self.sa.query(RhodeCodeUi).all() + + if not ret: + raise Exception('Could not get application ui settings !') + settings = {} + for each in ret: + k = each.ui_key + v = each.ui_value + if k == '/': + k = 'root_path' + + if k.find('.') != -1: + k = k.replace('.', '_') + + if each.ui_section == 'hooks': + v = each.ui_active + + settings[each.ui_section + '_' + k] = v + + return settings diff --git a/rhodecode/lib/middleware/simplegit.py b/rhodecode/lib/middleware/simplegit.py --- a/rhodecode/lib/middleware/simplegit.py +++ b/rhodecode/lib/middleware/simplegit.py @@ -63,7 +63,7 @@ from dulwich.web import HTTPGitApplicati from paste.auth.basic import AuthBasicAuthenticator from paste.httpheaders import REMOTE_USER, AUTH_TYPE from rhodecode.lib.auth import authfunc, HasPermissionAnyMiddleware -from rhodecode.lib.utils import is_git, invalidate_cache, check_repo_fast +from rhodecode.lib.utils import invalidate_cache, check_repo_fast from rhodecode.model.user import UserModel from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError import logging @@ -72,6 +72,18 @@ import traceback log = logging.getLogger(__name__) +def is_git(environ): + """ + Returns True if request's target is git server. ``HTTP_USER_AGENT`` would + then have git client version given. + + :param environ: + """ + http_user_agent = environ.get('HTTP_USER_AGENT') + if http_user_agent and http_user_agent.startswith('git'): + return True + return False + class SimpleGit(object): def __init__(self, application, config): diff --git a/rhodecode/lib/middleware/simplehg.py b/rhodecode/lib/middleware/simplehg.py --- a/rhodecode/lib/middleware/simplehg.py +++ b/rhodecode/lib/middleware/simplehg.py @@ -24,14 +24,13 @@ Created on 2010-04-28 SimpleHG middleware for handling mercurial protocol request (push/clone etc.) It's implemented with basic auth function """ -from itertools import chain from mercurial.error import RepoError from mercurial.hgweb import hgweb from mercurial.hgweb.request import wsgiapplication from paste.auth.basic import AuthBasicAuthenticator from paste.httpheaders import REMOTE_USER, AUTH_TYPE from rhodecode.lib.auth import authfunc, HasPermissionAnyMiddleware -from rhodecode.lib.utils import is_mercurial, make_ui, invalidate_cache, \ +from rhodecode.lib.utils import make_ui, invalidate_cache, \ check_repo_fast, ui_sections from rhodecode.model.user import UserModel from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError @@ -41,6 +40,16 @@ import traceback log = logging.getLogger(__name__) +def is_mercurial(environ): + """ + Returns True if request's target is mercurial server - header + ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. + """ + http_accept = environ.get('HTTP_ACCEPT') + if http_accept and http_accept.startswith('application/mercurial'): + return True + return False + class SimpleHg(object): def __init__(self, application, config): @@ -143,7 +152,7 @@ class SimpleHg(object): #invalidate cache on push if self.action == 'push': self.__invalidate_cache(repo_name) - + return app(environ, start_response) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -27,10 +27,10 @@ from mercurial import ui, config, hg from mercurial.error import RepoError from rhodecode.model import meta from rhodecode.model.caching_query import FromCache -from rhodecode.model.db import Repository, User, RhodeCodeUi, RhodeCodeSettings, \ - UserLog +from rhodecode.model.db import Repository, User, RhodeCodeUi, UserLog from rhodecode.model.repo import RepoModel from rhodecode.model.user import UserModel + from vcs.backends.base import BaseChangeset from paste.script import command import ConfigParser @@ -46,28 +46,6 @@ log = logging.getLogger(__name__) def get_repo_slug(request): return request.environ['pylons.routes_dict'].get('repo_name') -def is_mercurial(environ): - """ - Returns True if request's target is mercurial server - header - ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. - """ - http_accept = environ.get('HTTP_ACCEPT') - if http_accept and http_accept.startswith('application/mercurial'): - return True - return False - -def is_git(environ): - """ - Returns True if request's target is git server. ``HTTP_USER_AGENT`` would - then have git client version given. - - :param environ: - """ - http_user_agent = environ.get('HTTP_USER_AGENT') - if http_user_agent and http_user_agent.startswith('git'): - return True - return False - def action_logger(user, action, repo, ipaddr='', sa=None): """ Action logger for various actions made by users @@ -110,17 +88,16 @@ def action_logger(user, action, repo, ip user_log = UserLog() user_log.user_id = user_obj.user_id user_log.action = action - + user_log.repository_id = repo_obj.repo_id user_log.repository_name = repo_name - + user_log.action_date = datetime.datetime.now() user_log.user_ip = ipaddr sa.add(user_log) sa.commit() - log.info('Adding user %s, action %s on %s', - user_obj.username, action, repo) + log.info('Adding user %s, action %s on %s', user_obj, action, repo) except: log.error(traceback.format_exc()) sa.rollback() @@ -150,10 +127,6 @@ def get_repos(path, recursive=False, ini except VCSError: pass -if __name__ == '__main__': - get_repos('', '/home/marcink/workspace-python') - - def check_repo_fast(repo_name, base_path): if os.path.isdir(os.path.join(base_path, repo_name)):return False return True @@ -185,66 +158,6 @@ def ask_ok(prompt, retries=4, complaint= if retries < 0: raise IOError print complaint -def get_hg_ui_cached(): - try: - sa = meta.Session - ret = sa.query(RhodeCodeUi)\ - .options(FromCache("sql_cache_short", "get_hg_ui_settings"))\ - .all() - except: - pass - finally: - meta.Session.remove() - return ret - - -def get_hg_settings(): - try: - sa = meta.Session() - ret = sa.query(RhodeCodeSettings)\ - .options(FromCache("sql_cache_short", "get_hg_settings"))\ - .all() - except: - pass - finally: - meta.Session.remove() - - if not ret: - raise Exception('Could not get application settings !') - settings = {} - for each in ret: - settings['rhodecode_' + each.app_settings_name] = each.app_settings_value - - return settings - -def get_hg_ui_settings(): - try: - sa = meta.Session() - ret = sa.query(RhodeCodeUi).all() - except: - pass - finally: - meta.Session.remove() - - if not ret: - raise Exception('Could not get application ui settings !') - settings = {} - for each in ret: - k = each.ui_key - v = each.ui_value - if k == '/': - k = 'root_path' - - if k.find('.') != -1: - k = k.replace('.', '_') - - if each.ui_section == 'hooks': - v = each.ui_active - - settings[each.ui_section + '_' + k] = v - - return settings - #propagated from mercurial documentation ui_sections = ['alias', 'auth', 'decode/encode', 'defaults', @@ -288,7 +201,12 @@ def make_ui(read_from='file', path=None, elif read_from == 'db': - hg_ui = get_hg_ui_cached() + sa = meta.Session() + ret = sa.query(RhodeCodeUi)\ + .options(FromCache("sql_cache_short", + "get_hg_ui_settings")).all() + meta.Session.remove() + hg_ui = ret for ui_ in hg_ui: if ui_.ui_active: log.debug('settings ui from db[%s]%s:%s', ui_.ui_section, ui_.ui_key, ui_.ui_value) @@ -297,7 +215,12 @@ def make_ui(read_from='file', path=None, def set_rhodecode_config(config): - hgsettings = get_hg_settings() + """ + Updates pylons config with new settings from database + :param config: + """ + from rhodecode.model.settings import SettingsModel + hgsettings = SettingsModel().get_app_settings() for k, v in hgsettings.items(): config[k] = v diff --git a/rhodecode/model/settings.py b/rhodecode/model/settings.py --- a/rhodecode/model/settings.py +++ b/rhodecode/model/settings.py @@ -28,7 +28,6 @@ from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import RhodeCodeSettings from sqlalchemy.orm import joinedload -from sqlalchemy.orm.session import make_transient import logging log = logging.getLogger(__name__) @@ -46,6 +45,18 @@ class SettingsModel(BaseModel): "get_setting_%s" % settings_key)) return r + def get_app_settings(self): + ret = self.sa.query(RhodeCodeSettings)\ + .options(FromCache("sql_cache_short", + "get_hg_settings")).all() + + if not ret: + raise Exception('Could not get application settings !') + settings = {} + for each in ret: + settings['rhodecode_' + each.app_settings_name] = each.app_settings_value + + return settings def get_ldap_settings(self): """