Changeset - 928416088790
[Not reviewed]
default
0 7 0
Marcin Kuzminski - 16 years ago 2010-04-17 22:17:17
marcin@python-blog.com
reimplemented summary page,
added few filters, removed age from models and made it as filter.
7 files changed with 33 insertions and 23 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
 
    map.connect('hg_home', '/', controller='hg', action='index')
 
    
 
    map.resource('repo', 'repos', path_prefix='/_admin')
 
    map.resource('user', 'users', path_prefix='/_admin')
 
    
 
    
 
    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')
 
    
 
    
 
    map.connect('summary_home', '/{repo_name}/_summary', controller='hg', action='view')    
 
    map.connect('summary_home', '/{repo_name}/_summary', controller='hg', action='view')
 
    
 
    map.connect('hg', '/{path_info:.*}', controller='hg',
 
                action="view", path_info='/')
 

	
 
    return map
pylons_app/controllers/hg.py
Show inline comments
 
#!/usr/bin/python
 
# -*- coding: utf-8 -*-
 
import logging
 
from pylons import tmpl_context as c, app_globals as g, session, request, config
 
from pylons_app.lib import helpers as h
 
from pylons_app.lib.base import BaseController, render
 
from mako.template import Template
 
from pylons.controllers.util import abort
 

	
 
from operator import itemgetter
 
from pylons_app.model.hg_model import HgModel
 
log = logging.getLogger(__name__)
 

	
 
class HgController(BaseController):
 

	
 
    def __before__(self):
 
        c.repos_prefix = config['repos_name']
 
        c.staticurl = g.statics
 

	
 
    def index(self):
 
        hg_model = HgModel()
 
        c.repos_list = list(hg_model.get_repos())
 
        c.current_sort = request.GET.get('sort', 'name')
 
        
 
        cs = c.current_sort
 
        c.cs_slug = cs.replace('-', '')
 
        sortables = ['name', 'description', 'last_change', 'tip', 'contact']
 
        
 
        if cs and c.cs_slug in sortables:
 
            sort_key = c.cs_slug + '_sort'
 
            if cs.startswith('-'):
 
                c.repos_list.sort(key=itemgetter(sort_key), reverse=True)
 
            else:
 
                c.repos_list.sort(key=itemgetter(sort_key), reverse=False)
 
            
 
        return render('/index.html')
 

	
 
    def view(self, *args, **kwargs):
 
        #TODO: reimplement this not tu use hgwebdir
 
        
 
        #patch for replacing mercurial servings with hg_app servings
 
        vcs_impl = self._get_vcs_impl(request.environ) 
 
        if vcs_impl:
 
            return vcs_impl
 
        
 
        
 
        response = g.hgapp(request.environ, self.start_response)
 
        
 
        http_accept = request.environ.get('HTTP_ACCEPT', False)
 
        if not http_accept:
 
            return abort(status_code=400, detail='no http accept in header')
 
        
 
        #for mercurial protocols and raw files we can't wrap into mako
 
        if http_accept.find("mercurial") != -1 or \
 
        request.environ['PATH_INFO'].find('raw-file') != -1:
 
                    return response
 
        try:
 
            tmpl = u''.join(response)
 
            template = Template(tmpl, lookup=request.environ['pylons.pylons']\
 
                            .config['pylons.app_globals'].mako_lookup)
 
                        
 
        except (RuntimeError, UnicodeDecodeError):
 
            log.info('disabling unicode due to encoding error')
 
            response = g.hgapp(request.environ, self.start_response)
 
            tmpl = ''.join(response)
 
            template = Template(tmpl, lookup=request.environ['pylons.pylons']\
 
                            .config['pylons.app_globals'].mako_lookup, disable_unicode=True)
 

	
 

	
 
        return template.render(g=g, c=c, session=session, h=h)
 
    
 
    
 
    
 
    
 
    def _get_vcs_impl(self, environ):
 
        path_info = environ['PATH_INFO']
 
        c.repo_name = path_info.split('/')[-2]
 
        action = path_info.split('/')[-1]
 
        if not action.startswith('_'):
 
            return False
 
        else:
 
            hg_model = HgModel()
 
            c.repo_info = hg_model.get_repo(c.repo_name)
 
            c.repo_changesets = c.repo_info.get_changesets(10)
 
#            c.repo_tags = c.repo_info.get_tags(limit=10)
 
#            c.repo_branches = c.repo_info.get_branches(limit=10)
 
            return render('/summary.html')
 

	
