##// END OF EJS Templates
made action logger more global, to be used in other places to log other actions....
marcink -
r536:39203995 default
parent child Browse files
Show More
@@ -63,10 +63,10 b' def user_action_mapper(ui, repo, hooktyp'
63 username = kwargs['url'].split(':')[-1]
63 username = kwargs['url'].split(':')[-1]
64 user_log = sa.query(UserLog)\
64 user_log = sa.query(UserLog)\
65 .filter(UserLog.user == sa.query(User)\
65 .filter(UserLog.user == sa.query(User)\
66 .filter(User.username == username).one())\
66 .filter(User.username == username).one())\
67 .order_by(UserLog.user_log_id.desc()).first()
67 .order_by(UserLog.user_log_id.desc()).first()
68
68
69 if not user_log.revision:
69 if user_log and not user_log.revision:
70 user_log.revision = str(repo['tip'])
70 user_log.revision = str(repo['tip'])
71 sa.add(user_log)
71 sa.add(user_log)
72 sa.commit()
72 sa.commit()
@@ -24,7 +24,6 b' Created on 2010-04-28'
24 SimpleHG middleware for handling mercurial protocol request (push/clone etc.)
24 SimpleHG middleware for handling mercurial protocol request (push/clone etc.)
25 It's implemented with basic auth function
25 It's implemented with basic auth function
26 """
26 """
27 from datetime import datetime
28 from itertools import chain
27 from itertools import chain
29 from mercurial.error import RepoError
28 from mercurial.error import RepoError
30 from mercurial.hgweb import hgweb
29 from mercurial.hgweb import hgweb
@@ -35,12 +34,10 b' from pylons_app.lib.auth import authfunc'
35 get_user_cached
34 get_user_cached
36 from pylons_app.lib.utils import is_mercurial, make_ui, invalidate_cache, \
35 from pylons_app.lib.utils import is_mercurial, make_ui, invalidate_cache, \
37 check_repo_fast, ui_sections
36 check_repo_fast, ui_sections
38 from pylons_app.model import meta
39 from pylons_app.model.db import UserLog, User
40 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
37 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
38 from pylons_app.lib.utils import action_logger
41 import logging
39 import logging
42 import os
40 import os
43 import pylons_app.lib.helpers as h
44 import traceback
41 import traceback
45
42
46 log = logging.getLogger(__name__)
43 log = logging.getLogger(__name__)
@@ -183,23 +180,7 b' class SimpleHg(object):'
183 return mapping[cmd]
180 return mapping[cmd]
184
181
185 def __log_user_action(self, user, action, repo, ipaddr):
182 def __log_user_action(self, user, action, repo, ipaddr):
186 sa = meta.Session
183 action_logger(user, action, repo, ipaddr)
187 try:
188 user_log = UserLog()
189 user_log.user_id = user.user_id
190 user_log.action = action
191 user_log.repository = repo.replace('/', '')
192 user_log.action_date = datetime.now()
193 user_log.user_ip = ipaddr
194 sa.add(user_log)
195 sa.commit()
196 log.info('Adding user %s, action %s on %s',
197 user.username, action, repo)
198 except Exception, e:
199 sa.rollback()
200 log.error('could not log user action:%s', str(e))
201 finally:
202 meta.Session.remove()
203
184
204 def __invalidate_cache(self, repo_name):
185 def __invalidate_cache(self, repo_name):
205 """we know that some change was made to repositories and we should
186 """we know that some change was made to repositories and we should
@@ -26,16 +26,17 b' from beaker.cache import cache_region'
26 from mercurial import ui, config, hg
26 from mercurial import ui, config, hg
27 from mercurial.error import RepoError
27 from mercurial.error import RepoError
28 from pylons_app.model import meta
28 from pylons_app.model import meta
29 from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings
29 from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings,UserLog
30 from vcs.backends.base import BaseChangeset
30 from vcs.backends.base import BaseChangeset
31 from vcs.utils.lazy import LazyProperty
31 from vcs.utils.lazy import LazyProperty
32 import logging
32 import logging
33 import datetime
33 import os
34 import os
34
35
35 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
36
37
37
38
38 def get_repo_slug(request):
39 def get_repo_slug(request):
39 return request.environ['pylons.routes_dict'].get('repo_name')
40 return request.environ['pylons.routes_dict'].get('repo_name')
40
41
41 def is_mercurial(environ):
42 def is_mercurial(environ):
@@ -48,6 +49,40 b' def is_mercurial(environ):'
48 return True
49 return True
49 return False
50 return False
50
51
52 def action_logger(user, action, repo, ipaddr, sa=None):
53 """
54 Action logger for various action made by users
55 """
56
57 if not sa:
58 sa = meta.Session
59
60
61 if hasattr(user, 'user_id'):
62 user_id = user.user_id
63 elif isinstance(user, basestring):
64
65 user_id = sa.Query(User).filter(User.username == user).one()
66 else:
67 raise Exception('You have to provide user object or username')
68
69 try:
70 user_log = UserLog()
71 user_log.user_id = user_id
72 user_log.action = action
73 user_log.repository = sa.query(Repository)\
74 .filter(Repository.repo_name==repo.lstrip('/')).one()
75 user_log.action_date = datetime.datetime.now()
76 user_log.user_ip = ipaddr
77 sa.add(user_log)
78 sa.commit()
79 log.info('Adding user %s, action %s on %s',
80 user.username, action, repo)
81 except Exception, e:
82 raise
83 sa.rollback()
84 log.error('could not log user action:%s', str(e))
85
51 def check_repo_dir(paths):
86 def check_repo_dir(paths):
52 repos_path = paths[0][1].split('/')
87 repos_path = paths[0][1].split('/')
53 if repos_path[-1] in ['*', '**']:
88 if repos_path[-1] in ['*', '**']:
@@ -66,12 +66,14 b' class UserLog(Base):'
66 __table_args__ = {'useexisting':True}
66 __table_args__ = {'useexisting':True}
67 user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
67 user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
68 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
68 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
69 repository_id = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
69 user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
70 user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
70 repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_name'), nullable=False, unique=None, default=None)
71 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
71 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
72 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
72 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
73 revision = Column('revision', TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
73 revision = Column('revision', TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
74
74 user = relation('User')
75 user = relation('User')
76 repository = relation('Repository')
75
77
76 class Repository(Base):
78 class Repository(Base):
77 __tablename__ = 'repositories'
79 __tablename__ = 'repositories'
@@ -12,11 +12,11 b''
12 %for cnt,l in enumerate(c.users_log):
12 %for cnt,l in enumerate(c.users_log):
13 <tr class="parity${cnt%2}">
13 <tr class="parity${cnt%2}">
14 <td>${h.link_to(l.user.username,h.url('edit_user', id=l.user.user_id))}</td>
14 <td>${h.link_to(l.user.username,h.url('edit_user', id=l.user.user_id))}</td>
15 <td>${h.link_to(l.repository,h.url('summary_home',repo_name=l.repository))}</td>
15 <td>${h.link_to(l.repository.repo_name,h.url('summary_home',repo_name=l.repository.repo_name))}</td>
16 <td>
16 <td>
17 % if l.action == 'push' and l.revision:
17 % if l.action == 'push' and l.revision:
18 ${h.link_to('%s - %s' % (l.action,l.revision),
18 ${h.link_to('%s - %s' % (l.action,l.revision),
19 h.url('changeset_home',repo_name=l.repository,revision=l.revision))}
19 h.url('changeset_home',repo_name=l.repository.repo_name,revision=l.revision))}
20 %else:
20 %else:
21 ${l.action}
21 ${l.action}
22 %endif
22 %endif
General Comments 0
You need to be logged in to leave comments. Login now