changeset 6520:10f16cf8289e

cleanup: remove SQLAlchemy session argument to action_logger There's always a global SQLAlchemy session associated with the current thread; using another session for a single function call does not make any sense (as sessions cannot be mixed), unless the code works carefully to ensure the two sessions (and all objects loaded from them) are kept completely separate. Suffice to say that Kallithea does no such thing, thus there's no need to pretend to support multiple concurrent sessions.
author Søren Løvborg <sorenl@unity3d.com>
date Mon, 27 Feb 2017 19:47:54 +0100
parents 95eb0af77407
children 62e9be5eb783
files kallithea/controllers/admin/repo_groups.py kallithea/controllers/admin/repos.py kallithea/controllers/admin/user_groups.py kallithea/controllers/admin/users.py kallithea/controllers/changeset.py kallithea/controllers/pullrequests.py kallithea/lib/celerylib/tasks.py kallithea/lib/utils.py
diffstat 8 files changed, 21 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/repo_groups.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/admin/repo_groups.py	Mon Feb 27 19:47:54 2017 +0100
@@ -167,7 +167,7 @@
                 copy_permissions=form_result['group_copy_permissions']
             )
             Session().commit()
-            #TODO: in futureaction_logger(, '', '', '', self.sa)
+            #TODO: in future action_logger(, '', '', '')
         except formencode.Invalid as errors:
             return htmlfill.render(
                 render('admin/repo_groups/repo_group_add.html'),
@@ -234,7 +234,7 @@
                     % form_result['group_name'], category='success')
             # we now have new name !
             group_name = new_gr.group_name
-            #TODO: in future action_logger(, '', '', '', self.sa)
+            #TODO: in future action_logger(, '', '', '')
         except formencode.Invalid as errors:
             c.active = 'settings'
             return htmlfill.render(
@@ -271,7 +271,7 @@
             Session().commit()
             h.flash(_('Removed repository group %s') % group_name,
                     category='success')
-            #TODO: in future action_logger(, '', '', '', self.sa)
+            #TODO: in future action_logger(, '', '', '')
         except Exception:
             log.error(traceback.format_exc())
             h.flash(_('Error occurred during deletion of repository group %s')
@@ -372,7 +372,7 @@
                                              recursive)
         #TODO: implement this
         #action_logger(request.authuser, 'admin_changed_repo_permissions',
-        #              repo_name, request.ip_addr, self.sa)
+        #              repo_name, request.ip_addr)
         Session().commit()
         h.flash(_('Repository group permissions updated'), category='success')
         raise HTTPFound(location=url('edit_repo_group_perms', group_name=group_name))
--- a/kallithea/controllers/admin/repos.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/admin/repos.py	Mon Feb 27 19:47:54 2017 +0100
@@ -241,7 +241,7 @@
                     category='success')
             changed_name = repo.repo_name
             action_logger(request.authuser, 'admin_updated_repo',
-                              changed_name, request.ip_addr, self.sa)
+                changed_name, request.ip_addr)
             Session().commit()
         except formencode.Invalid as errors:
             log.info(errors)
@@ -282,7 +282,7 @@
                     h.flash(_('Deleted %s forks') % _forks, category='success')
             repo_model.delete(repo, forks=handle_forks)
             action_logger(request.authuser, 'admin_deleted_repo',
-                  repo_name, request.ip_addr, self.sa)
+                repo_name, request.ip_addr)
             ScmModel().mark_for_invalidation(repo_name)
             h.flash(_('Deleted repository %s') % repo_name, category='success')
             Session().commit()
@@ -334,7 +334,7 @@
                                         form['perms_updates'])
         #TODO: implement this
         #action_logger(request.authuser, 'admin_changed_repo_permissions',
-        #              repo_name, request.ip_addr, self.sa)
+        #              repo_name, request.ip_addr)
         Session().commit()
         h.flash(_('Repository permissions updated'), category='success')
         raise HTTPFound(location=url('edit_repo_perms', repo_name=repo_name))
@@ -356,7 +356,7 @@
                 )
             #TODO: implement this
             #action_logger(request.authuser, 'admin_revoked_repo_permissions',
-            #              repo_name, request.ip_addr, self.sa)
+            #              repo_name, request.ip_addr)
             Session().commit()
         except Exception:
             log.error(traceback.format_exc())
--- a/kallithea/controllers/admin/user_groups.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/admin/user_groups.py	Mon Feb 27 19:47:54 2017 +0100
@@ -142,7 +142,7 @@
             gr = form_result['users_group_name']
             action_logger(request.authuser,
                           'admin_created_users_group:%s' % gr,
-                          None, request.ip_addr, self.sa)
+                          None, request.ip_addr)
             h.flash(h.literal(_('Created user group %s') % h.link_to(h.escape(gr), url('edit_users_group', id=ug.users_group_id))),
                 category='success')
             Session().commit()
