Atom Syndication
最后更新于:2022-04-01 04:07:55
# Atom Syndication
This module provides a class called [AtomFeed](# "werkzeug.contrib.atom.AtomFeed") which can beused to generate feeds in the Atom syndication format (see [**RFC 4287**](http://tools.ietf.org/html/rfc4287.html) [http://tools.ietf.org/html/rfc4287.html]).
Example:
~~~
def atom_feed(request):
feed = AtomFeed("My Blog", feed_url=request.url,
url=request.host_url,
subtitle="My example blog for a feed test.")
for post in Post.query.limit(10).all():
feed.add(post.title, post.body, content_type='html',
author=post.author, url=post.url, id=post.uid,
updated=post.last_update, published=post.pub_date)
return feed.get_response()
~~~
*class *werkzeug.contrib.atom.AtomFeed(*title=None*, *entries=None*, ***kwargs*)
A helper class that creates Atom feeds.
<table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple"><li><strong>title</strong> – the title of the feed. Required.</li><li><strong>title_type</strong> – the type attribute for the title element. One of<tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>.</li><li><strong>url</strong> – the url for the feed (not the url <em>of</em> the feed)</li><li><strong>id</strong> – a globally unique id for the feed. Must be an URI. Ifnot present the <cite>feed_url</cite> is used, but one of both isrequired.</li><li><strong>updated</strong> – the time the feed was modified the last time. Mustbe a <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(在 Python v3.5)"><tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt></a><span class="link-target"> [http://docs.python.org/dev/library/datetime.html#datetime.datetime]</span> object. If notpresent the latest entry's <cite>updated</cite> is used.</li><li><strong>feed_url</strong> – the URL to the feed. Should be the URL that wasrequested.</li><li><strong>author</strong> – the author of the feed. Must be either a string (thename) or a dict with name (required) and uri oremail (both optional). Can be a list of (may bemixed, too) strings and dicts, too, if there aremultiple authors. Required if not every entry has anauthor element.</li><li><strong>icon</strong> – an icon for the feed.</li><li><strong>logo</strong> – a logo for the feed.</li><li><strong>rights</strong> – copyright information for the feed.</li><li><strong>rights_type</strong> – the type attribute for the rights element. One of<tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>. Default is<tt class="docutils literal"><span class="pre">'text'</span></tt>.</li><li><strong>subtitle</strong> – a short description of the feed.</li><li><strong>subtitle_type</strong> – the type attribute for the subtitle element.One of <tt class="docutils literal"><span class="pre">'text'</span></tt>, <tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt>or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>. Default is <tt class="docutils literal"><span class="pre">'text'</span></tt>.</li><li><strong>links</strong> – additional links. Must be a list of dictionaries withhref (required) and rel, type, hreflang, title, length(all optional)</li><li><strong>generator</strong> – the software that generated this feed. This must bea tuple in the form <tt class="docutils literal"><span class="pre">(name,</span> <span class="pre">url,</span> <span class="pre">version)</span></tt>. Ifyou don't want to specify one of them, set the itemto <cite>None</cite>.</li><li><strong>entries</strong> – a list with the entries for the feed. Entries can alsobe added later with <a class="reference internal" href="#werkzeug.contrib.atom.AtomFeed.add" title="werkzeug.contrib.atom.AtomFeed.add"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a>.</li></ul></td></tr></tbody></table>
For more information on the elements see[http://www.atomenabled.org/developers/syndication/](http://www.atomenabled.org/developers/syndication/)
Everywhere where a list is demanded, any iterable can be used.
add(**args*, ***kwargs*)
Add a new entry to the feed. This function can either be calledwith a [FeedEntry](# "werkzeug.contrib.atom.FeedEntry") or some keyword and positional argumentsthat are forwarded to the [FeedEntry](# "werkzeug.contrib.atom.FeedEntry") constructor.
generate()
Return a generator that yields pieces of XML.
get_response()
Return a response object for the feed.
to_string()
Convert the feed into a string.
*class *werkzeug.contrib.atom.FeedEntry(*title=None*, *content=None*, *feed_url=None*, ***kwargs*)
Represents a single entry in a feed.
<table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple"><li><strong>title</strong> – the title of the entry. Required.</li><li><strong>title_type</strong> – the type attribute for the title element. One of<tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>.</li><li><strong>content</strong> – the content of the entry.</li><li><strong>content_type</strong> – the type attribute for the content element. Oneof <tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>.</li><li><strong>summary</strong> – a summary of the entry's content.</li><li><strong>summary_type</strong> – the type attribute for the summary element. Oneof <tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>.</li><li><strong>url</strong> – the url for the entry.</li><li><strong>id</strong> – a globally unique id for the entry. Must be an URI. Ifnot present the URL is used, but one of both is required.</li><li><strong>updated</strong> – the time the entry was modified the last time. Mustbe a <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(在 Python v3.5)"><tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt></a><span class="link-target"> [http://docs.python.org/dev/library/datetime.html#datetime.datetime]</span> object. Required.</li><li><strong>author</strong> – the author of the entry. Must be either a string (thename) or a dict with name (required) and uri oremail (both optional). Can be a list of (may bemixed, too) strings and dicts, too, if there aremultiple authors. Required if the feed does not have anauthor element.</li><li><strong>published</strong> – the time the entry was initially published. Mustbe a <a class="reference external" href="http://docs.python.org/dev/library/datetime.html#datetime.datetime" title="(在 Python v3.5)"><tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt></a><span class="link-target"> [http://docs.python.org/dev/library/datetime.html#datetime.datetime]</span> object.</li><li><strong>rights</strong> – copyright information for the entry.</li><li><strong>rights_type</strong> – the type attribute for the rights element. One of<tt class="docutils literal"><span class="pre">'html'</span></tt>, <tt class="docutils literal"><span class="pre">'text'</span></tt> or <tt class="docutils literal"><span class="pre">'xhtml'</span></tt>. Default is<tt class="docutils literal"><span class="pre">'text'</span></tt>.</li><li><strong>links</strong> – additional links. Must be a list of dictionaries withhref (required) and rel, type, hreflang, title, length(all optional)</li><li><strong>categories</strong> – categories for the entry. Must be a list of dictionarieswith term (required), scheme and label (all optional)</li><li><strong>xml_base</strong> – The xml base (url) for this feed item. If not providedit will default to the item url.</li></ul></td></tr></tbody></table>
For more information on the elements see[http://www.atomenabled.org/developers/syndication/](http://www.atomenabled.org/developers/syndication/)
Everywhere where a list is demanded, any iterable can be used.