Beispiel #1
0
def lang_feed(lang):
    """generate atom feed for blog entries in the specific language"""
    blog_entries = db.session.query(BlogEntry).filter_by(lang = lang).order_by(BlogEntry.created.desc()).limit(PAGE_SIZE)
    updated = blog_entries.first().created if blog_entries.count() > 0 else ''

    etag = '"{0}"'.format(hashlib.sha256(str(updated)).hexdigest())
    if request.headers.get('If-None-Match', '') == etag:
        return '', 304
    last_modified = format_date_time(mktime(updated.timetuple()))
    if request.headers.get('If-Modified-Since', '') == last_modified:
        return '', 304

    title = app.config['BLOG_OWNER'] + "'s Thoughts and Writings"
    if lang == 'cn':
        title = app.config['BLOG_OWNER'] + u'的博客'
    feed = AtomFeed(title = title, 
                    url = request.url_root, 
                    feed_url = request.url, 
                    author = app.config['BLOG_OWNER'])
    if updated:
        feed.updated = updated
    for blog_entry in blog_entries:
        feed.add(title = blog_entry.title, 
                 content = unicode(blog_entry.content),
                 content_type = 'html',
                 author = app.config['BLOG_OWNER'],
                 url = urljoin(request.url_root, url_for('blog_entry.read', uid = blog_entry.uid)),
                 updated = blog_entry.updated,
                 published = blog_entry.created)
    
    response = feed.get_response()
    response.headers['ETag'] = etag
    response.headers['Last-Modified'] = last_modified
    return response