changeset 6582:1ae319cb41b1

middleware: convert check_locking_state to be a separate function Prepare for use with SSH.
author Anton Schur <tonich.sh@gmail.com>
date Thu, 06 Apr 2017 16:55:55 +0300
parents 2d0de5aa95d1
children 8a60eb2b7603
files kallithea/lib/base.py kallithea/lib/middleware/simplegit.py kallithea/lib/middleware/simplehg.py
diffstat 3 files changed, 40 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/base.py	Fri Apr 07 04:22:42 2017 +0200
+++ b/kallithea/lib/base.py	Thu Apr 06 16:55:55 2017 +0300
@@ -148,6 +148,42 @@
     return auth_user
 
 
+def check_locking_state(action, repo_name, user):
+    """
+    Checks locking on this repository, if locking is enabled, and if lock
+    is present. Returns a tuple of make_lock, locked, locked_by. make_lock
+    can have 3 states: None (do nothing), True (make lock), and False
+    (release lock). This value is later propagated to hooks, telling them
+    what to do.
+    """
+    locked = False  # defines that locked error should be thrown to user
+    make_lock = None
+    repo = Repository.get_by_repo_name(repo_name)
+    locked_by = repo.locked
+    if repo and repo.enable_locking:
+        if action == 'push':
+            # Check if repo already is locked !, if it is compare users
+            user_id, _date = locked_by
+            if user.user_id == user_id:
+                log.debug('Got push from user %s, now unlocking', user)
+                # Unlock if we have push from the user who locked
+                make_lock = False
+            else:
+                # Another used tried to push - deny access with something like 423 Locked!
+                locked = True
+        if action == 'pull':
+            if repo.locked[0] and repo.locked[1]:
+                locked = True
+            else:
+                log.debug('Setting lock on repo %s by %s', repo, user)
+                make_lock = True
+    else:
+        log.debug('Repository %s does not have locking enabled', repo)
+    log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
+              make_lock, locked, locked_by)
+    return make_lock, locked, locked_by
+
+
 class BasicAuth(paste.auth.basic.AuthBasicAuthenticator):
 
     def __init__(self, realm, authfunc, auth_http_code=None):
@@ -324,42 +360,6 @@
     def _get_ip_addr(self, environ):
         return _get_ip_addr(environ)
 
-    def _check_locking_state(self, action, repo_name, user):
-        """
-        Checks locking on this repository, if locking is enabled, and if lock
-        is present. Returns a tuple of make_lock, locked, locked_by. make_lock
-        can have 3 states: None (do nothing), True (make lock), and False
-        (release lock). This value is later propagated to hooks, telling them
-        what to do.
-        """
-        locked = False  # defines that locked error should be thrown to user
-        make_lock = None
-        repo = Repository.get_by_repo_name(repo_name)
-        locked_by = repo.locked
-        if repo and repo.enable_locking:
-            if action == 'push':
-                # Check if repo already is locked !, if it is compare users
-                user_id, _date = locked_by
-                if user.user_id == user_id:
-                    log.debug('Got push from user %s, now unlocking', user)
-                    # Unlock if we have push from the user who locked
-                    make_lock = False
-                else:
-                    # Another used tried to push - deny access with something like 423 Locked!
-                    locked = True
-            if action == 'pull':
-                if repo.locked[0] and repo.locked[1]:
-                    locked = True
-                else:
-                    log.debug('Setting lock on repo %s by %s', repo, user)
-                    make_lock = True
-
-        else:
-            log.debug('Repository %s does not have locking enabled', repo)
-        log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
-                  make_lock, locked, locked_by)
-        return make_lock, locked, locked_by
-
     def __call__(self, environ, start_response):
         start = time.time()
         try:
--- a/kallithea/lib/middleware/simplegit.py	Fri Apr 07 04:22:42 2017 +0200
+++ b/kallithea/lib/middleware/simplegit.py	Thu Apr 06 16:55:55 2017 +0300
@@ -40,7 +40,7 @@
 
 from kallithea.lib.utils2 import safe_str, safe_unicode, fix_PATH, get_server_url, \
     _set_extras
-from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback
+from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback, check_locking_state
 from kallithea.lib.utils import make_ui, is_valid_repo
 from kallithea.lib.exceptions import HTTPLockedRC
 from kallithea.lib.hooks import pre_pull
@@ -124,7 +124,7 @@
         # CHECK LOCKING only if it's not ANONYMOUS USER
         if not user.is_default_user:
             log.debug('Checking locking on repository')
-            make_lock, locked, locked_by = self._check_locking_state(action, repo_name, user)
+            make_lock, locked, locked_by = check_locking_state(action, repo_name, user)
             # store the make_lock for later evaluation in hooks
             extras.update({'make_lock': make_lock,
                            'locked_by': locked_by})
--- a/kallithea/lib/middleware/simplehg.py	Fri Apr 07 04:22:42 2017 +0200
+++ b/kallithea/lib/middleware/simplehg.py	Thu Apr 06 16:55:55 2017 +0300
@@ -37,7 +37,7 @@
 
 from kallithea.lib.utils2 import safe_str, safe_unicode, fix_PATH, get_server_url, \
     _set_extras
-from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback
+from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback, check_locking_state
 from kallithea.lib.utils import make_ui, is_valid_repo, ui_sections
 from kallithea.lib.vcs.utils.hgcompat import RepoError, hgweb_mod
 from kallithea.lib.exceptions import HTTPLockedRC
@@ -133,7 +133,7 @@
         # CHECK LOCKING only if it's not ANONYMOUS USER
         elif not user.is_default_user:
             log.debug('Checking locking on repository')
-            make_lock, locked, locked_by = self._check_locking_state(action, repo_name, user)
+            make_lock, locked, locked_by = check_locking_state(action, repo_name, user)
             # store the make_lock for later evaluation in hooks
             extras.update({'make_lock': make_lock,
                            'locked_by': locked_by})