Changeset - d924b931b488
[Not reviewed]
default
0 6 2
marcink - 16 years ago 2010-04-07 16:42:11

Added managment pages.
+ fixed routing bug
done a lot in templates
8 files changed with 178 insertions and 82 deletions:
0 comments (0 inline, 0 general)
pylons_app/config/routing.py
Show inline comments
 
"""Routes configuration
 

	
 
The more specific and detailed routes should be defined first so they
 
may take precedent over the more generic routes. For more information
 
refer to the routes manual at http://routes.groovie.org/docs/
 
"""
 
from routes import Mapper
 

	
 
def make_map(config):
 
    """Create, configure and return the routes Mapper"""
 
    map = Mapper(directory=config['pylons.paths']['controllers'],
 
                 always_scan=config['debug'])
 
    map.minimization = False
 
    map.explicit = False
 

	
 
    # The ErrorController route (handles 404/500 error pages); it should
 
    # likely stay at the top, ensuring it can always be resolved
 
    map.connect('/error/{action}', controller='error')
 
    map.connect('/error/{action}/{id}', controller='error')
 

	
 
    # CUSTOM ROUTES HERE
 
    with map.submapper(path_prefix='/_admin', controller='admin') as m:
 
        m.connect('admin_home', '/', action='index')#main page
 
        m.connect('admin_add_repo', '/add_repo/{new_repo:[a-z0-9\. _-]*}', action='add_repo')
 
        m.connect('admin_manage_users', '/manage_users', action='index')
 
        m.connect('admin_users_manage', '/repos_manage', action='users_manage')
 
        m.connect('admin_repos_manage', '/users_manage', action='repos_manage')
 
        
 
    map.connect('hg', '/{path_info:.*}', controller='hg',
 
                action="view", path_info='/')
 

	
 
    return map
pylons_app/controllers/admin.py
Show inline comments
 
import logging
 

	
 
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
 
import os
 
from mercurial import ui, hg
 
from mercurial.error import RepoError
 
from ConfigParser import ConfigParser
 
from pylons_app.lib import auth
 
log = logging.getLogger(__name__)
 

	
 
class AdminController(BaseController):
 

	
 

	
 
    def __before__(self):
 
        c.staticurl = g.statics
 
        c.admin_user = True
 
        
 
    def index(self):
 
        # Return a rendered template
 
        return render('/admin.html')
 

	
 

	
 
    def repos_manage(self):
 
        return render('/repos_manage.html')
 
    
 
    def users_manage(self):
 
        conn, cur = auth.get_sqlite_conn_cur()
 
        cur.execute('SELECT * FROM users')
 
        c.users_list = cur.fetchall()        
 
        return render('/users_manage.html')
 
                
 
    def manage_hgrc(self):
 
        pass
 

	
 
    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
 
        if new_repo == 'add':
 
            c.msg = 'you basstard ! this repo is a command'
 
        if new_repo == '_admin':
 
            c.msg = 'DENIED'
 
            c.new_repo = ''
 
            return render('add.html')
 

	
 
        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'
 
        except Exception as e:
 
            c.new_repo = 'Exception when adding: %s' % new_repo
 
            c.msg = str(e)
 

	
 
        return render('add.html')
 

	
 
    def _check_repo(self, repo_name):
 
        p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
 
        config_path = os.path.join(p, 'hgwebdir.config')
 

	
 
        cp = ConfigParser()
 

	
 
        cp.read(config_path)
 
        repos_path = cp.get('paths', '/').replace("**", '')
pylons_app/lib/auth.py
Show inline comments
 
import sqlite3
 
import os
 
import logging
 
from os.path import dirname as dn
 
from datetime import datetime
 
import crypt
 

	
 
log = logging.getLogger(__name__)
 
ROOT = dn(dn(dn(os.path.realpath(__file__))))
 

	
 
def get_sqlite_cur_conn():
 
def get_sqlite_conn_cur():
 
    conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite'))
 
    cur = conn.cursor()
 
    return conn, cur
 

	
 
