changeset 537:48be953851fc

extended user logs to create/delete/fork repositories for auditing some spelling corrections
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 02 Oct 2010 02:56:39 +0200
parents 39203995f2c4
children 16ba57d8fe23
files pylons_app/controllers/admin/repos.py pylons_app/controllers/admin/settings.py pylons_app/controllers/admin/users.py pylons_app/controllers/settings.py pylons_app/lib/base.py pylons_app/lib/utils.py pylons_app/model/db.py pylons_app/templates/admin/admin_log.html
diffstat 8 files changed, 48 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/pylons_app/controllers/admin/repos.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/controllers/admin/repos.py	Sat Oct 02 02:56:39 2010 +0200
@@ -32,7 +32,7 @@
 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator, \
     HasPermissionAnyDecorator
 from pylons_app.lib.base import BaseController, render
-from pylons_app.lib.utils import invalidate_cache
+from pylons_app.lib.utils import invalidate_cache, action_logger
 from pylons_app.model.db import User
 from pylons_app.model.forms import RepoForm
 from pylons_app.model.hg_model import HgModel
@@ -77,13 +77,20 @@
             invalidate_cache('cached_repo_list')
             h.flash(_('created repository %s') % form_result['repo_name'],
                     category='success')
-                                                             
+
+            if request.POST.get('user_created'):
+                action_logger(self.hg_app_user, 'user_created_repo', 
+                              form_result['repo_name'], '', self.sa)
+            else:
+                action_logger(self.hg_app_user, 'admin_created_repo', 
+                              form_result['repo_name'], '', self.sa)                
+                                                                             
         except formencode.Invalid as errors:
             c.new_repo = errors.value['repo_name']
             
             if request.POST.get('user_created'):
                 r = render('admin/repos/repo_add_create_repository.html')
