Changeset 5199


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.

Location:
MILK/trunk/milk_server/milk_server/controllers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • MILK/trunk/milk_server/milk_server/controllers/atom_editor/atomeditorcontroller.py

    r5158 r5199  
    44 @author: C Byrom, Tessella Dec 2008 
    55''' 
    6 import logging, xmlrpclib, cgi, traceback 
     6import logging, cgi 
    77from ndg.common.src.models.ndgObject import ndgObject 
    8 from ndg.common.src.lib.utilities import escapeSpecialCharacters 
    9 from ndg.common.src.models.vocabtermdata import VocabTermData as VTD 
    108from milk_server.lib.base import * 
    119from milk_server.models.ndgdoc import NDGDoc  
    1210from editorconstants import * 
    13 from formencode import htmlfill 
     11from milk_server.controllers.home import HomeController 
    1412    
    15 class AtomEditorController(BaseController): 
     13class AtomEditorController(HomeController): 
    1614    '''  
    1715    Provides the pylons controller for shared functions 
     
    9896        logging.debug("Pickleable data extracted") 
    9997        return tidyInputs 
    100  
    101  
    102     def _unpackErrors(self, e): 
    103         ''' 
    104         Add exception errors to the common error structures - for use 
    105         in templates 
    106         @param e: Exception to add 
    107         @return errors: dict of errors 
    108         ''' 
    109         if not c.errors: 
    110             c.errors = {} 
    111  
    112         errorMessage = e.message 
    113         if g.debugModeOn == 'True': 
    114             errorMessage = traceback.format_exc() 
    115  
    116         c.xml = escapeSpecialCharacters('Unexpected error loading page [%s]' \ 
    117                                         %str(errorMessage)) 
    118         c.doc = '' 
    119          
    120         # unpack errors, if possible - NB, the c.errors value is used by the template 
    121         # function, displayErrors, which is used by most of the editor templates 
    122         if isinstance(e, xmlrpclib.Fault): 
    123             # strip out the exception type - NB, this is usually native library code 
    124             # and is of no real interest - and will just confuse viewers 
    125             c.errors['Unexpected error'] = e.faultString.split(':')[-1]  
    126  
    127         if hasattr(e, 'unpack_errors'): 
    128             c.errors.update(e.unpack_errors()) 
    129              
    130         else: 
    131             c.errors['Unexpected error'] = [c.xml] 
    132  
    133         # tidy up errors - escaping any xml tags, if necessary 
    134         for key, errors in c.errors.items(): 
    135             newErrors = [] 
    136             for error in errors: 
    137                 for err in error.split('<br/>'): 
    138                     newErrors.append(escapeSpecialCharacters(err)) 
    139             c.errors[key] = newErrors 
    140          
    141         return c.errors 
    142  
    143  
    144     def _handleError(self, e, template='error'): 
    145         ''' 
    146         Handle exceptions thrown; if debug mode on, display full stack trace 
    147         in output, otherwise just show basic error message in error template 
    148         @param e: Exception to process 
    149         @keyword template: template to render - 'error' is the default - NB, if 
    150         an alternative is specified it should have a div with class set to 'error' 
    151         containing the variable, c.xml to display properly  
    152         ''' 
    153         self._unpackErrors(e) 
    154         logging.error(c.xml) 
    155         response.status_code = 400 
    156         return render("genshi", template) 
    157  
    158  
    159     def savePageAndRender(self, template, **inputs): 
    160         ''' 
    161         Save the current path info - to provide a memory function when changing 
    162         tabs + render the given template with the specified inputs filled in 
    163         automatically 
    164         @param template: name of template to render 
    165         @param inputs: dict of inputs with keynames matching input names in template 
    166         form   
    167         ''' 
    168         logging.debug("Saving current page url (%s) - to keep track of atom editor state" \ 
    169                       %self.pathInfo) 
    170         session['currentEditor'] = self.pathInfo 
    171         session.save() 
    172         logging.debug("Now rendering current template (%s)" %template) 
    173         html = render("genshi", template) 
    174          
    175         # replace any browse links with to links to editor 
    176         html = html.replace(VTD.BROWSE_SERVER_URL, g.server) 
    177          
    178         # NB, need html in unicode for for htmlfill.render 
    179         html = unicode(html, 'utf-8', 'xmlcharrefreplace') 
    180         for key, val in inputs.items(): 
    181             inputs[key] = str(val) 
    182  
    183         return htmlfill.render(html, inputs) 
  • 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.