from taggable   import acts_as_taggable, Taggable
from elixir     import Entity, Unicode, has_field, using_options, \
                       metadata, create_all, objectstore, options

import sqlalchemy

options.options_defaults['shortnames'] = True

class Person(Entity):
    has_field('name', Unicode(64))
    acts_as_taggable()
    
    def __repr__(self):
        return 'Person: %s' % self.name


class Movie(Entity):
    has_field('title', Unicode(64))
    acts_as_taggable()
    
    def __repr__(self):
        return 'Movie: %s' % self.title


if __name__ == '__main__':
    engine = sqlalchemy.create_engine('sqlite:///')
    metadata.connect(engine)
    create_all()
    
    # create three people and two movies
    p1 = Person(name='Jonathan LaCour')
    p2 = Person(name='Lacey LaCour')
    p3 = Person(name='Celine Dion')
    m1 = Movie(title='Batman Begins')
    m2 = Movie(title='Bambi')
    
    objectstore.flush()
    objectstore.clear()
    
    # tag the movies and people as either cool or uncool
    p1 = Person.select_by(name='Jonathan LaCour')[0]
    p2 = Person.select_by(name='Lacey LaCour')[0]
    p3 = Person.select_by(name='Celine Dion')[0]
    m1 = Movie.select_by(title='Batman Begins')[0]
    m2 = Movie.select_by(title='Bambi')[0]
    p1.add_tag('cool')
    p2.add_tag('cool')
    p3.add_tag('uncool')
    m1.add_tag('cool')
    m2.add_tag('uncool')
    
    objectstore.flush()
    objectstore.clear()
    
    # try fetching just one kind of object by its tag
    cool = Person.get_by_tag('cool')
    for person in cool: print 'Cool:', person.name
    print
    uncool = Person.get_by_tag('uncool')
    for person in uncool: print 'Uncool:', person.name
    
    # try fetching all objects by a tag
    print
    print 'All Cool Items'
    print '--------------'
    all_cool = Taggable.get('cool')
    for cool in all_cool:
        print cool
    
    # try fetching all objects, quickly by a tag
    print
    print 'All Cool Items (Fast)'
    print '---------------------'
    all_cool = Taggable.get_fast('cool')
    for cool in all_cool:
        print cool
    
    # try fetching only Person objects through the Taggable object
    print
    cool = Taggable.get('cool', of_kind=Person)
    for person in cool: print 'Cool:', person.name