-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/26/2011 06:13 AM, Wade Leftwich wrote:
> 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')
>
I use a couple nested resources that require a url namespace in my
application, so I've been using a container pattern.

class FooContainer(object):
 
    __name__ = 'foo'
 
    def __getitem__(self, key):
        #logic to return and own a foo sqlalchemy object or keyerror

class BarContainer(object):
 
    __name__ = 'bar'

    def __getitem__(self, key):
        #logic to return and own a bar sqlalchemy object or keyerror

class Foo(Base):

    _allowed_children = {'bar': BarContainer}
    __tablename__ = 'foo'
    ...
 
    def __getitem__(self, key):
        # below isn't the real logic; it's more dynamic, but it shows
what's going on.
        container = self._allowed_children[key]()
        container.__parent__ = self
        return container

This would allow for a url like example.com/foo/2/bar/12 and has been
working out well for the most part. I can see where it can go wrong,
but I've been working on making it a bit better.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJNQHdrAAoJEOB+qo9S/43onYYH/ildhlaAb5f2j/Le9Uj8Fybe
1ld17FnVEZ8DMfYORzDTVpfIBi45TqZKTfLC9udCeQAtoA98xhRaaN2VCMWxghGq
V1BVfYOgTrt218+GrteSKXpq7L7a+u9HuoBQb487e5FLY2b2vtdI55OXlI5/o34a
mcz+taZR8T6uTxTiZyLMPBg9jCMA8UEd34o7F5jbaDDXWI3W7mIB5notIFu6wTtI
O/LuAPSo2y0/TBVRy1EKwYIMIyGox4ffMABIidoGgzxEnuRHIaGkh8qthIj5Xeb2
s0rrO/xKv+nUtNblxfnDmYiWUfGg35OkZBlKtuYpyqy8eAtb5notWQqDQWF3Nho=
=otuo
-----END PGP SIGNATURE-----

-- 
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