turbofeeds.widgets

Package turbofeeds :: Module widgets |
[hide private]

# Source Code for Module turbofeeds.widgets

      1  # -*- coding: UTF-8 -*-
      2  """TurboFeed widgets for RSS/Atom feeds handling."""
      3  __docformat__ = 'restructuredtext'
      4
      5  __all__ = [
      6      'FeedLinks',
      7      'FeedLinksDesc',
      8      'js_dir',
      9  ]
     10
     11  import logging
     12
     13  import pkg_resources
     14
     15  from turbogears import url
     16  from turbogears.widgets import CSSLink, Widget, WidgetDescription, \
     17      register_static_directory
     18
     19
     20  log = logging.getLogger('turbofeeds.widgets')
     21
     22  static_dir = pkg_resources.resource_filename("turbofeeds", "static")
     23  register_static_directory("turbofeeds", static_dir)
     24
     25
     26 -class FeedLinks(Widget):
     27      """A list of links to feeds for all supported formats.
     28
     29      value is the link text. May use "%(type)s" placeholder
     30      for the feed format name.
     31      """
     32
     33      params = ['base_url', 'controller', 'feed_types', 'title', 'url_params']
     34      template = """\
     35  <div xmlns:py="http://purl.org/kid/ns#" py:strip="">
     36    <ul class="feedlinklist">
     37      <li py:for="type, name in feed_types">
     38        <a py:attrs="dict(title=title % dict(type=name))" class="feedlink"
     39          href="${feed_url(type)}">${value % dict(type=name)}</a>
     40      </li>
     41    </ul>
     42  </div>
     43  """
     44      css = [CSSLink("turbofeeds", "css/feeds.css", media="screen")]
     45      base_url = None
     46      controller = None
     47      feed_types = [('rss2_0', 'RSS 2.0'), ('atom0_3', 'Atom 0.3'),
     48          ('atom1_0', 'Atom 1.0')]
     49      title = ''
     50      url_params = {}
     51
     52      params_doc = {
     53          'base_url': 'The base_url of the feed. The feed format will be '
     54              'appended to this. Can be determined from "controler", if given.',
     55          'controller': 'The FeedController instance serving the feeds the '
     56              'links will point to.',
     57          'feed_types': 'A list of 2-item tuples matching format identifier to '
     58              'format name. A link will be generated for each format.',
     59          'title': 'String to use for "title" attribute of feed links. May use '
     60              '"%(type)s" placeholder for the feed format name.',
     61          'url_params' : 'Dictionary containing extra URL parameters appended to'
     62              ' the feed URL',
     63      }
     64
     65 -    def update_params(self, params):
     66          """Sets feed URL to callable that generates URL for each format."""
     67
     68          super(FeedLinks, self).update_params(params)
     69          base_url = params.get('base_url')
     70          if base_url is None:
     71              try:
     72                  base_url = params['controller'].base_url
     73                  log.debug('FeedController root URL: %s', base_url)
     74              except (KeyError, AttributeError):
     75                  raise ValueError("You must set 'base_url' or 'controller'.")
     76          params['feed_url'] = lambda type: self.feed_url(
     77              base_url, type, params['url_params'])
     78
     79 -    def feed_url(self, base_url, type, params):
     80          """Returns feed URL by combining base_url, feed type and params."""
     81
     82          return url("%s/%s" % (base_url, type), params)
     83
     84
     85 -class FeedLinksDesc(WidgetDescription):
     86      name = "Feed link list"
     87      for_widget = FeedLinks(base_url = '/feed')
     88      template = """
     89      <div>
     90          ${for_widget("Subscribe to %(type)s feed",
     91            title="Click link to access the feed in %(type)s format")}
     92      </div>
     93      """
     94
     95
     96  if __name__ == '__main__':
     97      fl = FeedLinks()
     98      print fl.render('View %(type)s feed')
     99      print
    100      print fl.render('View %(type)s feed', title='Click to view feed in browser',
    101        url_params=dict(compat=True))
    102      fl = FeedLinks(base_url='/myfeeds')
    103      print fl.render('View my %(type)s feed')
    104