comparison pylons_app/lib/middleware/simplehg.py @ 536:39203995f2c4

made action logger more global, to be used in other places to log other actions. cleaned unused import in simpleHG, fixed little logging in hooks
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 02 Oct 2010 01:52:43 +0200
parents 3d6d548ad3cc
children
comparison
equal deleted inserted replaced
535:72778dda34cf 536:39203995f2c4
22 22
23 @author: marcink 23 @author: marcink
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
31 from mercurial.hgweb.request import wsgiapplication 30 from mercurial.hgweb.request import wsgiapplication
32 from paste.auth.basic import AuthBasicAuthenticator 31 from paste.auth.basic import AuthBasicAuthenticator
33 from paste.httpheaders import REMOTE_USER, AUTH_TYPE 32 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
34 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware, \ 33 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware, \
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__)
47 44
48 class SimpleHg(object): 45 class SimpleHg(object):
181 cmd = qry.split('=')[-1] 178 cmd = qry.split('=')[-1]
182 if mapping.has_key(cmd): 179 if mapping.has_key(cmd):
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
206 invalidate the cache to see the changes right away but only for 187 invalidate the cache to see the changes right away but only for
207 push requests""" 188 push requests"""