source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/retrieve.py @ 2646

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/retrieve.py@2646
Revision 2646, 5.1 KB checked in by lawrence, 13 years ago (diff)

Interim check in for citation trackback support and
metadata correction from logged in ndg users.

Line 
1
2from ows_server.lib.base import *
3from ows_server.models import ndgObject,  ndgRetrieve
4from paste.request import parse_querystring
5from ows_server.models import stubB
6from ows_server.lib import mailer
7
8#from ows_server.models import ndgRetrieve
9
10class RetrieveController(BaseController):
11    ''' Provides the pylons controller for retrieving NDG documents. The simple model
12    is now that an attempt to retrieve/uri will parse the uri, read the config file,
13    and if the local server name is not the same as the uri server name. '''
14   
15    def __setup(self,uri):
16        ''' Common setup stuff for all the actions on this controller '''
17        try:
18            self.uri=ndgObject.ndgObject(uri)
19        except ValueError,e:
20            return e
21        self.cf=request.environ['ndgConfig']
22        self.uri.setConfig(self.cf)
23        self.requestor=request.environ['REMOTE_ADDR']
24        self.logger=0
25        if logger: self.logger=logger
26        self.inputs=dict(parse_querystring(request.environ))
27        # it's kind of unfortunate that format=(xml,html) is what we expect in the
28        # url but at the retrieval level format is the schema ... oh well ...
29        self.format=''
30        if 'original' in self.inputs:
31            self.format={'0':'','1':'original'}[self.inputs['original']]
32        return 0
33       
34    def index(self,uri):
35        ''' Returns the document unadorned in anyway, i.e. the raw xml'''
36        status=self.__setup(uri)
37        if status: return Response(status)
38        status,x=ndgRetrieve.ndgRetrieve(
39                self.uri,self.cf,logger=self.logger,requestor=self.requestor,format=self.format)
40       
41        if status:
42            #(the return object x is an xmlHandler object)
43            xml=x.tohtml()
44            return Response(xml)
45        else:
46            return Response(xml)
47       
48    def view(self,uri):
49        ''' Returns either an html marked up version of the xml, or a properly laid
50        out version of the document '''
51        status=self.__setup(uri)
52        if status: return Response(status)
53        if 'ndgSec' in session:
54            securityTokens=session['ndgSec']
55        else: securityTokens=None
56        status,x=ndgRetrieve.ndgRetrieve(
57                self.uri,self.cf,logger=self.logger,requestor=self.requestor,format=self.format,
58                securityTokens=securityTokens)
59        c.title='Viewing [%s]'%self.uri
60       
61        if status:
62            #(the return object x is an xmlHandler object)
63            if self.uri.schema=='NDG-B1':
64                r='stubB'
65                c.doc=stubB.stubB(x.tree,self.cf)
66                c.tbinfo=self.__mytb()
67            elif self.uri.schema=='NDG-A0':
68                r='csml'
69            elif self.uri.schema=='DIF':
70                r='dif'
71            else:
72                c.xml=x.tohtml()
73                r='content'
74            if 'format' in self.inputs:
75                #override previous output choice if format is xml
76                if self.inputs['format']=='xml': 
77                    c.xml=x.tohtml()
78                    r='content'
79            f=file('debug.xml','w')
80            f.write(x.tohtml())
81            f.close()
82            g=file('debug2.xml','w')
83            g.write(str(x))
84            g.close()       
85        else: 
86            c.xml='<div class="error">%s</div>'%x
87            r='content'
88        return render_response(r)
89   
90    def askCorrect(self,uri):
91        ''' Provide a form (ajaxaciously) so that a user can correct a metadata record'''
92        status=self.__setup(uri)
93        c.uri=self.uri
94        if status: return Response(status)
95        return render_response('correct',fragment=True)
96
97   
98    def correct(self,uri):
99        ''' Receive a correction form to update a record and post it to the holder '''
100        status=self.__setup(uri)
101        if 'ndgSec' not in session:
102            return Response('Not Logged In')
103        body='Metadata Change Request for %s'%uri
104        payload='<Change><uri>%s</uri><from>%s</from><Contents>%s</Contents></Change>'%(
105            self.inputs['Identifier'],self.inputs['Submitter'],self.inputs['Comment'])
106        name='ChangeRequestFor_%s.xml'%uri
107        status,message=mailer.mailHandler([self.cf.get('DEFAULT','metadataMaintainer'),],
108                       body,body,xmlAttachments=[(payload,name),],
109                       server=self.cf.get('DEFAULT','mailserver'))
110        if status:
111            c.xml='<p>Success: Mail sent to metadata maintainer</p>'
112        else:
113            c.xml=message
114        return render_response('content')
115       
116
117    def __mytb(self):
118        ''' Get the trackback info for this page
119        (can't do it in Kid because it doesn't do substutions in comments) '''
120        s='''<!--
121        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
122             xmlns:dc="http://purl.org/dc/elements/1.1/"
123             xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
124        <rdf:Description
125            rdf:about="%s"
126            dc:identifier="%s"
127            dc:title="%s"
128            trackback:ping="%s" />
129        </rdf:RDF>
130        -->'''%(self.uri,g.server+h.url_for(),c.doc.name,g.server+h.url_for(controller="trackback"))
131        return s
Note: See TracBrowser for help on using the repository browser.