Ignore:
Timestamp:
20/04/09 09:32:19 (11 years ago)
Author:
cbyrom
Message:

Move re-usable content in atomeditorcontroller to home controller and
inherit this in the atomeditorcontroller.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MILK/trunk/milk_server/milk_server/controllers/home.py

    r5158 r5199  
    11''' 
    2  Fall back controller, to render a home page for the service depending on what 
     2 Base controller for MILK controllers, providing common functionality - including 
     3 fall back index method, to render a home page for the service depending on what 
    34 mode it is running in - NB, if multiple modes are enabled, the order of preference, 
    45 from highest to lowest, is editor, discovery, browser  
     
    67 @author: C Byrom, Tessella Apr 2009 
    78''' 
     9import logging, traceback, xmlrpclib 
     10from formencode import htmlfill 
    811from milk_server.lib.base import * 
    9 from milk_server.models.ndgdoc import NDGDoc  
    10 from ndg.common.src.models.ndgObject import ndgObject 
    11 import ndg.common.src.clients.xmldb.eXist.dbconstants as dc 
    12 from ndg.common.src.lib.mailer import mailHandler 
     12from ndg.common.src.lib.utilities import escapeSpecialCharacters 
     13from ndg.common.src.models.vocabtermdata import VocabTermData as VTD 
    1314 
    1415class HomeController(BaseController): 
     
    3132            return render("genshi", 'error') 
    3233             
     34 
     35    def savePageAndRender(self, template, **inputs): 
     36        ''' 
     37        Save the current path info - to provide a memory function when changing 
     38        tabs + render the given template with the specified inputs filled in 
     39        automatically 
     40        @param template: name of template to render 
     41        @param inputs: dict of inputs with keynames matching input names in template 
     42        form   
     43        ''' 
     44        logging.debug("Saving current page url (%s) - to keep track of atom editor state" \ 
     45                      %self.pathInfo) 
     46        session['currentEditor'] = self.pathInfo 
     47        session.save() 
     48        logging.debug("Now rendering current template (%s)" %template) 
     49        try: 
     50            html = render("genshi", template) 
     51             
     52            # replace any browse links with to links to editor 
     53            html = html.replace(VTD.BROWSE_SERVER_URL, g.server) 
     54             
     55            # NB, need html in unicode for for htmlfill.render 
     56            html = unicode(html, 'utf-8', 'xmlcharrefreplace') 
     57            for key, val in inputs.items(): 
     58                inputs[key] = str(val) 
     59     
     60            return htmlfill.render(html, inputs) 
     61 
     62        except Exception, e: 
     63            c.xml = 'Unexpected error [%s] whilst rendering page' %str(e) 
     64            c.doc = '' 
     65            response.status_code = 400 
     66            logging.error(c.xml) 
     67            return render('error') 
     68 
     69 
     70    def _unpackErrors(self, e): 
     71        ''' 
     72        Add exception errors to the common error structures - for use 
     73        in templates 
     74        @param e: Exception to add 
     75        @return errors: dict of errors 
     76        ''' 
     77        if not c.errors: 
     78            c.errors = {} 
     79 
     80        errorMessage = e.message 
     81        if g.debugModeOn == 'True': 
     82            errorMessage = traceback.format_exc() 
     83 
     84        c.xml = escapeSpecialCharacters('Unexpected error loading page [%s]' \ 
     85                                        %str(errorMessage)) 
     86        c.doc = '' 
     87         
     88        # unpack errors, if possible - NB, the c.errors value is used by the template 
     89        # function, displayErrors, which is used by most of the editor templates 
     90        if isinstance(e, xmlrpclib.Fault): 
     91            # strip out the exception type - NB, this is usually native library code 
     92            # and is of no real interest - and will just confuse viewers 
     93            c.errors['Unexpected error'] = e.faultString.split(':')[-1]  
     94 
     95        if hasattr(e, 'unpack_errors'): 
     96            c.errors.update(e.unpack_errors()) 
     97             
     98        else: 
     99            c.errors['Unexpected error'] = [c.xml] 
     100 
     101        # tidy up errors - escaping any xml tags, if necessary 
     102        for key, errors in c.errors.items(): 
     103            newErrors = [] 
     104            for error in errors: 
     105                for err in error.split('<br/>'): 
     106                    newErrors.append(escapeSpecialCharacters(err)) 
     107            c.errors[key] = newErrors 
     108         
     109        return c.errors 
     110 
     111 
     112    def _handleError(self, e, template='error'): 
     113        ''' 
     114        Handle exceptions thrown; if debug mode on, display full stack trace 
     115        in output, otherwise just show basic error message in error template 
     116        @param e: Exception to process 
     117        @keyword template: template to render - 'error' is the default - NB, if 
     118        an alternative is specified it should have a div with class set to 'error' 
     119        containing the variable, c.xml to display properly  
     120        ''' 
     121        self._unpackErrors(e) 
     122        logging.error(c.xml) 
     123        response.status_code = 400 
     124        return render("genshi", template) 
Note: See TracChangeset for help on using the changeset viewer.