Changeset - 9f6300b96380
[Not reviewed]
default
0 2 1
Marcin Kuzminski - 16 years ago 2010-04-18 21:15:53
marcin@python-blog.com
Updated error handling, from mercurial to pylons. + added tempalte for 404
3 files changed with 59 insertions and 6 deletions:
0 comments (0 inline, 0 general)
pylons_app/controllers/error.py
Show inline comments
 
import logging
 
from paste.urlparser import PkgResourcesParser
 
import paste.fileapp
 
from pylons import request, tmpl_context as c
 
from pylons import tmpl_context as c, app_globals as g, request, config
 
from pylons.controllers.util import forward
 
from pylons.i18n.translation import _
 
from pylons_app.lib.base import BaseController, render
 
from pylons.middleware  import error_document_template, media_path
 
import cgi
 
import os
 

	
 
log = logging.getLogger(__name__)
 
class ErrorController(BaseController):
 
    """
 
    Generates error documents as and when they are required.
 

	
 
    The ErrorDocuments middleware forwards to ErrorController when error
 
    related status codes are returned from the application.
 

	
 
    This behaviour can be altered by changing the parameters to the
 
    ErrorDocuments middleware in your config/middleware.py file.
 
    """
 
#
 
    def __before__(self):
 
        pass
 

	
 
        c.repos_prefix = config['repos_name']
 
        c.staticurl = g.statics
 
        c.repo_name = request.environ['pylons.original_request']\
 
            .environ.get('PATH_INFO').split('/')[-1]
 
        
 
    def document(self):
 

	
 
        resp = request.environ.get('pylons.original_response')
 
        log.debug(resp.status)
 

	
 
        e = request.environ
 
        c.serv_p = r'%(protocol)s://%(host)s/' % {
 
                                                'protocol': e.get('wsgi.url_scheme'),
 
                                                'host':e.get('HTTP_HOST'),
 
                                                }
 
                
 
        if resp.status_int == 404:
 
            return render('/errors/error_404.html')
 
                
 
        c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
 
        c.error_explanation = self.get_error_explanation(resp.status_int)
 

	
 
        c.serv_p = ''.join(['http://', request.environ.get('HTTP_HOST', '')])
 

	
 
        #redirect to when error with given seconds
 
        c.redirect_time = 0
 
        c.redirect_module = _('Home page')# name to what your going to be redirected
 
        c.url_redirect = "/"
 

	
 
        return render('/errors/error_document.html')
 

	
 

	
 
    def img(self, id):
 
        """Serve Pylons' stock images"""
 
        return self._serve_file(os.path.join(media_path, 'img', id))
 

	
 
    def style(self, id):
 
        """Serve Pylons' stock stylesheets"""
 
        return self._serve_file(os.path.join(media_path, 'style', id))
 

	
 
    def _serve_file(self, path):
 
        """Call Paste's FileApp (a WSGI application) to serve the file
 
        at the specified path
 
        """
 
        fapp = paste.fileapp.FileApp(path)
 
        return fapp(request.environ, self.start_response)
 

	
 
    def get_error_explanation(self, code):
pylons_app/lib/filters.py
Show inline comments
 
from mercurial import util
 
from mercurial.templatefilters import age as _age, person as _person
 
from string import punctuation
 

	
 
def clean_repo(repo_name):
 
    for x in punctuation:
 
        if x != '_':
 
            repo_name = repo_name.replace(x, '')
 
    repo_name = repo_name.lower().strip()
 
    return repo_name.replace(' ', '_')
 

	
 
age = lambda  x:_age(x)
 
capitalize = lambda x: x.capitalize()
 
date = lambda x: util.datestr(x)
 
email = util.email
 
person = lambda x: _person(x)
 
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])
 
rfc822date = lambda  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")
 
time_ago = lambda x: util.datestr(_age(x), "%a, %d %b %Y %H:%M:%S %1%2")
pylons_app/templates/errors/error_404.html
Show inline comments
 
new file 100644
 
## -*- coding: utf-8 -*-
 
<%!
 
from pylons_app.lib import filters
 
%>
 
<%inherit file="./../base/base.html"/>
 
            
 
<%def name="title()">
 
    ${_('Repository not found')}
 
</%def>
 

	
 
<%def name="breadcrumbs()">
 
	${h.link_to(u'Home',h.url('hg_home'))}
 
	 / 
 
	${h.link_to(u'Admin',h.url('admin_home'))}
 
</%def>
 

	
 
<%def name="page_nav()">
 
<li>${h.link_to(u'Home',h.url('hg_home'))}</li>
 
<li class="current">${_('Admin')}</li>
 
</%def>
 
<%def name="js()">
 

	
 
</%def>
 
<%def name="main()">
 

	
 
    <h2 class="no-link no-border">Not Found</h2>
 
    <p class="normal">The specified repository "${c.repo_name}" is unknown, sorry.</p>
 
    <p class="normal">
 
    <a href="/_admin/add_repo/${c.repo_name|n,filters.clean_repo}">Create "${c.repo_name}" repository as ${c.repo_name|n,filters.clean_repo}</a>
 

	
 
    </p>
 
    <p class="normal">Go back to the ${h.link_to(_('main repository list page'),h.url('hg_home'))}.</p>
 
    <div class="page-footer">
 
    </div>
 
</%def>
 
\ No newline at end of file
0 comments (0 inline, 0 general)