def authfunc(environ, username, password):
 
    conn, cur = get_sqlite_cur_conn()
 
    conn, cur = get_sqlite_conn_cur()
 
    password_crypt = crypt.crypt(password, '6a')
 

	
 
    try:
 
        cur.execute("SELECT * FROM users WHERE username=?", (username,))
 
        data = cur.fetchone()
 
    except sqlite3.OperationalError as e:
 
        data = None
 
        log.error(e)
 

	
 
    if data:
 
        if data[3]:
 
            if data[1] == username and data[2] == password_crypt:
 
                log.info('user %s authenticated correctly', username)
 
                
 
                http_accept = environ.get('HTTP_ACCEPT')
 
        
 
                if http_accept.startswith('application/mercurial') or \
 
                    environ['PATH_INFO'].find('raw-file') != -1:
 
                    cmd = environ['PATH_INFO']
 
                    for qry in environ['QUERY_STRING'].split('&'):
 
                        if qry.startswith('cmd'):
 
                            cmd += "|" + qry
 
                            
 
                            try:
 
                                cur.execute('''INSERT INTO 
 
                                                    user_logs 
 
                                               VALUES(?,?,?,?)''',
 
                                                (None, data[0], cmd, datetime.now()))
 
                                conn.commit()
 
                            except Exception as e:
 
                                conn.rollback()
 
                                log.error(e)
 
                            
 
                                
 
                return True
 
        else:
 
            log.error('user %s is disabled', username)
 
            
 
    return False
 

	
 
def create_user_table():
 
    '''
 
    Create a auth database
 
    '''
 
    conn, cur = get_sqlite_cur_conn()
 
    conn, cur = get_sqlite_conn_cur()
 
    try:
 
        log.info('creating table %s', 'users')
 
        cur.execute('''DROP TABLE IF EXISTS users ''')
 
        cur.execute('''CREATE TABLE users
 
                        (id INTEGER PRIMARY KEY AUTOINCREMENT, 
 
                         username TEXT, 
 
                         password TEXT,
 
                         active INTEGER)''')
 
        log.info('creating table %s', 'user_logs')
 
        cur.execute('''DROP TABLE IF EXISTS user_logs ''')
 
        cur.execute('''CREATE TABLE user_logs
 
                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
 
                            user_id INTEGER,
 
                            last_action TEXT, 
 
                            last_action_date DATETIME)''')
 
        conn.commit()
 
    except:
 
        conn.rollback()
 
        raise
 
    
 
    cur.close()
 
    
 
def create_user(username, password):
 
    conn, cur = get_sqlite_cur_conn()    
 
    conn, cur = get_sqlite_conn_cur()    
 
    password_crypt = crypt.crypt(password, '6a')
 
    cur_date = datetime.now()
 
    log.info('creating user %s', username)
 
    try:
 
        cur.execute('''INSERT INTO users values (?,?,?,?) ''',
 
                    (None, username, password_crypt, 1,))     
 
        conn.commit()
 
    except:
 
        conn.rollback()
 
        raise
 
    
 
if __name__ == "__main__":
 
    create_user_table()
 
    create_user('marcink', 'qweqwe')
 
    create_user('lukaszd', 'qweqwe')
 
    create_user('adriand', 'qweqwe')
 
    create_user('radek', 'qweqwe')
 
    create_user('skrzeka', 'qweqwe')
 
    create_user('bart', 'qweqwe')
 
    create_user('maho', 'qweqwe')
 
    create_user('michalg', 'qweqwe')
 
    create_user('admin', 'qwe123qwe')
 
    
 
    #authfunc('', 'marcink', 'qweqwe')
pylons_app/templates/admin.html
Show inline comments
 
## -*- coding: utf-8 -*-
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
    <link rel="icon" href="${c.staticurl}hgicon.png" type="image/png" />
 
    <meta name="robots" content="index, nofollow"/>
 
    <link rel="stylesheet" href="${c.staticurl}style-monoblue.css" type="text/css" />
 
       <title>Mercurial repositories Admin</title>
 
</head>
 

	
 
<body>
 
<div id="container">
 
    <div class="page-header">
 
        <h1><a href="/">Home</a> / Admin</h1>
 
        <ul class="page-nav">
 
        </ul>
 
<%inherit file="base/base.html"/>
 
<%def name="title()">
 
    ${_('Repository managment')}
 
</%def>
 
<%def name="breadcrumbs()">
 
	${h.link_to(u'Home',h.url('/'))}
 
	 / 
 
	${h.link_to(u'Admin',h.url('admin_home'))}
 
</%def>
 
<%def name="page_nav()">
 
<li>${h.link_to(u'Home',h.url('/'))}</li>
 
<li class="current">${_('Admin')}</li>
 
</%def>
 
