view FeedList.py @ 72:e8c2730eb444

control the display of unread feeds/all feeds via Preferences
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 11 Aug 2010 03:23:02 +0200
parents 228da10cc98b
children 5585f3d23541
line wrap: on
line source


from Feed import Feed
from FeedEntry import FeedEntry
from Preferences import Preferences
from sqlalchemy.orm import joinedload

def getFeeds(session):
    preferences = Preferences(session)
    if preferences.showOnlyUnreadFeeds():
        return UnreadFeeds(session).getFeeds()
    else:
        return AllFeeds(session).getFeeds()

class AllFeeds(object):
    '''A simple feed list that returns all feeds stored in the database'''

    def __init__(self, session):
        self.session = session
    
    def getFeeds(self):
        return Feed.all(self.session)
    
class UnreadFeeds(object):
    '''A feed list that returns only Feed objects that have unread entries'''
    
    def __init__(self, session):
        self.session = session
        
    def getFeeds(self):
        query = self.session.query(FeedEntry).filter(FeedEntry.read == 0)
        queryWithOptions = query.options(joinedload("feed"))
        result = queryWithOptions.all()
        return self._collectFeeds(result)
    
    def _collectFeeds(self, feedEntries):
        feeds = [entry.feed for entry in feedEntries]
        uniqueFeeds = set(feeds)
        return list(uniqueFeeds)