# HG changeset patch # User Marcin Kuzminski # Date 2010-11-22 14:49:04 # Node ID dbec976d99756e49299d2bc4cbf5a0a563b710a3 # Parent 49eb69d789880d5a4fb4db3c38bccf39b93eda71 added action loggers to following repositories, refactoring of repo_model get functions diff --git a/rhodecode/controllers/admin/repos.py b/rhodecode/controllers/admin/repos.py --- a/rhodecode/controllers/admin/repos.py +++ b/rhodecode/controllers/admin/repos.py @@ -140,7 +140,7 @@ class ReposController(BaseController): changed_name, '', self.sa) except formencode.Invalid, errors: - c.repo_info = repo_model.get(repo_name) + c.repo_info = repo_model.get_by_repo_name(repo_name) c.users_array = repo_model.get_users_js() errors.value.update({'user':c.repo_info.user.username}) return htmlfill.render( @@ -168,7 +168,7 @@ class ReposController(BaseController): # url('repo', repo_name=ID) repo_model = RepoModel() - repo = repo_model.get(repo_name) + repo = repo_model.get_by_repo_name(repo_name) if not repo: h.flash(_('%s repository is not mapped to db perhaps' ' it was moved or renamed from the filesystem' @@ -245,7 +245,7 @@ class ReposController(BaseController): """GET /repos/repo_name/edit: Form to edit an existing item""" # url('edit_repo', repo_name=ID) repo_model = RepoModel() - c.repo_info = repo = repo_model.get(repo_name) + c.repo_info = repo = repo_model.get_by_repo_name(repo_name) if repo.stats: last_rev = repo.stats.stat_on_revision else: diff --git a/rhodecode/controllers/journal.py b/rhodecode/controllers/journal.py --- a/rhodecode/controllers/journal.py +++ b/rhodecode/controllers/journal.py @@ -28,6 +28,7 @@ from pylons.controllers.util import abor from rhodecode.lib.auth import LoginRequired from rhodecode.lib.base import BaseController, render from rhodecode.lib.helpers import get_token +from rhodecode.lib.utils import action_logger from rhodecode.model.db import UserLog, UserFollowing from rhodecode.model.scm import ScmModel import logging @@ -56,7 +57,6 @@ class JournalController(BaseController): def toggle_following(self): - print c.rhodecode_user if request.POST.get('auth_token') == get_token(): scm_model = ScmModel() diff --git a/rhodecode/controllers/settings.py b/rhodecode/controllers/settings.py --- a/rhodecode/controllers/settings.py +++ b/rhodecode/controllers/settings.py @@ -47,7 +47,7 @@ class SettingsController(BaseController) def index(self, repo_name): repo_model = RepoModel() - c.repo_info = repo = repo_model.get(repo_name) + c.repo_info = repo = repo_model.get_by_repo_name(repo_name) if not repo: h.flash(_('%s repository is not mapped to db perhaps' ' it was created or renamed from the filesystem' @@ -85,7 +85,7 @@ class SettingsController(BaseController) action_logger(self.rhodecode_user, 'user_updated_repo', changed_name, '', self.sa) except formencode.Invalid, errors: - c.repo_info = repo_model.get(repo_name) + c.repo_info = repo_model.get_by_repo_name(repo_name) c.users_array = repo_model.get_users_js() errors.value.update({'user':c.repo_info.user.username}) return htmlfill.render( @@ -113,7 +113,7 @@ class SettingsController(BaseController) # url('repo_settings_delete', repo_name=ID) repo_model = RepoModel() - repo = repo_model.get(repo_name) + repo = repo_model.get_by_repo_name(repo_name) if not repo: h.flash(_('%s repository is not mapped to db perhaps' ' it was moved or renamed from the filesystem' @@ -136,7 +136,7 @@ class SettingsController(BaseController) def fork(self, repo_name): repo_model = RepoModel() - c.repo_info = repo = repo_model.get(repo_name) + c.repo_info = repo = repo_model.get_by_repo_name(repo_name) if not repo: h.flash(_('%s repository is not mapped to db perhaps' ' it was created or renamed from the filesystem' @@ -152,7 +152,7 @@ class SettingsController(BaseController) def fork_create(self, repo_name): repo_model = RepoModel() - c.repo_info = repo_model.get(repo_name) + c.repo_info = repo_model.get_by_repo_name(repo_name) _form = RepoForkForm(old_data={'repo_type':c.repo_info.repo_type})() form_result = {} try: diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -459,7 +459,10 @@ def action_parser(user_log): 'admin_forked_repo':_('Admin forked repository'), 'admin_updated_repo':_('Admin updated repository'), 'push':_('Pushed') + get_cs_links(), - 'pull':_('Pulled'), } + 'pull':_('Pulled'), + 'started_following_repo':_('User started following repository'), + 'stopped_following_repo':_('User stopped following repository'), + } return map.get(action, action) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -70,13 +70,14 @@ def is_git(environ): def action_logger(user, action, repo, ipaddr='', sa=None): """ - Action logger for various action made by users + Action logger for various actions made by users - :param user: user that made this action, can be a string unique username or + :param user: user that made this action, can be a unique username string or object containing user_id attribute :param action: action to log, should be on of predefined unique actions for easy translations - :param repo: repository that action was made on + :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 @@ -86,20 +87,22 @@ def action_logger(user, action, repo, ip sa = meta.Session() try: + um = UserModel() if hasattr(user, 'user_id'): user_obj = user elif isinstance(user, basestring): - user_obj = UserModel().get_by_username(user, cache=False) + user_obj = um.get_by_username(user, cache=False) else: raise Exception('You have to provide user object or username') - if repo: + rm = RepoModel() + if hasattr(repo, 'repo_id'): + repo_obj = rm.get(repo.repo_id, cache=False) + repo_name = repo_obj.repo_name + elif isinstance(repo, basestring): repo_name = repo.lstrip('/') - - repository = RepoModel().get(repo_name, cache=False) - if not repository: - raise Exception('You have to provide valid repository') + repo_obj = rm.get_by_repo_name(repo_name, cache=False) else: raise Exception('You have to provide repository to action logger') @@ -107,8 +110,10 @@ def action_logger(user, action, repo, ip 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.repository = repository + user_log.action_date = datetime.datetime.now() user_log.user_ip = ipaddr sa.add(user_log) @@ -352,7 +357,7 @@ def repo2db_mapper(initial_repo_list, re user = sa.query(User).filter(User.admin == True).first() for name, repo in initial_repo_list.items(): - if not rm.get(name, cache=False): + if not rm.get_by_repo_name(name, cache=False): log.info('repository %s not found creating default', name) form_data = { diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -170,7 +170,7 @@ def ValidRepoName(edit, old_data): raise formencode.Invalid(_('This repository name is disallowed'), value, state) if old_data.get('repo_name') != value or not edit: - if RepoModel().get(slug, cache=False): + if RepoModel().get_by_repo_name(slug, cache=False): raise formencode.Invalid(_('This repository already exists') , value, state) return slug diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -42,13 +42,23 @@ class RepoModel(object): def get(self, repo_id, cache=False): repo = self.sa.query(Repository)\ - .filter(Repository.repo_name == repo_id) + .filter(Repository.repo_id == repo_id) if cache: repo = repo.options(FromCache("sql_cache_short", - "get_repo_%s" % repo)) + "get_repo_%s" % repo_id)) return repo.scalar() + + def get_by_repo_name(self, repo_name, cache=False): + repo = self.sa.query(Repository)\ + .filter(Repository.repo_name == repo_name) + + if cache: + repo = repo.options(FromCache("sql_cache_short", + "get_repo_%s" % repo_name)) + return repo.scalar() + def get_users_js(self): users = self.sa.query(User).filter(User.active == True).all() @@ -65,19 +75,21 @@ class RepoModel(object): #update permissions for username, perm in form_data['perms_updates']: r2p = self.sa.query(RepoToPerm)\ - .filter(RepoToPerm.user == UserModel().get_by_username(username, cache=False))\ - .filter(RepoToPerm.repository == self.get(repo_name))\ + .filter(RepoToPerm.user == UserModel()\ + .get_by_username(username, cache=False))\ + .filter(RepoToPerm.repository == \ + self.get_by_repo_name(repo_name))\ .one() r2p.permission_id = self.sa.query(Permission).filter( - Permission.permission_name == + Permission.permission_name == perm).one().permission_id self.sa.add(r2p) #set new permissions for username, perm in form_data['perms_new']: r2p = RepoToPerm() - r2p.repository = self.get(repo_name) + r2p.repository = self.get_by_repo_name(repo_name) r2p.user = UserModel().get_by_username(username, cache=False) r2p.permission_id = self.sa.query(Permission).filter( @@ -86,7 +98,7 @@ class RepoModel(object): self.sa.add(r2p) #update current repo - cur_repo = self.get(repo_name, cache=False) + cur_repo = self.get_by_repo_name(repo_name, cache=False) for k, v in form_data.items(): if k == 'user': @@ -172,7 +184,8 @@ class RepoModel(object): def delete_perm_user(self, form_data, repo_name): try: self.sa.query(RepoToPerm)\ - .filter(RepoToPerm.repository == self.get(repo_name))\ + .filter(RepoToPerm.repository \ + == self.get_by_repo_name(repo_name))\ .filter(RepoToPerm.user_id == form_data['user_id']).delete() self.sa.commit() except: @@ -183,7 +196,8 @@ class RepoModel(object): def delete_stats(self, repo_name): try: self.sa.query(Statistics)\ - .filter(Statistics.repository == self.get(repo_name)).delete() + .filter(Statistics.repository == \ + self.get_by_repo_name(repo_name)).delete() self.sa.commit() except: log.error(traceback.format_exc()) diff --git a/rhodecode/model/scm.py b/rhodecode/model/scm.py --- a/rhodecode/model/scm.py +++ b/rhodecode/model/scm.py @@ -27,7 +27,7 @@ from mercurial import ui from rhodecode import BACKENDS from rhodecode.lib import helpers as h from rhodecode.lib.auth import HasRepoPermissionAny -from rhodecode.lib.utils import get_repos, make_ui +from rhodecode.lib.utils import get_repos, make_ui, action_logger from rhodecode.model import meta from rhodecode.model.db import Repository, User, RhodeCodeUi, CacheInvalidation, \ UserFollowing @@ -45,6 +45,15 @@ import time log = logging.getLogger(__name__) +class UserTemp(object): + def __init__(self, user_id): + self.user_id = user_id + +class RepoTemp(object): + def __init__(self, repo_id): + self.repo_id = repo_id + + class ScmModel(object): """ Mercurial Model @@ -227,9 +236,13 @@ class ScmModel(object): .filter(UserFollowing.user_id == user_id).scalar() if f is not None: + try: self.sa.delete(f) self.sa.commit() + action_logger(UserTemp(user_id), + 'stopped_following_repo', + RepoTemp(follow_repo_id)) return except: log.error(traceback.format_exc()) @@ -243,6 +256,9 @@ class ScmModel(object): f.follows_repo_id = follow_repo_id self.sa.add(f) self.sa.commit() + action_logger(UserTemp(user_id), + 'started_following_repo', + RepoTemp(follow_repo_id)) except: log.error(traceback.format_exc()) self.sa.rollback()