"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/news.py $
$Id: news.py 30485 2008-01-21 21:06:41Z dbinger $
"""
from dulcinea.base import DulcineaPersistent
from dulcinea.timestamped import Timestamped, reverse_timestamp_sorted
from qp.lib.keep import Keyed, Keep
from qp.lib.spec import specify, spec, add_getters_and_setters, string
from qp.pub.common import get_publisher

def get_news_db():
    return get_publisher().get_connection().get_root().get('news_db')

class NewsItem(Keyed, Timestamped, DulcineaPersistent):
    """
    A single announcement.
    """
    title_is = spec(
        string,
        "Title of this announcement.")
    subtitle_is = spec(
        string,
        "A subtitle that can be shown with the link text, but isn't required.")
    text_is = spec(
        string,
        "HTML text of this announcement.  This field will not be escaped.")

    def __init__ (self):
        Keyed.__init__(self)
        Timestamped.__init__(self)
        specify(self, title='', subtitle='', text='')

add_getters_and_setters(NewsItem)

class NewsDatabase(Keep, DulcineaPersistent):

    def __init__(self):
        Keep.__init__(self, value_spec=NewsItem)

    def delete_news_item(self, item):
        """(item : NewsItem)
        """
        del self.mapping[item.get_key()]

    def get_news_item(self, key):
        return self.mapping[key]

    def get_news_items(self):
        return list(self.mapping.values())

    def get_sorted_news_items(self):
        return reverse_timestamp_sorted(self.mapping.values())
