Example #1
0
    def test_basic(self):
        class Address(Entity):
            street = Field(String(130))
            city = Field(String(100))
            using_options(shortnames=True)

        class Comment(Entity):
            id = Field(Integer, primary_key=True)
            name = Field(String(200))
            text = Field(Text)

        is_addressable = associable(Address, 'addresses')
        is_commentable = associable(Comment, 'comments')

        class Person(Entity):
            id = Field(Integer, primary_key=True)
            name = Field(String(50))
            orders = OneToMany('Order')
            using_options(shortnames=True)
            is_addressable()
            is_commentable()

        class Order(Entity):
            order_num = Field(Integer, primary_key=True)
            item_count = Field(Integer)
            person = ManyToOne('Person')
            using_options(shortnames=True)
            is_addressable('address', uselist=False)

        setup_all(True)

        home = Address(street='123 Elm St.', city='Spooksville')
        work = Address(street='243 Hooper st.', city='Cupertino')
        user = Person(name='Jane Doe')
        user.addresses.append(home)
        user.addresses.append(work)

        neworder = Order(item_count=4)
        neworder.address = home
        user.orders.append(neworder)

        session.commit()
        session.expunge_all()

        # Queries using the added helpers
        people = Person.select_by_addresses(city='Cupertino')
        assert len(people) == 1

        streets = [adr.street for adr in people[0].addresses]
        assert '243 Hooper st.' in streets
        assert '123 Elm St.' in streets

        people = Person.select_addresses(and_(Address.street=='132 Elm St',
                                              Address.city=='Smallville'))
        assert len(people) == 0
Example #2
0
    def test_empty(self):
        class Foo(Entity):
            pass

        class Bar(Entity):
            pass

        is_fooable = associable(Foo)
        is_barable = associable(Bar)

        class Quux(Entity):
            is_fooable()
            is_barable()

        setup_all(True)
Example #3
0
    def test_with_forward_ref(self):
        class Checkout(Entity):
            by = ManyToOne('Villian', ondelete='cascade')
            stamp = Field(DateTime)

        can_checkout = associable(Checkout, 'checked_out')

        class Article(Entity):
            title = Field(String(200))
            content = Field(Text)
            can_checkout('checked_out_by', uselist=False)
            using_options(tablename='article')

        class Villian(Entity):
            name = Field(String(50))
            using_options(tablename='villian')

        setup_all(True)

        art = Article(title='Hope Soars')

        session.commit()
        session.expunge_all()
Example #4
0
#
#  For use of this library in commercial applications, please contact
#  [email protected]
#
#  ============================================================================
from camelot.model import metadata
from elixir.entity import Entity
from elixir.options import using_options
from elixir.fields import Field
from sqlalchemy.types import Unicode, Integer, DateTime
from elixir.ext.associable import associable
"""Functionallity to synchronize elements from the camelot database against
other databases
"""

__metadata__ = metadata

import datetime

class Synchronized( Entity ):
    using_options( tablename = 'synchronized' )
    database = Field( Unicode( 30 ), index = True )
    tablename = Field( Unicode( 30 ), index = True )
    primary_key = Field( Integer(), index = True )
    last_update = Field( DateTime(), index = True,
                          default = datetime.datetime.now,
                           onupdate = datetime.datetime.now )

is_synchronized = associable( Synchronized )