from zope.interface import implementer, Interface

from axiom import item, store
from axiom.attributes import text, timestamp
from epsilon.extime import Time

class IPostit(Interface):
    def getPosts(how_many):
        """Retrieve 'how_many' posts from the database."""
    
    def getPost(id):
        """Retrieve the post 'id' from the database"""

class Post(item.Item):
    title = text()
    url = text()
    content = text()
    author = text()
    created = timestamp()

@implementer(IPostit)
class Application(item.Item, item.InstallableMixin):
    name = text()

    def installOn(self, other):
        super(Application, self).installOn(other)
        other.powerUp(self, IPostit)

    def getPosts(self, how_many = None):
        return self.store.query(Post, limit=how_many, sort=Post.created.descending)

    def getOne(self, id):
        return self.store.getItemByID(id)

def initialize():
    s = store.Store('postit.axiom')
    postit = IPostit(s, None)
    if not postit:
        Application(store=s, name='Postit').installOn(s)
        Post(store=s, 
             title="This is the title",
             url="http://www.divmod.org",
             content="Here is the content for the link",
             author="dialtone",
             created=Time())
    return s