@@ -183,7 +183,7 @@
             gr = form_result['users_group_name']
             action_logger(request.authuser,
                           'admin_updated_users_group:%s' % gr,
-                          None, request.ip_addr, self.sa)
+                          None, request.ip_addr)
             h.flash(_('Updated user group %s') % gr, category='success')
             Session().commit()
         except formencode.Invalid as errors:
@@ -286,7 +286,7 @@
             raise HTTPFound(location=url('edit_user_group_perms', id=id))
         #TODO: implement this
         #action_logger(request.authuser, 'admin_changed_repo_permissions',
-        #              repo_name, request.ip_addr, self.sa)
+        #              repo_name, request.ip_addr)
         Session().commit()
         h.flash(_('User group permissions updated'), category='success')
         raise HTTPFound(location=url('edit_user_group_perms', id=id))
--- a/kallithea/controllers/admin/users.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/admin/users.py	Mon Feb 27 19:47:54 2017 +0100
@@ -122,7 +122,7 @@
             form_result = user_form.to_python(dict(request.POST))
             user = user_model.create(form_result)
             action_logger(request.authuser, 'admin_created_user:%s' % user.username,
-                          None, request.ip_addr, self.sa)
+                          None, request.ip_addr)
             h.flash(_('Created user %s') % user.username,
                     category='success')
             Session().commit()
@@ -161,7 +161,7 @@
             user_model.update(id, form_result, skip_attrs=skip_attrs)
             usr = form_result['username']
             action_logger(request.authuser, 'admin_updated_user:%s' % usr,
-                          None, request.ip_addr, self.sa)
+                          None, request.ip_addr)
             h.flash(_('User updated successfully'), category='success')
             Session().commit()
         except formencode.Invalid as errors:
--- a/kallithea/controllers/changeset.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/changeset.py	Mon Feb 27 19:47:54 2017 +0100
@@ -393,7 +393,7 @@
 
         action_logger(request.authuser,
                       'user_commented_revision:%s' % revision,
-                      c.db_repo, request.ip_addr, self.sa)
+                      c.db_repo, request.ip_addr)
 
         Session().commit()
 
--- a/kallithea/controllers/pullrequests.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/controllers/pullrequests.py	Mon Feb 27 19:47:54 2017 +0100
@@ -818,7 +818,7 @@
 
         action_logger(request.authuser,
                       'user_commented_pull_request:%s' % pull_request_id,
-                      c.db_repo, request.ip_addr, self.sa)
+                      c.db_repo, request.ip_addr)
 
         if status:
             ChangesetStatusModel().set_status(
@@ -833,7 +833,7 @@
             PullRequestModel().close_pull_request(pull_request_id)
             action_logger(request.authuser,
                           'user_closed_pull_request:%s' % pull_request_id,
-                          c.db_repo, request.ip_addr, self.sa)
+                          c.db_repo, request.ip_addr)
 
         Session().commit()
 
--- a/kallithea/lib/celerylib/tasks.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/lib/celerylib/tasks.py	Mon Feb 27 19:47:54 2017 +0100
@@ -373,7 +373,7 @@
         )
 
         action_logger(cur_user, 'user_created_repo',
-                      form_data['repo_name_full'], '', DBS)
+                      form_data['repo_name_full'], '')
 
         DBS.commit()
         # now create this repo on Filesystem
@@ -449,7 +449,7 @@
             copy_fork_permissions=copy_fork_permissions
         )
         action_logger(cur_user, 'user_forked_repo:%s' % repo_name_full,
-                      fork_of.repo_name, '', DBS)
+                      fork_of.repo_name, '')
         DBS.commit()
 
         update_after_clone = form_data['update_after_clone'] # FIXME - unused!
--- a/kallithea/lib/utils.py	Tue Feb 28 15:24:55 2017 +0100
+++ b/kallithea/lib/utils.py	Mon Feb 27 19:47:54 2017 +0100
@@ -137,7 +137,7 @@
     return None
 
 
-def action_logger(user, action, repo, ipaddr='', sa=None, commit=False):
+def action_logger(user, action, repo, ipaddr='', commit=False):
     """
     Action logger for various actions made by users
 
@@ -148,12 +148,9 @@
     :param repo: string name of repository or object containing repo_id,
         that action was made on
     :param ipaddr: optional IP address from what the action was made
-    :param sa: optional sqlalchemy session
 
     """
 
-    if not sa:
-        sa = meta.Session()
     # if we don't get explicit IP address try to get one from registered user
     # in tmpl context var
     if not ipaddr:
@@ -186,12 +183,12 @@
 
     user_log.action_date = datetime.datetime.now()
     user_log.user_ip = ipaddr
-    sa.add(user_log)
+    meta.Session().add(user_log)
 
     log.info('Logging action:%s on %s by user:%s ip:%s',
              action, safe_unicode(repo), user_obj, ipaddr)
     if commit:
-        sa.commit()
+        meta.Session().commit()
 
 
 def get_filesystem_repos(path):