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      'FeedButton', 
  7      'FeedHeadLinks', 
  8      'FeedLinks', 
  9      'FeedLinksDesc', 
 10      'static_dir', 
 11  ] 
 12   
 13  import logging 
 14   
 15  import pkg_resources 
 16   
 17  from turbogears import url 
 18  from turbogears.widgets import CSSLink, Widget, WidgetDescription, \ 
 19      register_static_directory 
 20   
 21   
 22  log = logging.getLogger('turbofeeds.widgets') 
 23   
 24  static_dir = pkg_resources.resource_filename("turbofeeds", "static") 
 25  register_static_directory("turbofeeds", static_dir) 
 26   
 27   
 98   
113   
114 -class FeedLinksDesc(WidgetDescription):
115 name = "Feed link list" 116 for_widget = FeedLinks(base_url = '/feed') 117 template = """\ 118 <div> 119 ${for_widget("Subscribe to %(type)s feed", 120 title="Click link to access the feed in %(type)s format")} 121 </div> 122 """
123
124 -class FeedButton(Widget):
125 """A text-only button for linking to a feed. 126 127 ``value`` is the link text. May use "%(type)s" placeholder 128 for the feed format name. ``type`` is the type of the feed 129 to link to. 130 131 """ 132 params = ['base_url', 'title', 'type', 'url_params'] 133 template = """\ 134 <a href="${feed_url(type)}" class="feedbutton" 135 py:attrs="dict(title=title % dict(type=name))" xmlns:py="http://purl.org/kid/ns#" 136 ><span class="feedtype">${name}</span> <span class="feedtitle">${value % dict(type=name)}</span></a> 137 """ 138 css = [CSSLink("turbofeeds", "css/feeds.css", media="screen")] 139 base_url = '/feed' 140 types = dict( 141 atom0_3 = 'Atom 0.3', 142 atom1_0 = 'Atom 1.0', 143 rdf = 'RDF', 144 rss2_0 = 'RSS 2.0', 145 ) 146 type = 'atom1_0' 147 title = '' 148 url_params = {} 149 150 params_doc = { 151 'base_url': 'The base_url of the feed. The feed format will be ' 152 'appended to this. Can be determined from "controler", if given.', 153 'title': 'String to use for "title" attribute of feed links. May use ' 154 '"%(type)s" placeholder for the feed format name.', 155 'url_params' : 'Dictionary containing extra URL parameters appended to' 156 ' the feed URL', 157 } 158
159 - def update_params(self, params):
160 """Sets feed URL to callable that generates URL for each format.""" 161 162 super(FeedButton, self).update_params(params) 163 base_url = params.get('base_url') 164 if base_url is None: 165 raise ValueError("You must set 'base_url'.") 166 params['feed_url'] = lambda type: self.feed_url( 167 base_url, type, params['url_params']) 168 params['name'] = self.types[params['type']]
169
170 - def feed_url(self, base_url, type, params):
171 """Returns feed URL by combining base_url, feed type and params.""" 172 173 return url([base_url, type], params)
174 175 176 if __name__ == '__main__': 177 from turbogears.view import load_engines 178 load_engines() 179 fl = FeedLinks(base_url='/feed') 180 print fl.render('View %(type)s feed') 181 print 182 print fl.render('View %(type)s feed', title='Click to view feed in browser', 183 url_params=dict(compat=True)) 184 fl = FeedLinks(base_url='/myfeeds') 185 print fl.render('View my %(type)s feed') 186