# HG changeset patch # User Marcin Kuzminski # Date 2013-06-18 00:06:01 # Node ID 55dbc440878b48cc8d2f8564ef84afbec80a6891 # Parent 4959e22af6ca595c2fa4806b438b7abe1c98878d Fixed bug with log_delete hook didn't properly store user who triggered delete action diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -55,7 +55,7 @@ from rhodecode.model.db import Repositor UserLog, RepoGroup, RhodeCodeSetting, CacheInvalidation, UserGroup from rhodecode.model.meta import Session from rhodecode.model.repos_group import ReposGroupModel -from rhodecode.lib.utils2 import safe_str, safe_unicode +from rhodecode.lib.utils2 import safe_str, safe_unicode, get_current_rhodecode_user from rhodecode.lib.vcs.utils.fakemod import create_module from rhodecode.model.users_group import UserGroupModel @@ -150,9 +150,8 @@ def action_logger(user, action, repo, ip sa = meta.Session() # if we don't get explicit IP address try to get one from registered user # in tmpl context var - from pylons import tmpl_context - if not ipaddr and hasattr(tmpl_context, 'rhodecode_user'): - ipaddr = tmpl_context.rhodecode_user.ip_addr + if not ipaddr: + ipaddr = getattr(get_current_rhodecode_user(), 'ip_addr', '') try: if hasattr(user, 'user_id'): diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -642,3 +642,14 @@ def suuid(url=None, truncate_to=22, alph output.append(_ALPHABET[digit]) unique_id = int(unique_id / alphabet_length) return "".join(output)[:truncate_to] + +def get_current_rhodecode_user(): + """ + Get's rhodecode user from threadlocal tmpl_context variable if it's + defined, else returns None. + """ + from pylons import tmpl_context + if hasattr(tmpl_context, 'rhodecode_user'): + return tmpl_context.rhodecode_user + + return None diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -32,7 +32,7 @@ from datetime import datetime from rhodecode.lib.vcs.backends import get_backend from rhodecode.lib.compat import json from rhodecode.lib.utils2 import LazyProperty, safe_str, safe_unicode,\ - remove_prefix, obfuscate_url_pw + remove_prefix, obfuscate_url_pw, get_current_rhodecode_user from rhodecode.lib.caching_query import FromCache from rhodecode.lib.hooks import log_create_repository, log_delete_repository @@ -504,7 +504,7 @@ class RepoModel(BaseModel): from rhodecode.lib.celerylib import tasks, run_task run_task(tasks.create_repo_fork, form_data, cur_user) - def delete(self, repo, forks=None, fs_remove=True): + def delete(self, repo, forks=None, fs_remove=True, cur_user=None): """ Delete given repository, forks parameter defines what do do with attached forks. Throws AttachedForksError if deleted repo has attached @@ -514,6 +514,8 @@ class RepoModel(BaseModel): :param forks: str 'delete' or 'detach' :param fs_remove: remove(archive) repo from filesystem """ + if not cur_user: + cur_user = getattr(get_current_rhodecode_user(), 'username', '?') repo = self._get_repo(repo) if repo: if forks == 'detach': @@ -535,7 +537,7 @@ class RepoModel(BaseModel): else: log.debug('skipping removal from filesystem') log_delete_repository(old_repo_dict, - deleted_by=owner.username) + deleted_by=cur_user) except Exception: log.error(traceback.format_exc()) raise