I've been dealing with similar issues on my first-ever Pyramid
project, also using sqlalchemy and traversal.

This may be indirection overkill, but I separated Context objects from
Data objects. Note this is a reporting application, so it's more into
displaying the results of queries than in interacting with individual
objects in the model. The Context objects organize the tree and
queries the database.

# in resources package

# sqlalchemy declarative classes
from orm import Site
from orm import SiteMonth

DBSession =
scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

class Context(object):

    __name__ = None
    __parent__ = None
    children = {}

    def __getitem__(self, name):
        return self.children[name]

    def add_child(self, child, name):
        child.__name__ = name
        child.__parent__ = self
        self.children[name] = child


class Root(Context):

    def get_sites(self, like=None):
        session = DBSession()
        query = session.query(Site).order_by(Site.host)
        if like:
            if '%' not in like:
                like += '%'
            query = query.filter(Site.host.like(like))
        return iter(query)


class Report(Context):

    def get_report(self, hosts=None, startdate=None, enddate=None):
        session = DBSession()
        query = session.query(SiteMonth).order_by(SiteMonth.host,
SiteMonth.month)
        if hosts:
            query = query.filter(SiteMonth.host.in_(hosts))
        if startdate:
            query = query.filter(SiteMonth.month >= startdate)
        if enddate:
            query = query.filter(SiteMonth.month <= enddate)
        return iter(query)

root = Root()
root.add_child(Report(), 'report')

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to