pylons_app/lib/app_globals.py
Show inline comments
 
"""The application's Globals object"""
 
#uncomment the following if you want to serve a single repo
 
#from mercurial.hgweb.hgweb_mod import hgweb
 
from mercurial.hgweb.hgwebdir_mod import hgwebdir
 
from mercurial import templater
 
from mercurial.hgweb.request import wsgiapplication
 
from mercurial import ui, config
 
import os
 
from beaker.cache import CacheManager
 
from beaker.util import parse_cache_config_options
 

	
 
class Globals(object):
 

	
 
    """Globals acts as a container for objects available throughout the
 
    life of the application
 

	
 
    """
 

	
 
    def __init__(self, config):
 
        """One instance of Globals is created during application
 
        initialization and is available during requests via the
 
        'app_globals' variable
 

	
 
        """
 
        #two ways of building the merc app i don't know 
 
        #the fastest one but belive the wsgiapp is better
 
        #self.hgapp = self.make_web_app()
 
        self.cache = CacheManager(**parse_cache_config_options(config))
 
        self.hgapp = wsgiapplication(self.make_web_app)
 

	
 
    def make_web_app(self):
 
        repos = "hgwebdir.config"
 
        baseui = ui.ui()
 
        cfg = config.config()
 
        cfg.read(repos)
 
        paths = cfg.items('paths')
 
        self.paths = paths
 
        self.check_repo_dir(paths)
 
        
 
        self.set_statics(cfg)
 

	
 
        for k, v in cfg.items('web'):
 
            baseui.setconfig('web', k, v)
 
        #magic trick to make our custom template dir working
 
        templater.path.append(cfg.get('web', 'templates', None))
 
        self.baseui = baseui
 
        #baseui.setconfig('web', 'description', '')
 
        #baseui.setconfig('web', 'name', '')
 
        #baseui.setconfig('web', 'contact', '')
 
        #baseui.setconfig('web', 'allow_archive', '')
 
        #baseui.setconfig('web', 'style', 'monoblue_plain')
 
        #baseui.setconfig('web', 'baseurl', '')
 
        #baseui.setconfig('web', 'staticurl', '')
 
        
 
        hgwebapp = hgwebdir(paths, baseui=baseui)
 
        return hgwebapp
 

	
 

	
 
    def set_statics(self, cfg):
 
        '''
 
        set's the statics for use in mako templates
 
        @param cfg:
 
        '''
 
        self.statics = cfg.get('web', 'staticurl', '/static')
 
        if not self.statics.endswith('/'):
 
            self.statics += '/'
 

	
 

	
 
    def check_repo_dir(self, paths):
 
        repos_path = paths[0][1].split('/')
 
        if repos_path[-1] in ['*', '**']:
 
            repos_path = repos_path[:-1]
 
        if repos_path[0] != '/':
 
            repos_path[0] = '/'
 
        if not os.path.isdir(os.path.join(*repos_path)):
pylons_app/lib/filters.py
Show inline comments
 
from mercurial import util
 
from mercurial.templatefilters import age as _age
 

	
 
age = lambda context, x:_age(x)
 
capitalize = lambda x: x.capitalize()
 
date = lambda x: util.datestr(x)
 
email = util.email
 
hgdate = lambda x: "%d %d" % x
 
isodate = lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2')
 
isodatesec = lambda x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2')
 
localdate = lambda x: (x[0], util.makedate()[1])
 
hgdate = lambda context, x: "%d %d" % x
 
isodate = lambda context, x: util.datestr(x, '%Y-%m-%d %H:%M %1%2')
 
isodatesec = lambda context, x: util.datestr(x, '%Y-%m-%d %H:%M:%S %1%2')
 
localdate = lambda context, x: (x[0], util.makedate()[1])
 
rfc822date = lambda context, x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2")
 
rfc3339date = lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2")
 
rfc3339date = lambda context, x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2")
 
time_ago = lambda context, x: util.datestr(_age(x), "%a, %d %b %Y %H:%M:%S %1%2")
pylons_app/model/hg_model.py
Show inline comments
 
#!/usr/bin/env python
 
# encoding: utf-8
 
#
 
# Copyright (c) 2010 marcink.  All rights reserved.
 
#
 
'''
 
Created on Apr 9, 2010
 

	
 
@author: marcink
 
'''
 
import os
 
from pylons import tmpl_context as c, app_globals as g, session, request, config
 
from pylons.controllers.util import abort
 
