Fix item orders and use integrated limit operation;

This commit is contained in:
Benjamin Bouvier 2018-04-27 11:41:30 +02:00
parent ea79940e1c
commit bd3dbc37ea

13
main.py
View File

@ -7,7 +7,7 @@ from flask import Flask, request, abort
from feedgen.feed import FeedGenerator
from pony import orm
from pony.orm import Required, Optional, Set
from pony.orm import Required, Optional, Set, desc
config = configparser.ConfigParser()
config.read('settings.ini')
@ -146,8 +146,11 @@ def by_feed_name(slug):
gen.link(href=feed.atom_id)
gen.description(feed.description)
count = 0
for item in feed.items.order_by(Item.date):
# Take the items by reversed date order. When adding them in the RSS feed,
# order will be reversed again (first in array will end up last in the
# feed), so explicitly reverse one more time.
items = reversed(feed.items.order_by(desc(Item.date))[:20])
for item in items:
fe = gen.add_entry()
fe.id(item.atom_id)
fe.title(item.title)
@ -156,10 +159,6 @@ def by_feed_name(slug):
date = item.date.replace(tzinfo=timezone.utc)
fe.published(date)
count += 1
if count == 20:
break
return gen.rss_str(pretty=True).decode('utf8')