"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/news.py $
$Id: news.py 25462 2004-10-26 19:15:15Z dbinger $
"""
from dulcinea.base import DulcineaPersistent
from dulcinea.keyed import Keyed, KeyedMap
from dulcinea.timestamped import Timestamped, reverse_timestamp_sorted


class NewsItem(Keyed, Timestamped, DulcineaPersistent):
    """
    A single announcement.

    Instance attributes:
      title : string
        Title of this announcement.
      subtitle : string
        A subtitle that can be shown with the link text, but isn't required.
      text : string
        HTML text of this announcement.  This field will not be escaped.
    """

    title_is = str
    subtitle_is = str
    text_is = str

    def __init__ (self):
        Keyed.__init__(self)
        Timestamped.__init__(self)
        self.title = ""
        self.subtitle = ""
        self.text = ""

    def get_title(self):
        return self.title

    def get_subtitle(self):
        return self.subtitle

    def get_text(self):
        return self.text

    def set_title (self, title):
        self.title = title

    def set_subtitle(self, subtitle):
        self.subtitle = subtitle

    def set_text (self, text):
        self.text = text


class NewsDatabase(KeyedMap, DulcineaPersistent):
    """
    Instance attributes: none
    """

    def __init__(self):
        KeyedMap.__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 self.mapping.values()

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