model.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » examples » beaker_caching » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » examples » beaker_caching » model.py
"""Model.   We are modeling Person objects with a collection
of Address objects.  Each Address has a PostalCode, which 
in turn references a City and then a Country:

Person --(1..n)--> Address
Address --(has a)--> PostalCode
PostalCode --(has a)--> City
City --(has a)--> Country

"""
from sqlalchemy import Column,Integer,String,ForeignKey
from sqlalchemy.orm import relationship
from meta import Base,FromCache,Session,RelationshipCache

class Country(Base):
    __tablename__ = 'country'

    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    
    def __init__(self, name):
        self.name = name

class City(Base):
    __tablename__ = 'city'

    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    country_id = Column(Integer, ForeignKey('country.id'), nullable=False)
    country = relationship(Country)

    def __init__(self, name, country):
        self.name = name
        self.country = country

class PostalCode(Base):
    __tablename__ = 'postal_code'

    id = Column(Integer, primary_key=True)
    code = Column(String(10), nullable=False)
    city_id = Column(Integer, ForeignKey('city.id'), nullable=False)
    city = relationship(City)
    
    @property
    def country(self):
        return self.city.country
        
    def __init__(self, code, city):
        self.code = code
        self.city = city
    
class Address(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    person_id = Column(Integer, ForeignKey('person.id'), nullable=False)
    street = Column(String(200), nullable=False)
    postal_code_id = Column(Integer, ForeignKey('postal_code.id'))
    postal_code = relationship(PostalCode)
    
    @property
    def city(self):
        return self.postal_code.city
        
    @property
    def country(self):
        return self.postal_code.country
    
    def __str__(self):
        return "%s\t"\
              "%s, %s\t"\
              "%s" % (self.street, self.city.name, 
                self.postal_code.code, self.country.name)
        
class Person(Base):
    __tablename__ = 'person'

    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    addresses = relationship(Address, collection_class=set)

    def __init__(self, name, *addresses):
        self.name = name
        self.addresses = set(addresses)

    def __str__(self):
        return self.name
    
    def __repr__(self):
        return "Person(name=%r)" % self.name
        
    def format_full(self):
        return "\t".join([str(x) for x in [self] + list(self.addresses)])
        
# Caching options.   A set of three RelationshipCache options
# which can be applied to Query(), causing the "lazy load"
# of these attributes to be loaded from cache.
cache_address_bits = RelationshipCache("default", "byid", PostalCode.city).\
                and_(
                    RelationshipCache("default", "byid", City.country)
                ).and_(
                    RelationshipCache("default", "byid", Address.postal_code)
                )

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.