try:
 
    from vcs.backends.hg import get_repositories, MercurialRepository
 
except ImportError:
 
    print 'You have to import vcs module'
 
from mercurial.templatefilters import age
 

	
 
class HgModel(object):
 
    """
 
    Mercurial Model
 
    """
 

	
 

	
 
    def __init__(self):
 
        """
 
        Constructor
 
        """
 
        pass
 

	
 
    def get_repos(self):
 
        for mercurial_repo in get_repositories(g.paths[0][0], g.paths[0][1], g.baseui):
 
            
 
            if mercurial_repo._get_hidden():
 
                #skip hidden web repository
 
                continue
 
            
 
            last_change = mercurial_repo.last_change
 
            tip = mercurial_repo.repo.changectx('tip')
 
            tmp_d = {}
 
            tmp_d['name'] = mercurial_repo.name
 
            tmp_d['name_sort'] = tmp_d['name']
 
            tmp_d['description'] = mercurial_repo.description
 
            tmp_d['description_sort'] = tmp_d['description']
 
            tmp_d['last_change'] = age(last_change)
 
            tmp_d['last_change'] = last_change
 
            tmp_d['last_change_sort'] = last_change[1] - last_change[0]
 
            tmp_d['tip'] = str(tip)
 
            tmp_d['tip_sort'] = tip.rev()
 
            tmp_d['rev'] = tip.rev()
 
            tmp_d['contact'] = mercurial_repo.contact
 
            tmp_d['contact_sort'] = tmp_d['contact']
 
            tmp_d['repo_archives'] = mercurial_repo._get_archive_list()
 
            
 
            yield tmp_d
 

	
 
    def get_repo(self, repo_name):
 
        path = g.paths[0][1]
 
        repo = MercurialRepository(os.path.join(path, repo_name), g.baseui)
 
        path = g.paths[0][1].replace('*', '')
 
        repo = MercurialRepository(os.path.join(path, repo_name), baseui=g.baseui)
 
        return repo
pylons_app/templates/index.html
Show inline comments
 
## -*- coding: utf-8 -*-
 

	
 
<%inherit file="base/base.html"/>
 
<%def name="title()">
 
    ${c.repos_prefix} Mercurial Repositories
 
</%def>
 
<%def name="breadcrumbs()">
 
	${c.repos_prefix} Mercurial Repositories
 
</%def>
 
<%def name="page_nav()">
 
	<li class="current">${_('Home')}</li>
 
	<li>${h.link_to(u'Admin',h.url('admin_home'))}</li>
 
</%def>
 
<%def name="main()">
 
	<%def name="get_sort(name)">
 
		<%name_slug = name.lower().replace(' ','_') %>
 
		%if name_slug == c.cs_slug:
 
			<span style="font-weight: bold;color:#006699">${name}</span>
 
		%else:
 
			<span style="font-weight: bold">${name}</span>
 
		%endif
 
		
 
		<a href="?sort=${name_slug}">&darr;</a>
 
		<a href="?sort=-${name_slug}">&uarr;</a>
 
		
 
	</%def>
 
	<table>
 
	  <tr>
 
	    <td>${get_sort(_('Name'))}</td>
 
	    <td>${get_sort(_('Description'))}</td>
 
	    <td>${get_sort(_('Last change'))}</td>
 
	    <td>${get_sort(_('Tip'))}</td>
 
	    <td>${get_sort(_('Contact'))}</td>
 
	    <td></td>
 
	    <td></td>
 
	  </tr>	
 
	%for cnt,repo in enumerate(c.repos_list):
 
 		<tr class="parity${cnt%2}">
 
		    <td><a href="/${repo['name']}">${repo['name']}</a></td>
 
		    <td>${h.link(repo['name'],h.url('summary_home',repo_name=repo['name']))}</td>
 
		    <td>${repo['description']}</td>
 
	        <td>${repo['last_change']}</td>
 
	        <td>${repo['last_change']|n,self.f.age}</td>
 
	        <td>r${repo['rev']}:<a href="/${repo['name']}/rev/${repo['tip']}/">${repo['tip']}</a></td>
 
	        <td>${repo['contact']}</td>
 
	        <td class="indexlinks">
 
	        	%for archive in repo['repo_archives']:
 
					<a href="/${repo['name']}/archive/${archive['node']}${archive['extension']}">${archive['type']}</a>
 
				%endfor
 
	        </td>
 
			<td>
 
				<div class="rss_logo">
 
				<a href="/${repo['name']}/rss-log">RSS</a>
 
				<a href="/${repo['name']}/atom-log">Atom</a>
 
				</div>
 
			</td>        
 
		</tr>
 
	%endfor
 
	</table>
 
