Changeset 1705

Show
Ignore:
Timestamp:
07/23/08 12:30:40 (12 months ago)
Author:
JensDiemer
Message:

blog plugin: Change the RSS/Atom feed stuff

Location:
trunk/pylucid
Files:
2 added
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/media/PyLucid/internal_page/blog/display_blog.css

    r1701 r1705  
    3333} 
    3434/* -------------------------------------------------------------------------- */ 
    35 .action_link, .admin_info_line { 
     35.blog .action_link, .blog .admin_info_line { 
    3636  color: #fff; 
    3737  display:block; 
    3838  background-color: #09C; 
    3939} 
    40 .admin_info_line { 
     40.blog .admin_info_line { 
    4141  text-align: center; 
    4242  background-color: #33CC99; 
     
    151151  margin: 0px; 
    152152} 
    153 .feed_type, .feed_type li { 
     153.feeds, .feeds li { 
    154154  display:inline; 
    155155} 
  • trunk/pylucid/media/PyLucid/internal_page/blog/display_blog.html

    r1701 r1705  
    1 {% if back_url %}<p><a href="{{ back_url }}" class="action_link">Display all blog entries</a></p>{% endif %} 
     1{% if back_url %}<p><a href="{{ back_url }}" class="action_link">&laquo; Display all blog entries</a></p>{% endif %} 
    22{% if create_url %}<p><a href="{{ create_url }}" class="action_link">create a new blog entry</a></p>{% endif %} 
    33 
     
    9797 
    9898<fieldset class="feeds"><legend>{% trans 'available syndication feeds' %}</legend> 
    99     {% regroup feed_info|dictsort:"feed_type" by feed_type as feeds_grouped %} 
    100     <ul> 
    101     {% for feed in feeds_grouped %} 
    102         <li class="grouper"><strong>{{ feed.grouper }}:</strong> 
    103         <ul class="feed_type"> 
    104             {% for item in feed.list %} 
    105             <li><a href="{{ item.url }}">{{ item.title_info }}</a></li> 
    106             {% endfor %} 
    107         </ul> 
    108         </li> 
     99    <ul>{% trans 'normal feeds:' %} 
     100    {% for feed in feed_info %} 
     101        <li><a href="{{ feed.url }}">{{ feed.title_info }}</a></li> 
     102    {% endfor %} 
     103    </ul> 
     104    <ul>{% trans 'tag feeds:' %} 
     105    {% for feed in tag_feed_info %} 
     106        <li><a href="{{ feed.url }}">{{ feed.title_info }}</a></li> 
    109107    {% endfor %} 
    110108    </ul> 
    111109</fieldset> 
    112110 
    113 {% if back_url %}<p><a href="{{ back_url }}" class="action_link">Display all blog entries</a></p>{% endif %} 
     111{% if back_url %}<p><a href="{{ back_url }}" class="action_link">&laquo;  Display all blog entries</a></p>{% endif %} 
    114112{% if create_url %}<p><a href="{{ create_url }}" class="action_link">create a new blog entry</a></p>{% endif %} 
  • trunk/pylucid/PyLucid/plugins_internal/blog/blog.py

    r1701 r1705  
    2727from django.conf import settings 
    2828from django.http import HttpResponse 
    29 from django.utils import feedgenerator 
    3029from django.core.mail import send_mail 
    31 from django.utils.safestring import mark_safe 
    32 from django.utils.encoding import force_unicode 
     30#from django.utils.safestring import mark_safe 
     31#from django.utils.encoding import force_unicode 
    3332from django.utils.translation import ugettext as _ 
    34 from django.utils import feedgenerator 
    35 from django.contrib.syndication.feeds import Feed, FeedDoesNotExist 
     33#from django import newforms as forms 
     34#from django.newforms import ValidationError 
     35#from django.contrib.syndication.feeds import Feed, FeedDoesNotExist 
    3636 
    3737# from PyLucid 
     
    4040from PyLucid.tools.utils import escape_django_tags 
    4141from PyLucid.system.page_msg import PageMessages 
     42#from PyLucid.tools.newforms_utils import InternalURLField 
     43from PyLucid.tools.syndication_feed import FeedFormat, FEED_FORMAT_INFO 
    4244 
    4345# from blog plugin 
     
    6567ENTRIES_FEED_NAME = u"entries" 
    6668COMMENTS_FEED_NAME = u"comments" 
    67 TAG_FEED_PREFIX = u"tag_" # The tag slug would be appended! 
    68  
    69 # All file endings must be lower case and without "." ! 
    70 RSS = u"rss" 
    71 ATOM = u"atom" 
    72  
    73 #______________________________________________________________________________ 
    74  
    75 class FeedInfo(list): 
    76     """ 
    77     A list of feed information. 
    78     I a normal list with one spectial add method. 
    79     Contains a dict for every feed info. 
    80     """ 
    81     def add(self, title_info, url_parts): 
    82         assert(isinstance(url_parts, (list,tuple))) #Needed? 
    83         url_start = posixpath.join(*url_parts) 
    84  
    85         list.append(self, { 
    86             "feed_type": RSS, 
    87             "title_info": title_info, 
    88             "url": "%s.%s" % (url_start, RSS), 
    89         }) 
    90         list.append(self, { 
    91             "feed_type": ATOM, 
    92             "title_info": title_info, 
    93             "url": "%s.%s" % (url_start, ATOM), 
    94         }) 
    95  
     69TAG_FEED_PREFIX = u"tag-" # The tag slug would be appended! 
    9670 
    9771#______________________________________________________________________________ 
     
    11185        ) 
    11286 
     87        # URLs 
     88        self.index_url = self.current_page.get_absolute_url() 
     89        self.feed_url_prefix = self.URLs.methodLink("feed") 
     90 
    11391        # Get the default preference entry. 
    11492        self.preferences = self.get_preferences() 
     
    11694        # Change the page title. 
    11795        self.current_page.title = self.preferences["blog_title"] 
    118  
    119         # The absolute url to the page witch contains the blog 
    120         self.index_url = "FIXME" 
    121  
    122         self.feed_url_prefix = self.URLs.methodLink("feed") 
    12396 
    12497 
     
    176149        # Add all available syndication feeds information 
    177150        context["feed_info"] = self._get_feeds_info() 
     151        context["tag_feed_info"] = self._get_tag_feeds_info() 
    178152 
    179153        self._render_template("display_blog", context, debug=0) 
     
    256230            #"blog_entry": blog_entry, 
    257231            "add_comment_form": form, 
    258             "back_url": self.URLs.methodLink("lucidTag"), 
     232            "back_url": self.index_url, 
    259233        } 
    260234 
     
    691665        self._render_template("mod_comments", context)#, debug=2) 
    692666 
    693     def _get_feeds_info(self): 
    694         """ 
    695         returns information about all available syndication feeds. 
    696         """ 
    697         feed_info = FeedInfo() 
    698  
    699         # Add "normal" feeds 
    700         feed_info.add( 
    701             title_info = ENTRIES_FEED_NAME, 
    702             url_parts = (self.feed_url_prefix, ENTRIES_FEED_NAME,) 
    703         ) 
    704         feed_info.add( 
    705             title_info = COMMENTS_FEED_NAME, 
    706             url_parts = (self.feed_url_prefix, COMMENTS_FEED_NAME,) 
    707         ) 
    708  
     667    def _feed_filenames(self): 
     668        """ 
     669        returns a list of all existing feed filenames 
     670        """ 
     671        filenames = [] 
     672        filenames.extend([ENTRIES_FEED_NAME, COMMENTS_FEED_NAME]) 
     673 
     674        tags = self._get_tags() 
     675        filenames.extend([TAG_FEED_PREFIX + tag_slug for tag_slug, _ in tags]) 
     676 
     677        return filenames 
     678 
     679    def _get_tags(self): 
     680        """ 
     681        returns a list of all tags. 
     682        """ 
    709683        # Build a list of tag feeds 
    710684        limit = self.preferences.get("max_tag_feed", 10) 
    711685        tags = BlogTag.objects.values_list("slug", "name").all()[:limit] 
    712         tag_feeds = [TAG_FEED_PREFIX + i[0] for i in tags] 
    713         #self.page_msg(tag_feeds) 
    714  
     686        return tags 
     687 
     688    def _get_feeds_info(self): 
     689        """ 
     690        returns information about all available syndication feeds. 
     691        """ 
     692        feeds = [] 
     693 
     694        for feed_name in (ENTRIES_FEED_NAME, COMMENTS_FEED_NAME,): 
     695            feeds.append({ 
     696                "url": self.URLs.methodLink("select_feed_format", feed_name), 
     697                "title_info": feed_name, 
     698                "filename": feed_name 
     699            }) 
     700 
     701        #self.page_msg(feeds) 
     702        return feeds 
     703 
     704    def _get_tag_feeds_info(self): 
     705        """ 
     706        returns information about all available syndication feeds. 
     707        """ 
     708        tags = self._get_tags() 
     709 
     710        feeds = [] 
    715711        # Add tag feeds 
    716712        for tag_slug, tag_name in tags: 
    717713            filename = TAG_FEED_PREFIX + tag_slug 
    718  
    719             feed_info.add( 
    720                 title_info = tag_name, 
    721                 url_parts = (self.feed_url_prefix, filename,) 
    722             ) 
    723  
    724         return feed_info 
    725  
    726  
    727     def feed(self, raw_feed_name): 
    728         """ 
    729         Generate and return a syndication feeds. 
     714            feeds.append({ 
     715                "url": self.URLs.methodLink("select_feed_format", filename), 
     716                "title_info": tag_name, 
     717                "filename": filename 
     718            }) 
     719 
     720        #self.page_msg(feeds) 
     721        return feeds 
     722 
     723    def select_feed_format(self, raw_feed_name=None): 
     724        """ 
     725        Build a html page with all existing feed formats 
     726        """ 
     727        if raw_feed_name == None: 
     728            return self.error(_("Wrong URL."), "No feed filename given in URL.") 
     729 
     730        feed_name = raw_feed_name.strip("/") 
     731        existing_filenames = self._feed_filenames() 
     732        if feed_name not in existing_filenames: 
     733            return self.error(_("Wrong URL."), "Feed filename doesn't exist") 
     734 
     735        format_info = [] 
     736        for feed_info in FEED_FORMAT_INFO: 
     737            filename = "%s.%s" % (feed_name, feed_info["ext"]) 
     738            format_info.append({ 
     739                "filename": filename, 
     740                "url": self.URLs.methodLink("feed", filename).rstrip("/"), 
     741                "title": feed_info["title"], 
     742                "info_link": feed_info["info_link"], 
     743                "mime_type": feed_info["generator"].mime_type, 
     744            }) 
     745 
     746        context = { 
     747            "back_url": self.index_url, 
     748            "format_info": format_info, 
     749        } 
     750 
     751        self._render_template("select_feed_format", context, debug=0) 
     752 
     753    def feed(self, raw_feed_name=None): 
     754        """ 
     755        Generate and return a syndication feed. 
    730756 
    731757        feed_name e.g.: 
     
    735761            comments .rss/.atom 
    736762        """ 
     763        if raw_feed_name == None: 
     764            return self.error(_("Wrong URL."), "No feed filename given in URL.") 
     765 
    737766        title = self.preferences["blog_title"] 
    738767 
     768        feed_info = FeedFormat() 
    739769        try: 
    740             feed_name, feed_type = os.path.splitext(raw_feed_name) 
    741             feed_type = feed_type.lstrip(".") 
     770            feed_info.parse_filename(raw_feed_name) 
    742771        except Exception, err: 
     772            if self.request.debug: raise 
    743773            return self.error(_("Wrong URL."), err) 
    744774 
    745         if feed_type == RSS: 
    746             FeedGenerator = feedgenerator.Rss201rev2Feed 
    747         elif feed_type == ATOM: 
    748             FeedGenerator = feedgenerator.Atom1Feed 
    749         else: 
    750             return self.error( 
    751                 _("Wrong URL."), " feed type '%s' unknown." % feed_type 
    752             ) 
     775        feed_name = feed_info["feed_name"] 
     776        format_info = feed_info["format_info"] 
     777        FeedGenerator = format_info["generator"] 
    753778 
    754779        limit = self._get_max_count() 
     
    780805        items = entries.filter(is_public=True).all()[:limit] 
    781806 
    782         feed = self._get_feed(FeedGenerator, items, title) 
     807        feed = self._get_feed(FeedGenerator, items, title, feed_name) 
    783808        feed_content = feed.writeString('utf8') 
    784809        content_type = "%s; charset=utf-8" % feed.mime_type 
     
    798823 
    799824 
    800     def _get_feed(self, FeedGenerator, items, title): 
     825    def _get_feed(self, FeedGenerator, items, title, feed_name): 
    801826        """ 
    802827        returns the generated feed. 
     
    805830            title = title, 
    806831            link = self.URLs.make_absolute_url(self.index_url), 
    807             description = self.preferences.get("description", ""), # XXX 
    808             language = self.preferences.get("language", u"en"), # XXX 
     832            description = self.preferences.get("description", ""), 
     833            language = self.preferences.get("language", u"en"), 
    809834        ) 
    810835        for item in items: 
    811             feed.add_item( 
    812                 title = item.headline, 
    813                 link = self.URLs.make_absolute_url( 
    814                     self.URLs.methodLink("detail", item.id) 
    815                 ), 
    816                 description = item.html_content(self.context), 
    817             ) 
     836            if feed_name == COMMENTS_FEED_NAME: 
     837                # Feed with all comments 
     838                feed.add_item( 
     839                    title = _("Comment from '%s' for blog entry '%s'") % ( 
     840                        item.person_name, item.blog_entry.headline 
     841                    ), 
     842                    link = self.URLs.make_absolute_url( 
     843                        self.URLs.methodLink("detail", item.blog_entry.id) 
     844                    ), 
     845                    description = item.content, 
     846                    author_name=item.person_name, 
     847                    pubdate = item.createtime, 
     848                ) 
     849            else: 
     850                # Feed with blog entries 
     851                feed.add_item( 
     852                    title = item.headline, 
     853                    link = self.URLs.make_absolute_url( 
     854                        self.URLs.methodLink("detail", item.id) 
     855                    ), 
     856                    description = item.html_content(self.context), 
     857                    author_name=item.createby, 
     858                    pubdate = item.createtime, 
     859                ) 
    818860 
    819861        return feed 
  • trunk/pylucid/PyLucid/plugins_internal/blog/blog_cfg.py

    r1701 r1705  
    55__author__              = "Jens Diemer" 
    66__url__                 = "http://www.PyLucid.org" 
    7 __description__         = "A simple blog system (extermental)" 
     7__description__         = "A simple blog system" 
    88__long_description__ = """ 
     9More information: http://www.pylucid.org/_goto/165/blog/ 
    910""" 
    1011 
     
    1213# preferences 
    1314 
    14 from django.conf import settings 
    1515from django import newforms as forms 
    1616from django.contrib.auth.models import User 
     
    3535    'mod_keywords' and 'spam_keywords' are case-insensitive and matches on every 
    3636    partial word. Be carefull with 'spam_keywords'. 
     37 
     38    More information: http://www.pylucid.org/_goto/165/blog/ 
    3739    """ 
    3840    blog_title = forms.CharField( 
     
    4345    ) 
    4446    description = forms.CharField( 
    45         initial = "", 
     47        initial = "", required=False, 
    4648        help_text = _( 
    4749            "The blog description (Used for RSS/Atom feeds)." 
     
    132134    ) 
    133135 
    134 # Optional, this Plugin can't have multiple preferences 
     136 
     137# They can only exist one blog in a PyLucid instance 
    135138multiple_pref = False 
    136139 
     
    150153    "add_comment":  anonymous_rights, 
    151154    "detail":       anonymous_rights, 
    152     "feed":         anonymous_rights, 
     155 
     156    "select_feed_format":   anonymous_rights, 
     157    "feed":                 anonymous_rights, 
    153158 
    154159    #__________________________________________________________________________