Mercurial > kallithea
comparison pylons_app/lib/utils.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 | 63212fea2471 |
children | 48be953851fc |
comparison
equal
deleted
inserted
replaced
535:72778dda34cf | 536:39203995f2c4 |
---|---|
24 """ | 24 """ |
25 from beaker.cache import cache_region | 25 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): |
42 """ | 43 """ |
43 Returns True if request's target is mercurial server - header | 44 Returns True if request's target is mercurial server - header |
46 http_accept = environ.get('HTTP_ACCEPT') | 47 http_accept = environ.get('HTTP_ACCEPT') |
47 if http_accept and http_accept.startswith('application/mercurial'): | 48 if http_accept and http_accept.startswith('application/mercurial'): |
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 ['*', '**']: |
54 repos_path = repos_path[:-1] | 89 repos_path = repos_path[:-1] |
55 if repos_path[0] != '/': | 90 if repos_path[0] != '/': |