source: MILK/trunk/milk_server/milk_server/controllers/atom_editor/atomeditorcontroller.py @ 5212

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/controllers/atom_editor/atomeditorcontroller.py@5212
Revision 5212, 3.3 KB checked in by cbyrom, 11 years ago (diff)

Adjust the Atom and MolesEntity? data models to store their data
internally, 'as is' - and to escape/unescape XML unfriendly characters
on data export/import. Adjust templates and inputs processing to
cope with this change - which allows the input of most characters
without problem (NB, old method caused unfriendly errors when illegal
characters were detected).

Line 
1'''
2 Class representing base pylons controller for the atom editor shared functions
3 
4 @author: C Byrom, Tessella Dec 2008
5'''
6import logging, cgi
7from xml.sax.saxutils import unescape
8from ndg.common.src.models.ndgObject import ndgObject
9from milk_server.lib.base import *
10from milk_server.models.ndgdoc import NDGDoc
11from editorconstants import *
12from milk_server.controllers.home import HomeController
13   
14class AtomEditorController(HomeController):
15    '''
16    Provides the pylons controller for shared functions
17    '''
18    STANDARD_SAVE = 1
19    ADD_ASSOCIATIONS = 3
20    REMOVE_ASSOCIATIONS = 4
21
22    def __before__(self):
23        '''
24        Before anything is done, ensure the atom editor is enabled
25        '''
26        if not g.atomEditorEnabled:
27            logging.info("Editor not enabled - cannot edit atoms - redirecting to default")
28            return h.redirect_to(h.url_for('default'))
29       
30
31    def _setup(self, uri=None, loadAtom=True):
32        '''
33        Common setup stuff for all the actions on this controller
34        @keyword uri: uri of the atom to set up
35        @keyword loadAtom: if True, load the atom into c.atom, otherwise don't.
36        Default is True.
37        '''
38
39        logging.info("Setting up AtomEditorController")
40        self.cf=request.environ['ndgConfig']
41        self.inputs = self.__getTidyInputs()
42
43        if uri:
44            self.ndgObject = ndgObject(uri, config = self.cf)
45
46            self.ndgDoc = NDGDoc(ndgURI = uri, config = self.cf)
47            if loadAtom:
48                self.__prepareDataModel(uri)
49
50        logging.info("AtomEditorController set up")
51
52
53    def __prepareDataModel(self, uri):
54        '''
55        Set up the underlying atom data model - loading the bulk from eXist
56        then updating any input fields appropriately
57        '''
58        logging.info("Preparing underlying data model")
59        logging.info("Retrieving document to edit")
60
61        # NB, passing in the inputs will overwrite any original values with the
62        # user input ones
63        if not self.inputs:
64            self.inputs = self.__getTidyInputs()
65
66        self.ndgDoc.setupDocumentModel(ndgUri = uri, inputs = self.inputs)
67
68        if not self.ndgDoc.status:
69            c.xml = self.ndgDoc.xmlString
70            response.status_code = self.ndgDoc.htmlCode
71            raise SystemError('Problem experienced retrieving atom doc from eXist [%s]' 
72                              %self.ndgDoc.xmlString)
73
74        c.atom = self.ndgDoc.docModel
75
76        # save the current atom - to avoid this needing be recreated by the
77        # asynch viewAssociatedData call
78        session['currentAtom'] = c.atom
79        session.save()
80        logging.info("Data model set up")
81
82
83    def __getTidyInputs(self):
84        '''
85        The inputs can be used generically to specify atom attributes - however
86        some inputs should not be set against the Atom object since they use slots
87        so cannot be pickled - which means saving the atom in the session with fail.
88        This method clears out any unwanted inputs
89        '''
90        logging.debug("Getting pickleable input data")
91        inputs = request.params
92        tidyInputs = {}
93        for key, val in inputs.items():
94            if not isinstance(val, cgi.FieldStorage):
95                tidyInputs[key] = unescape(val)
96
97        logging.debug("Pickleable data extracted")
98        return tidyInputs
Note: See TracBrowser for help on using the repository browser.