admin.py
101 lines
| 3.7 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 | ||||
r44 | from pylons_app.lib import auth | |||
r45 | from pylons_app.model.forms import LoginForm | |||
import formencode | ||||
import formencode.htmlfill as htmlfill | ||||
Marcin Kuzminski
|
r62 | from pylons_app.model import meta | ||
from pylons_app.model.db import Users, UserLogs | ||||
Marcin Kuzminski
|
r78 | from webhelpers.paginate import Page | ||
r140 | from pylons_app.lib.utils import check_repo, invalidate_cache | |||
r43 | log = logging.getLogger(__name__) | |||
class AdminController(BaseController): | ||||
def __before__(self): | ||||
r52 | c.admin_user = session.get('admin_user', False) | |||
r45 | c.admin_username = session.get('admin_username') | |||
r43 | ||||
def index(self): | ||||
# Return a rendered template | ||||
r45 | if request.POST: | |||
#import Login Form validator class | ||||
login_form = LoginForm() | ||||
try: | ||||
c.form_result = login_form.to_python(dict(request.params)) | ||||
Marcin Kuzminski
|
r46 | if auth.admin_auth(c.form_result['username'], c.form_result['password']): | ||
r45 | session['admin_user'] = True | |||
session['admin_username'] = c.form_result['username'] | ||||
session.save() | ||||
return redirect(url('admin_home')) | ||||
else: | ||||
raise formencode.Invalid('Login Error', None, None, | ||||
error_dict={'username':'invalid login', | ||||
'password':'invalid password'}) | ||||
except formencode.Invalid, error: | ||||
c.form_result = error.value | ||||
c.form_errors = error.error_dict or {} | ||||
r127 | html = render('admin/admin.html') | |||
r45 | ||||
return htmlfill.render( | ||||
html, | ||||
defaults=c.form_result, | ||||
encoding="UTF-8" | ||||
) | ||||
Marcin Kuzminski
|
r62 | if c.admin_user: | ||
sa = meta.Session | ||||
Marcin Kuzminski
|
r78 | |||
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) | ||||
r127 | c.log_data = render('admin/admin_log.html') | |||
Marcin Kuzminski
|
r79 | if request.params.get('partial'): | ||
return c.log_data | ||||
r127 | return render('admin/admin.html') | |||
r43 | ||||
def hgrc(self, dirname): | ||||
filename = os.path.join(dirname, '.hg', 'hgrc') | ||||
return filename | ||||
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 | |||
r140 | invalidate_cache('repo_list_2') | |||
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) | ||||