source: MILK/trunk/milk_server/milk_server/controllers/feed.py @ 5299

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

Add new controller, feeds, to centralise all feed creation code. Add
new template, feed_creator, to allow input form to directly create
atom feed entry. Add general feed to discovery search page - tieing
this to the feed created by feed_creator. Add icons for feed and
include these in the config set up.

Line 
1'''
2 Class representing pylons controller for the retrieval of atom feeds
3 
4 @author: C Byrom, Tessella May 2009
5'''
6import logging
7from milk_server.lib.base import *
8from milk_server.lib.constants import *
9import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
10from milk_server.controllers.home import HomeController
11   
12class FeedController(HomeController):
13    '''
14    Provides the pylons controller for retrieving atom feeds.
15    '''
16    def getFeed(self, feedType):
17        '''
18        Retrieve data from the specified feed type
19        @param feedType: type of feed to rertrieve.  Currently supports
20        types, editorconstants.DIF_FEED and editorconstants.PUBLISHED_FEED
21        @raise ValueError: if unrecognised feed type input
22        @return XML doc representing requested feed
23        '''
24        logging.info("Retrieving feed for '%s' data" %feedType)
25       
26        collection = self.__getFeedCollection(feedType)
27       
28        feed, xml = g.validator._atomClient.feedClient.getAtomFeed(collection,
29                                                                   proxyServer = g.proxyServer)
30       
31        logging.info("- returning feed")
32        response.headers['Content-Type'] = 'application/xml'
33        response.write(xml)
34
35
36    def createFeedEntry(self, feedType):
37        '''
38        Render the input form to create content for a particular feed
39        @param feedType: the feed type to create the entry for
40        '''
41        logging.debug("Setting up input form to add feed entry for feed, '%s'" %feedType)
42        # check feed type is valid
43        collection = self.__getFeedCollection(feedType)
44
45        c.feedType = feedType
46        c.title = "Create feed entry for '%s' feed" %feedType
47        c.createEntryLink = h.url_for('addFeedEntry')
48        return self.savePageAndRender("feed_creator", **dict(request.params))
49       
50
51    def addFeedEntry(self):
52        '''
53        Add an entry to a particular feed
54        '''
55        feedType = request.params['feedType']
56        c.errors = {}
57        for input in ['feedTitle', 'feedContent', 'authorName']:
58            if not request.params.has_key(input):
59                if ['Missing value'] not in c.errors:
60                    c.errors['Missing value'] = []
61                c.errors['Missing value'].append(input)
62            elif not request.params[input]:
63                c.errors[input] = 'Please enter a value'
64
65        if c.errors:
66            return self.createFeedEntry(feedType)
67
68        authorName = request.params['authorName']
69        title = request.params['feedTitle']
70        content = request.params['feedContent']
71       
72        logging.debug("Adding feed entry for feed, '%s'" %feedType)
73
74        collection = self.__getFeedCollection(feedType)
75        g.validator._atomClient.feedClient.createGenericFeedEntry(authorName,
76                                                                  title, content,
77                                                                  collection)
78        setattr(g, POP_UP_MESSAGE, 'A feed entry has successfully been added to the %s feed' %feedType)
79
80        logging.info("Feed entry added - returning to create feed entry page")
81        h.redirect_to(h.url_for('createFeedEntry', feedType = feedType))
82
83       
84    def __getFeedCollection(self, feedType):
85        '''
86        Get the correct eXist collection relating to the specified feed type
87        @param feedType: name of feed type to get the collection for
88        @return dbPath: path of collection relating to feedType
89        '''
90        logging.debug("Getting collection for feed type, '%s'" %feedType)
91        collection = ""
92        if feedType == PUBLISHED_FEED:
93            collection = dc.ATOM_COLLECTION_PATH + dc.PUBLISHED_COLLECTION_PATH
94        elif feedType == DIF_FEED:
95            collection = dc.DIF_COLLECTION_PATH
96        elif feedType == DISCOVERY_FEED:
97            collection = dc.DISCOVERY_FEED_PATH
98        else:
99            raise ValueError("- unrecognised feed type (%s) - currently only support types, '%s', '%s' and '%s'"
100                             %(feedType, PUBLISHED_FEED, DIF_FEED, DISCOVERY_FEED))
101       
102        logging.debug("- returning collection '%s'" %collection)
103        return collection
104
105       
Note: See TracBrowser for help on using the repository browser.