<%def name="main()">
 
    %if c.admin_user:
 
    <ul class="submenu">
 
        <li>
 
            ${h.link_to(u'Repos managment',h.url('admin_repos_manage'))}
 
        </li>
 
        <li>
 
            ${h.link_to(u'Users managment',h.url('admin_users_manage'))}
 
        </li>
 
    </ul>
 
    <br/>
 
    <div>
 
    
 
        <h2>Hi !</h2>
 
    </div>
 
    <table cellspacing="0">
 
        <tr>
 
            <td>${h.link_to(u'Create "ccc" repository',h.url('admin_add_repo',new_repo='ccc'))}</td>
 
        </tr>
 
        <tr>
 
            <td>${h.link_to(u'Create "ccc" repository',h.url('admin_add_repo',new_repo='ccc'))}</td>
 
        </tr>
 
        <tr>
 
            <td>${h.link_to(u'Create "ccc" repository',h.url('admin_add_repo',new_repo='ccc'))}</td>
 
        </tr>
 
        <tr>
 
            <td><h2>${c.new_repo}</h2></td>
 
        </tr>
 
    </table>    
 
    <div class="page-footer">
 
        Mercurial Repository: admin
 
    </div>
 

	
 
    <div id="powered-by">
 
        <p>
 
        <a href="http://mercurial.selenic.com/" title="Mercurial">
 
            <img src="${c.staticurl}hglogo.png" width="75" height="90" alt="mercurial"></a>
 
        </p>
 
    </div>
 

	
 
    <div id="corner-top-left"></div>
 
    <div id="corner-top-right"></div>
 
    <div id="corner-bottom-left"></div>
 
    <div id="corner-bottom-right"></div>
 

	
 
</div>
 
</body>
 
</html>
 
        
 
\ No newline at end of file
 
    %else:
 
        <div>
 
        <br />
 
        <h2>${_('Login')}</h2>
 
        ${h.form(h.url.current())}
 
        <table>
 
            <tr>
 
                <td>${_('Username')}</td>
 
                <td>${h.text('username')}</td>
 
            </tr>
 
            <tr>
 
                <td>${_('Password')}</td>
 
                <td>${h.text('password')}</td>
 
            </tr>
 
            <tr>
 
                <td></td>
 
                <td>${h.submit('login','login')}</td>
 
            </tr>            
 
        </table>
 
        ${h.end_form()}
 
        </div>
 
    %endif
 
    
 
</%def>
 
\ No newline at end of file
pylons_app/templates/base/base.html
Show inline comments
 
## -*- coding: utf-8 -*-
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
  <style type="text/css">
 
  </style>
 
  <title>${next.page_title()}</title>
 
  <meta name="author" content=""/>
 
  <meta name="keywords" content=""/>
 
  <meta name="description" content=""/>
 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 
  <link rel="shortcut icon" type="image/x-icon" href="/images/sample.ico"/>
 
  <script src="/css_browser_selector.js" type="text/javascript"></script>
 
  <script type="text/javascript" src="/js/yui/utilities/utilities.js"></script>
 
  <link rel="stylesheet" type="text/css" href="/css/style.css"/>     
 

	
 
    <link rel="icon" href="${c.staticurl}hgicon.png" type="image/png" />
 
    <meta name="robots" content="index, nofollow"/>
 
    <link rel="stylesheet" href="${c.staticurl}style-monoblue.css" type="text/css" />
 
       <title>${next.title()}</title>
 
</head>
 
<body>
 

	
 
  <div id="top-glow"></div>
 

	
 
  <div id="container">
 
    ${next.body()}
 
<body>
 
<div id="container">
 
    <div class="page-header">
 
        <h1>
 
            ${next.breadcrumbs()}
 
        </h1>
 
        <ul class="page-nav">
 
            ${next.page_nav()}
 
        </ul>
 
    </div>
 
    ${next.main()}
 
    <div class="page-footer">
 
        Mercurial Repository: ${c.repo_name}
 
    </div>   
 

	
 
  <div id="footer" class="clearfix">
 
    ${next.footer()}
 
  </div><!-- /footer -->
 
    <div id="powered-by">
 
        <p>
 
        <a href="http://mercurial.selenic.com/" title="Mercurial">
 
            <img src="${c.staticurl}hglogo.png" width="75" height="90" alt="mercurial"></a>
 
        </p>
 
    </div>
 

	
 
  </div><!-- /container -->
 
    <div id="corner-top-left"></div>
 
    <div id="corner-top-right"></div>
 
    <div id="corner-bottom-left"></div>
 
    <div id="corner-bottom-right"></div>
 

	
 
  <div id="bottom-glow"></div>
 

	
 
 </body>
 
 </html>
 
\ No newline at end of file
 
</div>
 
</body>
 
</html>
 
\ No newline at end of file
pylons_app/templates/monoblue_custom/index.tmpl
Show inline comments
 
## -*- coding: utf-8 -*-
 
{header}
 
    <title>{repo|escape}: Mercurial repositories index</title>
 
</head>
 

	
 
<body>
 
