changeset 756:01be209b9828 beta

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
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 25 Nov 2010 01:57:37 +0100
parents 99ece4c484e1
children c52e88b57bf4
files rhodecode/controllers/admin/settings.py rhodecode/lib/middleware/simplegit.py rhodecode/lib/middleware/simplehg.py rhodecode/lib/utils.py rhodecode/model/settings.py
diffstat 5 files changed, 79 insertions(+), 103 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/admin/settings.py	Thu Nov 25 01:53:31 2010 +0100
+++ b/rhodecode/controllers/admin/settings.py	Thu Nov 25 01:57:37 2010 +0100
@@ -33,7 +33,7 @@
 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 @@
         """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 @@
             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 @@
 
         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
--- a/rhodecode/lib/middleware/simplegit.py	Thu Nov 25 01:53:31 2010 +0100
+++ b/rhodecode/lib/middleware/simplegit.py	Thu Nov 25 01:57:37 2010 +0100
@@ -63,7 +63,7 @@
 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 @@
 
 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):
--- a/rhodecode/lib/middleware/simplehg.py	Thu Nov 25 01:53:31 2010 +0100
+++ b/rhodecode/lib/middleware/simplehg.py	Thu Nov 25 01:57:37 2010 +0100
@@ -24,14 +24,13 @@
 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 @@
 
 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 @@
         #invalidate cache on push
         if self.action == 'push':
             self.__invalidate_cache(repo_name)
-        
+
         return app(environ, start_response)
 
 
--- a/rhodecode/lib/utils.py	Thu Nov 25 01:53:31 2010 +0100
+++ b/rhodecode/lib/utils.py	Thu Nov 25 01:57:37 2010 +0100
@@ -27,10 +27,10 @@
 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 @@
 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 @@
         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 @@
         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 @@
         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 @@
 
 
     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 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
--- a/rhodecode/model/settings.py	Thu Nov 25 01:53:31 2010 +0100
+++ b/rhodecode/model/settings.py	Thu Nov 25 01:57:37 2010 +0100
@@ -28,7 +28,6 @@
 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 @@
                                           "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):
         """