-            else:
+            else:              
                 r = render('admin/repos/repo_add.html')
             
             return htmlfill.render(
@@ -169,10 +176,14 @@
         
             return redirect(url('repos'))
         try:
+            action_logger(self.hg_app_user, 'admin_deleted_repo', 
+                              repo_name, '', self.sa)
             repo_model.delete(repo)            
             invalidate_cache('cached_repo_list')
             h.flash(_('deleted repository %s') % repo_name, category='success')
-        except Exception:
+           
+        except Exception, e:
+            log.error(traceback.format_exc())
             h.flash(_('An error occured during deletion of %s') % repo_name,
                     category='error')
         
--- a/pylons_app/controllers/admin/settings.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/controllers/admin/settings.py	Sat Oct 02 02:56:39 2010 +0200
@@ -101,7 +101,7 @@
             initial = HgModel.repo_scan(g.paths[0][0], g.paths[0][1], g.baseui)
             repo2db_mapper(initial, rm_obsolete)
             invalidate_cache('cached_repo_list')
-            h.flash(_('Repositories sucessfully rescanned'), category='success')            
+            h.flash(_('Repositories successfully rescanned'), category='success')            
         
         if setting_id == 'whoosh':
             repo_location = get_hg_ui_settings()['paths_root_path']
@@ -134,7 +134,7 @@
                                     
                 except:
                     log.error(traceback.format_exc())
-                    h.flash(_('error occured during updating application settings'),
+                    h.flash(_('error occurred during updating application settings'),
                             category='error')
                                 
                     self.sa.rollback()
@@ -187,7 +187,7 @@
                                     
                 except:
                     log.error(traceback.format_exc())
-                    h.flash(_('error occured during updating application settings'),
+                    h.flash(_('error occurred during updating application settings'),
                             category='error')
                                 
                     self.sa.rollback()
--- a/pylons_app/controllers/admin/users.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/controllers/admin/users.py	Sat Oct 02 02:56:39 2010 +0200
@@ -17,6 +17,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
+from pylons_app.lib.utils import action_logger
 """
 Created on April 4, 2010
 users controller for pylons
@@ -71,6 +72,7 @@
             user_model.create(form_result)
             h.flash(_('created user %s') % form_result['username'],
                     category='success')
+            #action_logger(self.hg_app_user, 'new_user', '', '', self.sa)
         except formencode.Invalid as errors:
             return htmlfill.render(
                 render('admin/users/user_add.html'),
--- a/pylons_app/controllers/settings.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/controllers/settings.py	Sat Oct 02 02:56:39 2010 +0200
@@ -28,7 +28,7 @@
 from pylons.i18n.translation import _
 from pylons_app.lib.auth import LoginRequired, HasRepoPermissionAllDecorator
 from pylons_app.lib.base import BaseController, render
-from pylons_app.lib.utils import invalidate_cache
+from pylons_app.lib.utils import invalidate_cache, action_logger
 from pylons_app.model.forms import RepoSettingsForm, RepoForkForm
 from pylons_app.model.repo_model import RepoModel
 import formencode
@@ -79,7 +79,7 @@
             form_result = _form.to_python(dict(request.POST))
             repo_model.update(repo_name, form_result)
             invalidate_cache('cached_repo_list')
-            h.flash(_('Repository %s updated succesfully' % repo_name),
+            h.flash(_('Repository %s updated successfully' % repo_name),
                     category='success')
             changed_name = form_result['repo_name']               
         except formencode.Invalid as errors:
@@ -121,11 +121,13 @@
         
             return redirect(url('hg_home'))
         try:
+            action_logger(self.hg_app_user, 'user_deleted_repo', 
+                              repo_name, '', self.sa)            
             repo_model.delete(repo)            
             invalidate_cache('cached_repo_list')
             h.flash(_('deleted repository %s') % repo_name, category='success')
         except Exception:
-            h.flash(_('An error occured during deletion of %s') % repo_name,
+            h.flash(_('An error occurred during deletion of %s') % repo_name,
                     category='error')
         
         return redirect(url('hg_home'))
@@ -158,7 +160,8 @@
             h.flash(_('fork %s repository as %s task added') \
                       % (repo_name, form_result['fork_name']),
                     category='success')
-                                                             
+            action_logger(self.hg_app_user, 'user_forked_repo',
+                            repo_name, '', self.sa)                                                 
         except formencode.Invalid as errors:
             c.new_repo = errors.value['fork_name']
             r = render('settings/repo_fork.html')
--- a/pylons_app/lib/base.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/lib/base.py	Sat Oct 02 02:56:39 2010 +0200
@@ -40,7 +40,7 @@
         # available in environ['pylons.routes_dict']
         try:
             #putting this here makes sure that we update permissions every time
-            c.hg_app_user = auth.get_user(session)
+            self.hg_app_user = c.hg_app_user = auth.get_user(session)
             return WSGIController.__call__(self, environ, start_response)
         finally:
             meta.Session.remove()
--- a/pylons_app/lib/utils.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/lib/utils.py	Sat Oct 02 02:56:39 2010 +0200
@@ -26,7 +26,7 @@
 from mercurial import ui, config, hg
 from mercurial.error import RepoError
 from pylons_app.model import meta
-from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings,UserLog
+from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings, UserLog
 from vcs.backends.base import BaseChangeset
 from vcs.utils.lazy import LazyProperty
 import logging
@@ -57,21 +57,21 @@
     if not sa:
         sa = meta.Session 
         
-    
-    if hasattr(user, 'user_id'):
-        user_id = user.user_id
-    elif isinstance(user, basestring):
-        
-        user_id = sa.Query(User).filter(User.username == user).one()
-    else:
-        raise Exception('You have to provide user object or username')
-   
     try:
+        if hasattr(user, 'user_id'):
+            user_id = user.user_id
+        elif isinstance(user, basestring):
+            user_id = sa.query(User).filter(User.username == user).one()
+        else:
+            raise Exception('You have to provide user object or username')
+       
+        repo_name = repo.lstrip('/')
         user_log = UserLog()
         user_log.user_id = user_id
         user_log.action = action
+        user_log.repository_name = repo_name
         user_log.repository = sa.query(Repository)\
-            .filter(Repository.repo_name==repo.lstrip('/')).one()
+            .filter(Repository.repo_name == repo_name).one()
         user_log.action_date = datetime.datetime.now()
         user_log.user_ip = ipaddr
         sa.add(user_log)
--- a/pylons_app/model/db.py	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/model/db.py	Sat Oct 02 02:56:39 2010 +0200
@@ -66,7 +66,8 @@
     __table_args__ = {'useexisting':True}
     user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
-    repository_id = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
+    repository_id = Column("repository_id", INTEGER(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
+    repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) 
     action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
--- a/pylons_app/templates/admin/admin_log.html	Sat Oct 02 01:52:43 2010 +0200
+++ b/pylons_app/templates/admin/admin_log.html	Sat Oct 02 02:56:39 2010 +0200
@@ -12,7 +12,13 @@
 	%for cnt,l in enumerate(c.users_log):
 	<tr class="parity${cnt%2}">
 		<td>${h.link_to(l.user.username,h.url('edit_user', id=l.user.user_id))}</td>
-		<td>${h.link_to(l.repository.repo_name,h.url('summary_home',repo_name=l.repository.repo_name))}</td>
+		<td>
+		%if l.repository:
+		  ${h.link_to(l.repository.repo_name,h.url('summary_home',repo_name=l.repository.repo_name))}
+		%else:
+		  ${l.repository_name}
+		%endif
+		</td>
 		<td>
 		% if l.action == 'push' and l.revision:
 		  ${h.link_to('%s - %s' % (l.action,l.revision),