<div id="container">
 
    <div class="page-header">
 
        <h1>${c.repos_prefix} Mercurial Repositories</h1>
 
        <ul class="page-nav">
 
            <li class="current">Home</li>
 
            <li>${h.link_to(u'Admin',h.url('admin_home'))}</li>
 
        </ul>
 
    </div>
 
    
 
    <table cellspacing="0">
 
        <tr>
 
            <td><a href="?sort={sort_name}">Name</a></td>
 
            <td><a href="?sort={sort_description}">Description</a></td>
 
            <td><a href="?sort={sort_contact}">Contact</a></td>
 
            <td><a href="?sort={sort_lastchange}">Last change</a></td>
 
            <td>&nbsp;</td>
 
            <td>&nbsp;</td>
 
        </tr>
 
        {entries%indexentry}
 
    </table>
 
    <div class="page-footer">
 
        {motd}
 
    </div>
 

	
 
    <div id="powered-by">
 
        <p><a href="http://mercurial.selenic.com/" title="Mercurial"><img src="{staticurl}hglogo.png" width="75" height="90" alt="mercurial"/></a></p>
 
    </div>
 

	
 
    <div id="corner-top-left"></div>
 
    <div id="corner-top-right"></div>
pylons_app/templates/repos_manage.html
Show inline comments
 
new file 100644
 
<%inherit file="base/base.html"/>
 
<%def name="title()">
 
    ${_('Repository managment')}
 
</%def>
 
<%def name="breadcrumbs()">
 
    ${h.link_to(u'Home',h.url('/'))}
 
    / 
 
    ${h.link_to(u'Admin',h.url('admin_home'))}
 
    /
 
    ${h.link_to(u'Repos managment',h.url('admin_repos_manage'))}
 
</%def>
 
<%def name="page_nav()">
 
	<li>${h.link_to(u'Home',h.url('/'))}</li>
 
	<li class="current">${_('Admin')}</li>
 
</%def>
 
<%def name="main()">
 

	
 

	
 
<div class="twocol-form">
 
        <h2>Create new repository</h2>
 
        <form method="post" action="/repo/create/">
 
            <table>
 
                <tbody><tr><th><label for="id_name">Name:</label></th><td><input type="text" maxlength="255" name="name" id="id_name"></td></tr>
 
<tr><th><label for="id_description">Description:</label></th><td><textarea name="description" cols="40" rows="10" id="id_description"></textarea></td></tr>
 
<tr><th><label for="id_website">Website:</label></th><td><input type="text" maxlength="128" name="website" id="id_website"></td></tr>
 
<tr><th><label for="id_is_private">Private:</label></th><td><input type="checkbox" id="id_is_private" name="is_private"></td></tr>
 
<tr><th><label for="id_has_issues">Issue tracking:</label></th><td><input type="checkbox" id="id_has_issues" name="has_issues" checked="checked"></td></tr>
 
<tr><th><label for="id_has_wiki">Wiki:</label></th><td><input type="checkbox" id="id_has_wiki" name="has_wiki" checked="checked"></td></tr>
 
                
 
                
 
                <tr><td colspan="2">&nbsp;</td></tr>
 
                <tr>
 
                    <td colspan="2">
 
                        <input type="submit" class="primary-button" value="Create repository"> <input type="reset" onclick="document.location='http://bitbucket.org/';" class="secondary-button secondary-button-darkbg" value="Cancel">
 
                    </td>
 
                </tr>
 
            </tbody></table>
 
        </form>
 
    </div>
 
</%def>    
 
\ No newline at end of file
pylons_app/templates/users_manage.html
Show inline comments
 
new file 100644
 
<%inherit file="base/base.html"/>
 
<%def name="title()">
 
    ${_('Repository managment')}
 
</%def>
 
<%def name="breadcrumbs()">
 
    ${h.link_to(u'Home',h.url('/'))}
 
    / 
 
    ${h.link_to(u'Admin',h.url('admin_home'))}
 
    /
 
    ${h.link_to(u'Users managment',h.url('admin_users_manage'))}
 
</%def>
 
<%def name="page_nav()">
 
    <li>${h.link_to(u'Home',h.url('/'))}</li>
 
    <li class="current">${_('Admin')}</li>
 
</%def>
 
<%def name="main()">
 

	
 
        <table cellspacing="0">
 
         <tr>
 
            <th>Id</th>
 
            <th>Username</th>
 
            <th>Password</th>
 
            <th>Active</th>
 
         </tr>    
 
            %for i in c.users_list:
 
                <tr>
 
                    <td>${i[0]}</td>
 
                    <td>${i[1]}</td>
 
                    <td>${i[2]}</td>
 
                    <td>${i[3]}</td>
 
                </tr>
 
            %endfor
 
        </table>
 
</%def>    
 
\ No newline at end of file
0 comments (0 inline, 0 general)