admin.py
67 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
r43 | import logging | |||
r133 | import os | |||
r43 | ||||
from pylons import request, response, session, tmpl_context as c, url, app_globals as g | ||||
from pylons.controllers.util import abort, redirect | ||||
from pylons_app.lib.base import BaseController, render | ||||
Marcin Kuzminski
|
r62 | from pylons_app.model import meta | ||
r191 | from pylons_app.model.db import UserLogs | |||
Marcin Kuzminski
|
r78 | from webhelpers.paginate import Page | ||
r140 | from pylons_app.lib.utils import check_repo, invalidate_cache | |||
r191 | from pylons_app.lib.auth import LoginRequired | |||
r140 | ||||
r43 | log = logging.getLogger(__name__) | |||
class AdminController(BaseController): | ||||
r191 | ||||
@LoginRequired() | ||||
r43 | def __before__(self): | |||
r191 | user = session['hg_app_user'] | |||
c.admin_user = user.is_admin | ||||
c.admin_username = user.username | ||||
super(AdminController, self).__before__() | ||||
r43 | ||||
def index(self): | ||||
r191 | sa = meta.Session | |||
users_log = sa.query(UserLogs)\ | ||||
.order_by(UserLogs.action_date.desc()) | ||||
p = int(request.params.get('page', 1)) | ||||
c.users_log = Page(users_log, page=p, items_per_page=10) | ||||
c.log_data = render('admin/admin_log.html') | ||||
if request.params.get('partial'): | ||||
return c.log_data | ||||
r127 | return render('admin/admin.html') | |||
r43 | ||||
def add_repo(self, new_repo): | ||||
#extra check it can be add since it's the command | ||||
r44 | if new_repo == '_admin': | |||
c.msg = 'DENIED' | ||||
r43 | c.new_repo = '' | |||
r133 | return render('admin/add.html') | |||
r43 | ||||
new_repo = new_repo.replace(" ", "_") | ||||
new_repo = new_repo.replace("-", "_") | ||||
try: | ||||
self._create_repo(new_repo) | ||||
c.new_repo = new_repo | ||||
c.msg = 'added repo' | ||||
r133 | #clear our cached list for refresh with new repo | |||
r171 | invalidate_cache('cached_repo_list') | |||
r43 | except Exception as e: | |||
c.new_repo = 'Exception when adding: %s' % new_repo | ||||
c.msg = str(e) | ||||
r133 | return render('admin/add.html') | |||
r43 | ||||
def _create_repo(self, repo_name): | ||||
if repo_name in [None, '', 'add']: | ||||
raise Exception('undefined repo_name of repo') | ||||
r133 | repo_path = os.path.join(g.base_path, repo_name) | |||
r125 | if check_repo(repo_name, g.base_path): | |||
r133 | log.info('creating repo %s in %s', repo_name, repo_path) | |||
from vcs.backends.hg import MercurialRepository | ||||
MercurialRepository(repo_path, create=True) | ||||