changeset 6516:eea19c23b741

cleanup: refer less to User.DEFAULT_USER Down the road we might want to identify the default user in another way than by username.
author Søren Løvborg <sorenl@unity3d.com>
date Thu, 23 Feb 2017 18:00:56 +0100
parents 884d2c246570
children a7271dbefd96
files kallithea/controllers/admin/my_account.py kallithea/controllers/admin/users.py kallithea/controllers/api/api.py kallithea/lib/auth_modules/auth_internal.py kallithea/lib/db_manage.py kallithea/lib/middleware/simplegit.py kallithea/lib/middleware/simplehg.py kallithea/lib/utils2.py kallithea/model/db.py kallithea/model/permission.py kallithea/model/pull_request.py kallithea/model/repo_group.py kallithea/model/user.py kallithea/tests/api/api_base.py kallithea/tests/other/manual_test_vcs_operations.py
diffstat 15 files changed, 23 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/my_account.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/controllers/admin/my_account.py	Thu Feb 23 18:00:56 2017 +0100
@@ -66,7 +66,7 @@
 
     def __load_data(self):
         c.user = User.get(request.authuser.user_id)
-        if c.user.username == User.DEFAULT_USER:
+        if c.user.is_default_user:
             h.flash(_("You can't edit this user since it's"
                       " crucial for entire application"), category='warning')
             raise HTTPFound(location=url('users'))
--- a/kallithea/controllers/admin/users.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/controllers/admin/users.py	Thu Feb 23 18:00:56 2017 +0100
@@ -68,7 +68,7 @@
 
     def index(self, format='html'):
         c.users_list = User.query().order_by(User.username) \
-                        .filter(User.username != User.DEFAULT_USER) \
+                        .filter_by(is_default_user=False) \
                         .order_by(func.lower(User.username)) \
                         .all()
 
--- a/kallithea/controllers/api/api.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/controllers/api/api.py	Thu Feb 23 18:00:56 2017 +0100
@@ -588,7 +588,7 @@
             user.get_api_data()
             for user in User.query()
                 .order_by(User.username)
-                .filter(User.username != User.DEFAULT_USER)
+                .filter_by(is_default_user=False)
         ]
 
     @HasPermissionAnyDecorator('hg.admin')
--- a/kallithea/lib/auth_modules/auth_internal.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/lib/auth_modules/auth_internal.py	Thu Feb 23 18:00:56 2017 +0100
@@ -87,7 +87,7 @@
         if userobj.active:
             from kallithea.lib import auth
             password_match = auth.check_password(password, userobj.password)
-            if userobj.username == User.DEFAULT_USER and userobj.active:
+            if userobj.is_default_user and userobj.active:
                 log.info('user %s authenticated correctly as anonymous user',
                          username)
                 return user_data
--- a/kallithea/lib/db_manage.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/lib/db_manage.py	Thu Feb 23 18:00:56 2017 +0100
@@ -140,9 +140,7 @@
         Fixes a old default user with some 'nicer' default values,
         used mostly for anonymous access
         """
-        def_user = self.sa.query(User) \
-                .filter(User.username == User.DEFAULT_USER) \
-                .one()
+        def_user = self.sa.query(User).filter_by(is_default_user=True).one()
 
         def_user.name = 'Anonymous'
         def_user.lastname = 'User'
--- a/kallithea/lib/middleware/simplegit.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/lib/middleware/simplegit.py	Thu Feb 23 18:00:56 2017 +0100
@@ -122,7 +122,7 @@
         log.debug('Repository path is %s', repo_path)
 
         # CHECK LOCKING only if it's not ANONYMOUS USER
-        if user.username != User.DEFAULT_USER:
+        if not user.is_default_user:
             log.debug('Checking locking on repository')
             (make_lock,
              locked,
--- a/kallithea/lib/middleware/simplehg.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/lib/middleware/simplehg.py	Thu Feb 23 18:00:56 2017 +0100
@@ -127,7 +127,7 @@
         log.debug('Repository path is %s', repo_path)
 
         # CHECK LOCKING only if it's not ANONYMOUS USER
-        if user.username != User.DEFAULT_USER:
+        if not user.is_default_user:
             log.debug('Checking locking on repository')
             (make_lock,
              locked,
--- a/kallithea/lib/utils2.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/lib/utils2.py	Thu Feb 23 18:00:56 2017 +0100
@@ -546,7 +546,7 @@
     result = set()
     for name in extract_mentioned_usernames(text):
         user = User.get_by_username(name, case_insensitive=True)
-        if user is not None and user.username != User.DEFAULT_USER:
+        if user is not None and not user.is_default_user:
             result.add(user)
     return result
 
--- a/kallithea/model/db.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/model/db.py	Thu Feb 23 18:00:56 2017 +0100
@@ -530,6 +530,10 @@
     def is_admin(self):
         return self.admin
 
+    @hybrid_property
+    def is_default_user(self):
+        return self.username == User.DEFAULT_USER
+
     @property
     def AuthUser(self):
         """
