source: MILK/trunk/milk_server/milk_server/controllers/trackback/trackback.py @ 4992

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

Adjust code to fit with changes to ndgCommon codebase + tidy up
structure of ndgInterface to make easier to follow + add documentation
+ add universal debug mode + various other small code tidy ups.

Line 
1'''
2 Class representing the pylons controller for trackback functionality.  This allows:
3 
4 i) commenting on metadata
5 ii) referencing of metadata
6 
7 @author: C Byrom, Tessella Feb 2009
8'''
9from milk_server.lib.base import *
10from milk_server.lib.mailer import mailHandler
11from milk_server.lib import mailer
12from ndg.common.src.models.ndgObject import ndgObject
13
14class TrackbackController(BaseController):
15    '''
16    Provides the pylons controller for recieving trackbacks to NDG documents
17    '''
18    def __setup(self,uri):
19        '''
20        Common setup stuff for all the actions on this controller
21        @param uri: ndgURI associated with current doc being viewed
22        '''
23        logging.info("Setting up TrackbackController")
24        self.cf=request.environ['ndgConfig']
25        try:
26            self.uri=ndgObject(uri, config=self.cf)
27        except ValueError,e:
28            return e
29
30        self.inputs = request.params
31
32        logging.info("TrackbackController set up")
33        return 0   
34
35
36   
37    def index(self,uri):
38        '''
39        This is the default trackback receipt method. Note we haven't implemented
40        whitelisting yet
41        '''
42       
43        err=0
44       
45        # need to receive a trackback, and mail someone ...
46        incoming=request.params
47        keys=incoming.keys()
48        payload='<trackback><uri>%s</uri></trackback>'%uri
49       
50        # make sure we have both url and title
51        if 'url' not in keys and 'title' not in keys:
52            err='Incomplete trackback, need both url and title at the every least'
53        for key in keys:
54            payload+='<%s>%s</%s>'%(key,incoming[key],key)
55       
56        # now get server and trackback recipient.
57        self.cf=request.environ['ndgConfig']
58       
59        server=self.cf.get('DEFAULT','mailserver',None)
60        if server is None:
61            err='Server not configured for trackback (no mail server)'
62       
63        recipient=self.cf.get('DEFAULT','tbrecipient',None)
64        if recipient is None:
65            err='Server not configured for trackback (no recipient)'
66       
67        body='Citation Trackback for %s\n\n\n%s'% (uri, payload)
68        name='CitationTrackbackFor_%s.xml'%uri
69       
70        if err!=0:
71            c.message=err
72            c.err=1
73        else: 
74            status,message=mailHandler([recipient,],body,body,xmlAttachments=[(payload,name),],
75                server=server)
76            if status:
77                c.err=0
78            else:
79                c.err=1
80                c.message='Unable to forward trackback to designated recipient [%s]'%message
81       
82        response.headers['content-type'] = 'text/xml'
83        return render('trackback',format='xml')
84   
85   
86           
87
88    def __mytb(self,name):
89        ''' Get the trackback info for this page
90        (can't do it in Kid because it doesn't do substitutions in comments) '''
91        s='''<!--
92        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
93             xmlns:dc="http://purl.org/dc/elements/1.1/"
94             xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
95        <rdf:Description
96            rdf:about="%s"
97            dc:identifier="%s"
98            dc:title="%s"
99            trackback:ping="%s" />
100        </rdf:RDF>
101        -->'''%(self.uri,g.server+h.url_for(),name,g.server+h.url_for(controller="trackback"))
102        return s
103
104
105   
106    def askCorrect(self,uri):
107        '''
108        Provide a form (ajaxaciously) so that a user can correct a metadata record
109        '''
110        logging.info("Called askCorrect - to render a corrections input form")
111        status=self.__setup(uri)
112        c.uri=self.uri
113       
114        if status:
115            return response.write(status)
116
117        try:
118            html =  render("genshi", 'correct',fragment=True)
119        except Exception, e:
120            logging.error("Error experienced whilst rendering 'correct' template: %s" %e)
121           
122        logging.info("- retrieved info - now rendering form")
123        return html
124
125   
126    def correct(self,uri):
127        '''
128        Receive a correction form to update a record and post it to the holder
129        '''
130        logging.info("Submitting correction info - this will be mailed to metadata " + \
131            "maintainer + added to the corrections feed")
132        status=self.__setup(uri)
133#        if 'ndgSec' not in session:
134        if 'ndgSec' in session:
135            c.xml = 'Error - user not currently logged in.  Please login before submitting user comments.'
136            logging.error(c.xml)
137            return render('kid', 'error')
138       
139        body = 'Metadata Change Request for %s' %uri
140        payload = '<Change><uri>%s</uri><from>%s</from><Contents>%s</Contents></Change>' \
141            %(self.inputs['Identifier'],self.inputs['Submitter'],self.inputs['Comment'])
142        name = 'ChangeRequestFor_%s.xml' %uri
143        maintainer = self.cf.get('DEFAULT','metadataMaintainer')
144       
145        logging.info("Mailing corrections info to %s" %maintainer)
146        status,message=mailer.mailHandler([maintainer,],
147                       body,body,xmlAttachments=[(payload,name),],
148                       server=self.cf.get('DEFAULT','mailserver'))
149        if status:
150            result = 'Your comments have been sent to the metadata maintainer and added to corrections feed for the metatadata database.'
151        else:
152            result = message
153           
154        c.xml = result
155        logging.info("- now add these to the corrections feed")
156        # add the maintainer info
157        body += " - for metadata maintainer, '%s'" %maintainer
158        g.validator._atomClient.feedClient.createCorrectionsFeedEntry(body, payload)
159
160        g.correctionPosted = result
161        h.redirect_to(h.url_for(controller = 'browse/retrieve', action='view', \
162                                uri = uri))
163
Note: See TracBrowser for help on using the repository browser.