##// END OF EJS Templates
added action loggers to following repositories,...
marcink -
r735:dbec976d beta
parent child Browse files
Show More
@@ -140,7 +140,7 b' class ReposController(BaseController):'
140 changed_name, '', self.sa)
140 changed_name, '', self.sa)
141
141
142 except formencode.Invalid, errors:
142 except formencode.Invalid, errors:
143 c.repo_info = repo_model.get(repo_name)
143 c.repo_info = repo_model.get_by_repo_name(repo_name)
144 c.users_array = repo_model.get_users_js()
144 c.users_array = repo_model.get_users_js()
145 errors.value.update({'user':c.repo_info.user.username})
145 errors.value.update({'user':c.repo_info.user.username})
146 return htmlfill.render(
146 return htmlfill.render(
@@ -168,7 +168,7 b' class ReposController(BaseController):'
168 # url('repo', repo_name=ID)
168 # url('repo', repo_name=ID)
169
169
170 repo_model = RepoModel()
170 repo_model = RepoModel()
171 repo = repo_model.get(repo_name)
171 repo = repo_model.get_by_repo_name(repo_name)
172 if not repo:
172 if not repo:
173 h.flash(_('%s repository is not mapped to db perhaps'
173 h.flash(_('%s repository is not mapped to db perhaps'
174 ' it was moved or renamed from the filesystem'
174 ' it was moved or renamed from the filesystem'
@@ -245,7 +245,7 b' class ReposController(BaseController):'
245 """GET /repos/repo_name/edit: Form to edit an existing item"""
245 """GET /repos/repo_name/edit: Form to edit an existing item"""
246 # url('edit_repo', repo_name=ID)
246 # url('edit_repo', repo_name=ID)
247 repo_model = RepoModel()
247 repo_model = RepoModel()
248 c.repo_info = repo = repo_model.get(repo_name)
248 c.repo_info = repo = repo_model.get_by_repo_name(repo_name)
249 if repo.stats:
249 if repo.stats:
250 last_rev = repo.stats.stat_on_revision
250 last_rev = repo.stats.stat_on_revision
251 else:
251 else:
@@ -28,6 +28,7 b' from pylons.controllers.util import abor'
28 from rhodecode.lib.auth import LoginRequired
28 from rhodecode.lib.auth import LoginRequired
29 from rhodecode.lib.base import BaseController, render
29 from rhodecode.lib.base import BaseController, render
30 from rhodecode.lib.helpers import get_token
30 from rhodecode.lib.helpers import get_token
31 from rhodecode.lib.utils import action_logger
31 from rhodecode.model.db import UserLog, UserFollowing
32 from rhodecode.model.db import UserLog, UserFollowing
32 from rhodecode.model.scm import ScmModel
33 from rhodecode.model.scm import ScmModel
33 import logging
34 import logging
@@ -56,7 +57,6 b' class JournalController(BaseController):'
56
57
57
58
58 def toggle_following(self):
59 def toggle_following(self):
59 print c.rhodecode_user
60
60
61 if request.POST.get('auth_token') == get_token():
61 if request.POST.get('auth_token') == get_token():
62 scm_model = ScmModel()
62 scm_model = ScmModel()
@@ -47,7 +47,7 b' class SettingsController(BaseController)'
47
47
48 def index(self, repo_name):
48 def index(self, repo_name):
49 repo_model = RepoModel()
49 repo_model = RepoModel()
50 c.repo_info = repo = repo_model.get(repo_name)
50 c.repo_info = repo = repo_model.get_by_repo_name(repo_name)
51 if not repo:
51 if not repo:
52 h.flash(_('%s repository is not mapped to db perhaps'
52 h.flash(_('%s repository is not mapped to db perhaps'
53 ' it was created or renamed from the filesystem'
53 ' it was created or renamed from the filesystem'
@@ -85,7 +85,7 b' class SettingsController(BaseController)'
85 action_logger(self.rhodecode_user, 'user_updated_repo',
85 action_logger(self.rhodecode_user, 'user_updated_repo',
86 changed_name, '', self.sa)
86 changed_name, '', self.sa)
87 except formencode.Invalid, errors:
87 except formencode.Invalid, errors:
88 c.repo_info = repo_model.get(repo_name)
88 c.repo_info = repo_model.get_by_repo_name(repo_name)
89 c.users_array = repo_model.get_users_js()
89 c.users_array = repo_model.get_users_js()
90 errors.value.update({'user':c.repo_info.user.username})
90 errors.value.update({'user':c.repo_info.user.username})
91 return htmlfill.render(
91 return htmlfill.render(
@@ -113,7 +113,7 b' class SettingsController(BaseController)'
113 # url('repo_settings_delete', repo_name=ID)
113 # url('repo_settings_delete', repo_name=ID)
114
114
115 repo_model = RepoModel()
115 repo_model = RepoModel()
116 repo = repo_model.get(repo_name)
116 repo = repo_model.get_by_repo_name(repo_name)
117 if not repo:
117 if not repo:
118 h.flash(_('%s repository is not mapped to db perhaps'
118 h.flash(_('%s repository is not mapped to db perhaps'
119 ' it was moved or renamed from the filesystem'
119 ' it was moved or renamed from the filesystem'
@@ -136,7 +136,7 b' class SettingsController(BaseController)'
136
136
137 def fork(self, repo_name):
137 def fork(self, repo_name):
138 repo_model = RepoModel()
138 repo_model = RepoModel()
139 c.repo_info = repo = repo_model.get(repo_name)
139 c.repo_info = repo = repo_model.get_by_repo_name(repo_name)
140 if not repo:
140 if not repo:
141 h.flash(_('%s repository is not mapped to db perhaps'
141 h.flash(_('%s repository is not mapped to db perhaps'
142 ' it was created or renamed from the filesystem'
142 ' it was created or renamed from the filesystem'
@@ -152,7 +152,7 b' class SettingsController(BaseController)'
152
152
153 def fork_create(self, repo_name):
153 def fork_create(self, repo_name):
154 repo_model = RepoModel()
154 repo_model = RepoModel()
155 c.repo_info = repo_model.get(repo_name)
155 c.repo_info = repo_model.get_by_repo_name(repo_name)
156 _form = RepoForkForm(old_data={'repo_type':c.repo_info.repo_type})()
156 _form = RepoForkForm(old_data={'repo_type':c.repo_info.repo_type})()
157 form_result = {}
157 form_result = {}
158 try:
158 try:
@@ -459,7 +459,10 b' def action_parser(user_log):'
459 'admin_forked_repo':_('Admin forked repository'),
459 'admin_forked_repo':_('Admin forked repository'),
460 'admin_updated_repo':_('Admin updated repository'),
460 'admin_updated_repo':_('Admin updated repository'),
461 'push':_('Pushed') + get_cs_links(),
461 'push':_('Pushed') + get_cs_links(),
462 'pull':_('Pulled'), }
462 'pull':_('Pulled'),
463 'started_following_repo':_('User started following repository'),
464 'stopped_following_repo':_('User stopped following repository'),
465 }
463
466
464 return map.get(action, action)
467 return map.get(action, action)
465
468
@@ -70,13 +70,14 b' def is_git(environ):'
70
70
71 def action_logger(user, action, repo, ipaddr='', sa=None):
71 def action_logger(user, action, repo, ipaddr='', sa=None):
72 """
72 """
73 Action logger for various action made by users
73 Action logger for various actions made by users
74
74
75 :param user: user that made this action, can be a string unique username or
75 :param user: user that made this action, can be a unique username string or
76 object containing user_id attribute
76 object containing user_id attribute
77 :param action: action to log, should be on of predefined unique actions for
77 :param action: action to log, should be on of predefined unique actions for
78 easy translations
78 easy translations
79 :param repo: repository that action was made on
79 :param repo: string name of repository or object containing repo_id,
80 that action was made on
80 :param ipaddr: optional ip address from what the action was made
81 :param ipaddr: optional ip address from what the action was made
81 :param sa: optional sqlalchemy session
82 :param sa: optional sqlalchemy session
82
83
@@ -86,20 +87,22 b' def action_logger(user, action, repo, ip'
86 sa = meta.Session()
87 sa = meta.Session()
87
88
88 try:
89 try:
90 um = UserModel()
89 if hasattr(user, 'user_id'):
91 if hasattr(user, 'user_id'):
90 user_obj = user
92 user_obj = user
91 elif isinstance(user, basestring):
93 elif isinstance(user, basestring):
92 user_obj = UserModel().get_by_username(user, cache=False)
94 user_obj = um.get_by_username(user, cache=False)
93 else:
95 else:
94 raise Exception('You have to provide user object or username')
96 raise Exception('You have to provide user object or username')
95
97
96
98
97 if repo:
99 rm = RepoModel()
100 if hasattr(repo, 'repo_id'):
101 repo_obj = rm.get(repo.repo_id, cache=False)
102 repo_name = repo_obj.repo_name
103 elif isinstance(repo, basestring):
98 repo_name = repo.lstrip('/')
104 repo_name = repo.lstrip('/')
99
105 repo_obj = rm.get_by_repo_name(repo_name, cache=False)
100 repository = RepoModel().get(repo_name, cache=False)
101 if not repository:
102 raise Exception('You have to provide valid repository')
103 else:
106 else:
104 raise Exception('You have to provide repository to action logger')
107 raise Exception('You have to provide repository to action logger')
105
108
@@ -107,8 +110,10 b' def action_logger(user, action, repo, ip'
107 user_log = UserLog()
110 user_log = UserLog()
108 user_log.user_id = user_obj.user_id
111 user_log.user_id = user_obj.user_id
109 user_log.action = action
112 user_log.action = action
113
114 user_log.repository_id = repo_obj.repo_id
110 user_log.repository_name = repo_name
115 user_log.repository_name = repo_name
111 user_log.repository = repository
116
112 user_log.action_date = datetime.datetime.now()
117 user_log.action_date = datetime.datetime.now()
113 user_log.user_ip = ipaddr
118 user_log.user_ip = ipaddr
114 sa.add(user_log)
119 sa.add(user_log)
@@ -352,7 +357,7 b' def repo2db_mapper(initial_repo_list, re'
352 user = sa.query(User).filter(User.admin == True).first()
357 user = sa.query(User).filter(User.admin == True).first()
353
358
354 for name, repo in initial_repo_list.items():
359 for name, repo in initial_repo_list.items():
355 if not rm.get(name, cache=False):
360 if not rm.get_by_repo_name(name, cache=False):
356 log.info('repository %s not found creating default', name)
361 log.info('repository %s not found creating default', name)
357
362
358 form_data = {
363 form_data = {
@@ -170,7 +170,7 b' def ValidRepoName(edit, old_data):'
170 raise formencode.Invalid(_('This repository name is disallowed'),
170 raise formencode.Invalid(_('This repository name is disallowed'),
171 value, state)
171 value, state)
172 if old_data.get('repo_name') != value or not edit:
172 if old_data.get('repo_name') != value or not edit:
173 if RepoModel().get(slug, cache=False):
173 if RepoModel().get_by_repo_name(slug, cache=False):
174 raise formencode.Invalid(_('This repository already exists') ,
174 raise formencode.Invalid(_('This repository already exists') ,
175 value, state)
175 value, state)
176 return slug
176 return slug
@@ -42,13 +42,23 b' class RepoModel(object):'
42
42
43 def get(self, repo_id, cache=False):
43 def get(self, repo_id, cache=False):
44 repo = self.sa.query(Repository)\
44 repo = self.sa.query(Repository)\
45 .filter(Repository.repo_name == repo_id)
45 .filter(Repository.repo_id == repo_id)
46
46
47 if cache:
47 if cache:
48 repo = repo.options(FromCache("sql_cache_short",
48 repo = repo.options(FromCache("sql_cache_short",
49 "get_repo_%s" % repo))
49 "get_repo_%s" % repo_id))
50 return repo.scalar()
50 return repo.scalar()
51
51
52
53 def get_by_repo_name(self, repo_name, cache=False):
54 repo = self.sa.query(Repository)\
55 .filter(Repository.repo_name == repo_name)
56
57 if cache:
58 repo = repo.options(FromCache("sql_cache_short",
59 "get_repo_%s" % repo_name))
60 return repo.scalar()
61
52 def get_users_js(self):
62 def get_users_js(self):
53
63
54 users = self.sa.query(User).filter(User.active == True).all()
64 users = self.sa.query(User).filter(User.active == True).all()
@@ -65,19 +75,21 b' class RepoModel(object):'
65 #update permissions
75 #update permissions
66 for username, perm in form_data['perms_updates']:
76 for username, perm in form_data['perms_updates']:
67 r2p = self.sa.query(RepoToPerm)\
77 r2p = self.sa.query(RepoToPerm)\
68 .filter(RepoToPerm.user == UserModel().get_by_username(username, cache=False))\
78 .filter(RepoToPerm.user == UserModel()\
69 .filter(RepoToPerm.repository == self.get(repo_name))\
79 .get_by_username(username, cache=False))\
80 .filter(RepoToPerm.repository == \
81 self.get_by_repo_name(repo_name))\
70 .one()
82 .one()
71
83
72 r2p.permission_id = self.sa.query(Permission).filter(
84 r2p.permission_id = self.sa.query(Permission).filter(
73 Permission.permission_name ==
85 Permission.permission_name ==
74 perm).one().permission_id
86 perm).one().permission_id
75 self.sa.add(r2p)
87 self.sa.add(r2p)
76
88
77 #set new permissions
89 #set new permissions
78 for username, perm in form_data['perms_new']:
90 for username, perm in form_data['perms_new']:
79 r2p = RepoToPerm()
91 r2p = RepoToPerm()
80 r2p.repository = self.get(repo_name)
92 r2p.repository = self.get_by_repo_name(repo_name)
81 r2p.user = UserModel().get_by_username(username, cache=False)
93 r2p.user = UserModel().get_by_username(username, cache=False)
82
94
83 r2p.permission_id = self.sa.query(Permission).filter(
95 r2p.permission_id = self.sa.query(Permission).filter(
@@ -86,7 +98,7 b' class RepoModel(object):'
86 self.sa.add(r2p)
98 self.sa.add(r2p)
87
99
88 #update current repo
100 #update current repo
89 cur_repo = self.get(repo_name, cache=False)
101 cur_repo = self.get_by_repo_name(repo_name, cache=False)
90
102
91 for k, v in form_data.items():
103 for k, v in form_data.items():
92 if k == 'user':
104 if k == 'user':
@@ -172,7 +184,8 b' class RepoModel(object):'
172 def delete_perm_user(self, form_data, repo_name):
184 def delete_perm_user(self, form_data, repo_name):
173 try:
185 try:
174 self.sa.query(RepoToPerm)\
186 self.sa.query(RepoToPerm)\
175 .filter(RepoToPerm.repository == self.get(repo_name))\
187 .filter(RepoToPerm.repository \
188 == self.get_by_repo_name(repo_name))\
176 .filter(RepoToPerm.user_id == form_data['user_id']).delete()
189 .filter(RepoToPerm.user_id == form_data['user_id']).delete()
177 self.sa.commit()
190 self.sa.commit()
178 except:
191 except:
@@ -183,7 +196,8 b' class RepoModel(object):'
183 def delete_stats(self, repo_name):
196 def delete_stats(self, repo_name):
184 try:
197 try:
185 self.sa.query(Statistics)\
198 self.sa.query(Statistics)\
186 .filter(Statistics.repository == self.get(repo_name)).delete()
199 .filter(Statistics.repository == \
200 self.get_by_repo_name(repo_name)).delete()
187 self.sa.commit()
201 self.sa.commit()
188 except:
202 except:
189 log.error(traceback.format_exc())
203 log.error(traceback.format_exc())
@@ -27,7 +27,7 b' from mercurial import ui'
27 from rhodecode import BACKENDS
27 from rhodecode import BACKENDS
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
29 from rhodecode.lib.auth import HasRepoPermissionAny
29 from rhodecode.lib.auth import HasRepoPermissionAny
30 from rhodecode.lib.utils import get_repos, make_ui
30 from rhodecode.lib.utils import get_repos, make_ui, action_logger
31 from rhodecode.model import meta
31 from rhodecode.model import meta
32 from rhodecode.model.db import Repository, User, RhodeCodeUi, CacheInvalidation, \
32 from rhodecode.model.db import Repository, User, RhodeCodeUi, CacheInvalidation, \
33 UserFollowing
33 UserFollowing
@@ -45,6 +45,15 b' import time'
45
45
46 log = logging.getLogger(__name__)
46 log = logging.getLogger(__name__)
47
47
48 class UserTemp(object):
49 def __init__(self, user_id):
50 self.user_id = user_id
51
52 class RepoTemp(object):
53 def __init__(self, repo_id):
54 self.repo_id = repo_id
55
56
48 class ScmModel(object):
57 class ScmModel(object):
49 """
58 """
50 Mercurial Model
59 Mercurial Model
@@ -227,9 +236,13 b' class ScmModel(object):'
227 .filter(UserFollowing.user_id == user_id).scalar()
236 .filter(UserFollowing.user_id == user_id).scalar()
228
237
229 if f is not None:
238 if f is not None:
239
230 try:
240 try:
231 self.sa.delete(f)
241 self.sa.delete(f)
232 self.sa.commit()
242 self.sa.commit()
243 action_logger(UserTemp(user_id),
244 'stopped_following_repo',
245 RepoTemp(follow_repo_id))
233 return
246 return
234 except:
247 except:
235 log.error(traceback.format_exc())
248 log.error(traceback.format_exc())
@@ -243,6 +256,9 b' class ScmModel(object):'
243 f.follows_repo_id = follow_repo_id
256 f.follows_repo_id = follow_repo_id
244 self.sa.add(f)
257 self.sa.add(f)
245 self.sa.commit()
258 self.sa.commit()
259 action_logger(UserTemp(user_id),
260 'started_following_repo',
261 RepoTemp(follow_repo_id))
246 except:
262 except:
247 log.error(traceback.format_exc())
263 log.error(traceback.format_exc())
248 self.sa.rollback()
264 self.sa.rollback()
General Comments 0
You need to be logged in to leave comments. Login now