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

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

Extend trackback code to add author details to produced feeds.

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 NB, info on TrackBack available from:
8 
9 http://www.sixapart.com/pronet/docs/trackback_spec
10 
11 @author: C Byrom, Tessella Feb 2009
12'''
13from milk_server.lib.base import *
14import milk_server.lib.constants as constants
15from milk_server.models.ndgdoc import NDGDoc
16from ndg.common.src.models.ndgObject import ndgObject
17import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
18from ndg.common.src.lib.mailer import mailHandler
19
20class TrackbackController(BaseController):
21    '''
22    Provides the pylons controller for recieving trackbacks to NDG documents
23    '''
24    def __setup(self,uri):
25        '''
26        Common setup stuff for all the actions on this controller
27        @param uri: ndgURI associated with current doc being viewed
28        '''
29        logging.info("Setting up TrackbackController")
30        self.cf = request.environ['ndgConfig']
31        try:
32            self.uri=ndgObject(uri, config = self.cf)
33        except ValueError,e:
34            return e
35
36        self.inputs = request.params
37
38        self.ndgDoc = NDGDoc(ndgURI = uri, config = self.cf)
39        logging.info("TrackbackController set up")
40        return 0   
41
42
43    def ping(self, uri):
44        '''
45        This is the default trackback receipt method. When a doc is referenced
46        from a TrackBack enabled site, this method should be invoked, via a
47        TrackBack ping, to notify that it has been referenced.  Log the reference
48        by sending a mail to the trackback recipient set in the config file and
49        adding an entry to the citation feed.
50        ( Note we haven't implemented whitelisting yet )
51        @param uri: ndgURI associated with the doc being pinged by trackback
52        '''
53        logging.info("Trackback ping called - notifying trackback recipient + feed of citation")
54        err = 0
55        status=self.__setup(uri)
56       
57        # lookup referenced doc - to ensure it exists and get extra info to add
58        # to citation messages
59        self.ndgDoc.setupDocumentModel()
60        payload = ""
61        if self.ndgDoc.status:
62            payload = '<trackback><uri>%s</uri><title>%s</title></trackback>' %(self.ndgDoc.docModel.atomBrowseURL, 
63                                                                                self.ndgDoc.name)
64        else:
65            err = "Could not find document, '%s', to reference in trackback" %uri
66       
67        if not g.mailServer:
68            err = 'Server not configured for trackback (no mail server)'
69       
70        if not g.tbRecipient:
71            err = 'Server not configured for trackback (no recipient)'
72       
73        body='Citation Trackback for %s\n\n\n%s'% (uri, payload)
74        name='CitationTrackbackFor_%s.xml'%uri
75       
76        if err != 0:
77            c.message = err
78            c.err = 1
79        else: 
80            logging.info("Sending notification mail to '%s'" %g.tbRecipient)
81            status, message = mailHandler([g.tbRecipient,], body, body,
82                                          xmlAttachments = [(payload, name),],
83                                          server = g.mailServer)
84            if status:
85                c.err = 0
86            else:
87                c.err = 1
88                c.message = 'Unable to forward trackback to designated recipient [%s]' %message
89
90            # lastly, add to citation feed
91            logging.info("Mail sent - now add data to the citations feed")
92            g.validator._atomClient.feedClient.createGenericFeedEntry(constants.AUTO_AUTHOR,
93                                                                      name, payload,
94                                                                      dc.CITATIONS_COLLECTION_PATH)
95       
96        response.headers['content-type'] = 'text/xml'
97
98        logging.info("Trackback logged - now sending receipt")
99        return render('genshi', 'trackback/trackback',format='xml')
100   
101
102    def getTrackBackInfo(self, uri, name):
103        '''
104        Get the trackback info for the specified uri and name
105        @param uri: ndgURI representing doc
106        @param name: title of doc
107        @return rdf element representing trackback info
108        '''
109        s='''<!--
110        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
111             xmlns:dc="http://purl.org/dc/elements/1.1/"
112             xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
113        <rdf:Description
114            rdf:about="%s"
115            dc:identifier="%s"
116            dc:title="%s"
117            trackback:ping="%s" />
118        </rdf:RDF>
119        -->'''%(uri,g.server+h.url_for(),name,
120                g.server+h.url_for(controller="trackback/trackback", action="ping"))
121        return s
122
123   
124    def askCorrect(self,uri):
125        '''
126        Provide a form (ajaxaciously) so that a user can correct a metadata record
127        '''
128        logging.info("Called askCorrect - to render a corrections input form")
129        status=self.__setup(uri)
130        c.uri=self.uri
131       
132        if status:
133            return response.write(status)
134
135        try:
136            html =  render("genshi", 'trackback/correct',fragment=True)
137        except Exception, e:
138            logging.error("Error experienced whilst rendering 'correct' template: %s" %e)
139           
140        logging.info("- retrieved info - now rendering form")
141        return html
142
143   
144    def correct(self, uri):
145        '''
146        Receive a correction form to update a record and post it to the holder
147        '''
148        logging.info("Submitting correction info - this will be mailed to metadata " + \
149            "maintainer + added to the corrections feed")
150        status=self.__setup(uri)
151        if 'ndgSec' not in session:
152            c.xml = 'Error - user not currently logged in.  Please login before submitting user comments.'
153            logging.error(c.xml)
154            return render('kid', 'error')
155       
156        body = 'Metadata Change Request for %s' %uri
157        payload = '<Change><uri>%s</uri><from>%s</from><Contents>%s</Contents></Change>' \
158            %(self.inputs['Identifier'],self.inputs['Submitter'],self.inputs['Comment'])
159        name = 'ChangeRequestFor_%s.xml' %uri
160       
161        logging.info("Mailing corrections info to %s" %g.metadataMaintainer)
162        status,message = mailHandler([g.metadataMaintainer],
163                                     body,body,xmlAttachments=[(payload,name),],
164                                     server = g.mailServer)
165        if status:
166            result = 'Your comments have been sent to the metadata maintainer and added to corrections feed for the metatadata database.'
167        else:
168            result = message
169           
170        c.xml = result
171        logging.info("- now add these to the corrections feed")
172        # add the maintainer info
173        body += " - for metadata maintainer, '%s'" %g.metadataMaintainer
174        g.validator._atomClient.feedClient.createGenericFeedEntry(self.inputs['Submitter'], body, payload,
175                                                                  dc.CORRECTIONS_COLLECTION_PATH)
176
177        g.correctionPosted = result
178        h.redirect_to(h.url_for(controller = 'browse/retrieve', action='view', \
179                                uri = uri))
180
Note: See TracBrowser for help on using the repository browser.