</%def>    
pylons_app/templates/summary.html
Show inline comments
 
<%inherit file="base/base.html"/>
 

	
 
<%def name="title()">
 
    ${_('Repository managment')}
 
</%def>
 
<%def name="breadcrumbs()">
 
    ${h.link_to(u'Home',h.url('/'))}
 
    / 
 
    ${h.link_to(c.repo_name,h.url('summary_home',repo_name=c.repo_name))}
 
    /
 
    ${_('summary')}
 
</%def>
 
<%def name="page_nav()">
 
        <form action="{url}log">
 
            {sessionvars%hiddenformentry}
 
            <dl class="search">
 
                <dt><label>Search: </label></dt>
 
                <dd><input type="text" name="rev" /></dd>
 
            </dl>
 
        </form>
 

	
 
        <ul class="page-nav">
 
            <li class="current">summary</li>
 
            <li><a href="{url}shortlog{sessionvars%urlparameter}">shortlog</a></li>
 
            <li><a href="{url}log{sessionvars%urlparameter}">changelog</a></li>
 
            <li><a href="{url}graph/{node|short}{sessionvars%urlparameter}">graph</a></li>
 
            <li><a href="{url}tags{sessionvars%urlparameter}">tags</a></li>
 
            <li><a href="{url}branches{sessionvars%urlparameter}">branches</a></li>
 
            <li><a href="{url}file/{node|short}{sessionvars%urlparameter}">files</a></li>
 
        </ul>
 
</%def>
 
<%def name="main()">
 

	
 
    <h2 class="no-link no-border">${_('Mercurial Repository Overview')}</h2>
 
    <dl class="overview">
 
        <dt>name</dt>
 
        <dd>${c.repo_info.name}</dd>
 
        <dt>description</dt>
 
        <dd>${c.repo_info.description}</dd>
 
        <dt>contact</dt>
 
        <dd>${c.repo_info.contact}</dd>
 
        <dt>last change</dt>
 
        <dd>${c.repo_info.last_change|n,self.f.rfc822date}</dd>
 
        <dd>${c.repo_info.last_change|n,self.f.time_ago}</dd>
 
    </dl>
 

	
 
    <h2><a href="{url}shortlog{sessionvars%urlparameter}">Changes</a></h2>
 
    <table>
 
	%for cnt,cs in enumerate(c.repo_changesets):
 
		<tr class="parity${cnt%2}">
 
			<td>${cs.date}</td>
 
			<td>${cs._ctx.date()|n,self.f.time_ago}</td>
 
			<td>${cs.author}</td>
 
			<td>${cs.message}</td>
 
			<td>${h.link_to(cs.message,h.url('rev/'+str(cs._ctx)))}</td>
 
			<td class="nowrap">
 
			${h.link_to(u'changset')}
 
			${h.link_to(_('changeset'),h.url('file/'+str(cs._ctx)))}
 
			|
 
			${h.link_to(u'files')}
 
			${h.link_to(_('files'),h.url('file/'+str(cs._ctx)))}
 
			</td>
 
		</tr>
 
	%endfor
 
        <tr class="light">
 
            <td colspan="4"><a class="list" href="{url}shortlog{sessionvars%urlparameter}">...</a></td>
 
        </tr>
 
    </table>
 

	
 
    <h2><a href="{url}tags{sessionvars%urlparameter}">Tags</a></h2>
 
    <h2><a href="{url}tags{sessionvars%urlparameter}">${_('Tags')}</a></h2>
 
    <table>
 
{tags}
 
		%for tag in c.repo_tags:
 
			${tag}
 
		%endfor
 
        <tr class="light">
 
            <td colspan="3"><a class="list" href="{url}tags{sessionvars%urlparameter}">...</a></td>
 
        </tr>
 
    </table>
 

	
 
    <h2 class="no-link">Branches</h2>
 
    <table>
 
    {branches%branchentry}
 
		%for branch in c.repo_branches:
 
			${branch}
 
		%endfor
 
        <tr class="light">
 
          <td colspan="4"><a class="list"  href="#">...</a></td>
 
        </tr>
 
    </table>
 

	
 
</%def>    
 
\ No newline at end of file
0 comments (0 inline, 0 general)