def _generate(self): now = datetime.utcnow() posts = iter(Post.query.order_by(Post.last_update.desc())) try: first_post = posts.next() last_update = first_post.last_update posts = chain((first_post,), posts) except StopIteration: first_post = None last_update = now feed_id = build_tag_uri(self.app, last_update, 'zxa_export', 'full') yield (XML_PREAMBLE % { 'version': escape(rezine.__version__), 'title': escape(self.app.cfg['blog_title']), 'subtitle': escape(self.app.cfg['blog_tagline']), 'atom_ns': ATOM_NS, 'rezine_ns': ZINE_NS, 'id': escape(feed_id), 'blog_url': escape(self.app.cfg['blog_url']), 'updated': format_iso8601(last_update), 'language': self.app.cfg['language'] }).encode('utf-8') def dump_node(node): return etree.tostring(node, encoding='utf-8') for participant in self.participants: participant.before_dump() # dump configuration cfg = self.z('configuration') for key, value in self.app.cfg.export(): self.z('item', key=key, text=value, parent=cfg) yield dump_node(cfg) # allow plugins to dump trees for participant in self.participants: rv = participant.dump_data() if rv is not None: yield dump_node(rv) # look up all the users and add them as dependencies if they # have written a comment or created a post. for user in User.query.all(): if user.posts.count() > 0 or user.comments.count() > 0: self._register_user(user) # dump all the posts for post in posts: yield dump_node(self._dump_post(post)) # if we have dependencies (very likely) dump them now if self._dependencies: yield '<rezine:dependencies>' for node in self._dependencies.itervalues(): yield dump_node(node) yield '</rezine:dependencies>' yield XML_EPILOG.encode('utf-8')
def __init__(self, title, author, text, slug=None, pub_date=None, last_update=None, comments_enabled=True, pings_enabled=True, status=STATUS_PUBLISHED, parser=None, uid=None, content_type='entry', extra=None): app = get_application() self.content_type = content_type self.title = title self.author = author if parser is None: parser = app.cfg['default_parser'] self.parser = parser self.text = text or u'' if extra: self.extra = dict(extra) else: self.extra = {} self.comments_enabled = comments_enabled self.pings_enabled = pings_enabled self.status = status # set times now, they depend on status being set self.touch_times(pub_date) if last_update is not None: self.last_update = last_update # now bind the slug for which we need the times set. self.bind_slug(slug) # generate a UID if none is given if uid is None: uid = build_tag_uri(app, self.pub_date, content_type, self.slug) self.uid = uid
def populate_feed(req, feed, author=None, year=None, month=None, day=None, category=None, tag=None, post=None): """Renders an atom feed requested. :URL endpoint: ``blog/atom_feed`` """ # the feed only contains published items query = Post.query.lightweight(lazy=('comments', )).published() # feed for a category if category is not None: category = Category.query.filter_by(slug=category).first(True) query = query.filter(Post.categories.contains(category)) # feed for a tag if tag is not None: tag = Tag.query.filter_by(slug=tag).first(True) query = query.filter(Post.tags.contains(tag)) # feed for an author if author is not None: author = User.query.filter_by(username=author).first(True) query = query.filter(Post.author == author) # feed for dates if year is not None: query = query.for_index().date_filter(year, month, day) # if no post slug is given we filter the posts by the cretereons # provided and pass them to the feed builder. This will only return # a feed for posts with a content type listed in `index_content_types` if post is None: for post in query.for_index().order_by(Post.pub_date.desc()) \ .limit(15).all(): alt_title = '%s @ %s' % (post.author.display_name, post.pub_date) feed.add_item(post.title or alt_title, url_for(post, _external=True), unicode(post.body), author_name=post.author.display_name, pubdate=post.pub_date, unique_id=post.uid) # otherwise we create a feed for all the comments of a post. # the function is called this way by `dispatch_content_type`. else: comment_num = 1 for comment in post.comments: if not comment.visible: continue uid = build_tag_uri(req.app, comment.pub_date, 'comment', comment.id) title = _(u'Comment %(num)d on %(post)s') % { 'num': comment_num, 'post': post.title } author = {'name': comment.author} if comment.www: author['uri'] = comment.www feed.add_item(title, url_for(comment, _external=True), unicode(comment.body), author_name=author, pubdate=comment.pub_date, unique_id=uid) comment_num += 1 return feed.writeString('utf-8')
def populate_feed(req, feed, author=None, year=None, month=None, day=None, category=None, tag=None, post=None): """Renders an atom feed requested. :URL endpoint: ``blog/atom_feed`` """ # the feed only contains published items query = Post.query.lightweight(lazy=('comments',)).published() # feed for a category if category is not None: category = Category.query.filter_by(slug=category).first(True) query = query.filter(Post.categories.contains(category)) # feed for a tag if tag is not None: tag = Tag.query.filter_by(slug=tag).first(True) query = query.filter(Post.tags.contains(tag)) # feed for an author if author is not None: author = User.query.filter_by(username=author).first(True) query = query.filter(Post.author == author) # feed for dates if year is not None: query = query.for_index().date_filter(year, month, day) # if no post slug is given we filter the posts by the cretereons # provided and pass them to the feed builder. This will only return # a feed for posts with a content type listed in `index_content_types` if post is None: for post in query.for_index().order_by(Post.pub_date.desc()) \ .limit(15).all(): alt_title = '%s @ %s' % (post.author.display_name, post.pub_date) feed.add_item(post.title or alt_title, url_for(post, _external=True), unicode(post.body), author_name=post.author.display_name, pubdate=post.pub_date, unique_id=post.uid) # otherwise we create a feed for all the comments of a post. # the function is called this way by `dispatch_content_type`. else: comment_num = 1 for comment in post.comments: if not comment.visible: continue uid = build_tag_uri(req.app, comment.pub_date, 'comment', comment.id) title = _(u'Comment %(num)d on %(post)s') % { 'num': comment_num, 'post': post.title } author = {'name': comment.author} if comment.www: author['uri'] = comment.www feed.add_item(title, url_for(comment, _external=True), unicode(comment.body), author_name=author, pubdate=comment.pub_date, unique_id=uid) comment_num += 1 return feed.writeString('utf-8')