@@ -570,9 +574,8 @@
         the default user.
         '''
         user = super(User, cls).get_or_404(id_)
-        if allow_default == False:
-            if user.username == User.DEFAULT_USER:
-                raise DefaultUserException
+        if not allow_default and user.is_default_user:
+            raise DefaultUserException()
         return user
 
     @classmethod
--- a/kallithea/model/permission.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/model/permission.py	Thu Feb 23 18:00:56 2017 +0100
@@ -99,7 +99,7 @@
 
         try:
             # stage 1 set anonymous access
-            if perm_user.username == User.DEFAULT_USER:
+            if perm_user.is_default_user:
                 perm_user.active = str2bool(form_result['anonymous'])
                 self.sa.add(perm_user)
 
--- a/kallithea/model/pull_request.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/model/pull_request.py	Thu Feb 23 18:00:56 2017 +0100
@@ -54,7 +54,7 @@
         """
         for user_spec in seq:
             user = User.guess_instance(user_spec)
-            if user is None or user.username == User.DEFAULT_USER:
+            if user is None or user.is_default_user:
                 raise UserInvalidException(user_spec)
             yield user
 
--- a/kallithea/model/repo_group.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/model/repo_group.py	Thu Feb 23 18:00:56 2017 +0100
@@ -202,7 +202,7 @@
 
                 # private repos will not allow to change the default permissions
                 # using recursive mode
-                if obj.private and user.username == User.DEFAULT_USER:
+                if obj.private and user.is_default_user:
                     return
 
                 # we set group permission but we have to switch to repo
--- a/kallithea/model/user.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/model/user.py	Thu Feb 23 18:00:56 2017 +0100
@@ -210,7 +210,7 @@
         from kallithea.lib.auth import get_crypt_password
         skip_attrs = skip_attrs or []
         user = self.get(user_id, cache=False)
-        if user.username == User.DEFAULT_USER:
+        if user.is_default_user:
             raise DefaultUserException(
                             _("You can't edit this user since it's "
                               "crucial for entire application"))
@@ -232,7 +232,7 @@
         from kallithea.lib.auth import get_crypt_password
 
         user = User.guess_instance(user)
-        if user.username == User.DEFAULT_USER:
+        if user.is_default_user:
             raise DefaultUserException(
                 _("You can't edit this user since it's"
                   " crucial for entire application")
@@ -251,7 +251,7 @@
             cur_user = getattr(get_current_authuser(), 'username', None)
         user = User.guess_instance(user)
 
-        if user.username == User.DEFAULT_USER:
+        if user.is_default_user:
             raise DefaultUserException(
                 _("You can't remove this user since it is"
                   " crucial for the entire application"))
--- a/kallithea/tests/api/api_base.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/tests/api/api_base.py	Thu Feb 23 18:00:56 2017 +0100
@@ -216,7 +216,7 @@
         id_, params = _build_data(self.apikey, 'get_users', )
         response = api_call(self, params)
         ret_all = []
-        _users = User.query().filter(User.username != User.DEFAULT_USER) \
+        _users = User.query().filter_by(is_default_user=False) \
             .order_by(User.username).all()
         for usr in _users:
             ret = usr.get_api_data()
--- a/kallithea/tests/other/manual_test_vcs_operations.py	Mon Feb 20 19:21:34 2017 +0100
+++ b/kallithea/tests/other/manual_test_vcs_operations.py	Thu Feb 23 18:00:56 2017 +0100
@@ -158,11 +158,11 @@
 
 
 def set_anonymous_access(enable=True):
-    user = User.get_by_username(User.DEFAULT_USER)
+    user = User.get_default_user()
     user.active = enable
     Session().commit()
     print '\tanonymous access is now:', enable
-    if enable != User.get_by_username(User.DEFAULT_USER).active:
+    if enable != User.get_default_user().active:
         raise Exception('Cannot set anonymous access')