-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/30/2011 09:04 AM, Wade Leftwich wrote:
> If you want to stick with Traversal, don't forget that you have
> request.subpath available to you. For the path "/blog/archive/
> 2011/01/29/my-post-about-pyramid", you could have an Archive context
> whose default (unnamed) view would be called with a request that
> included request.subpath = ['2011', '01', '29', 'my-post-about-
> pyramid'].
Other traversal-based strategies:
- - an 'archive' view registered for the blog context (assuming you don't
otherwise need the 'archive' context).
- - add non-persistent placeholder objects to your app to take advantage
of traversal's use of '__getitem__'. This solution allows you write
views for the "transient" context classes, even though they aren't in
your database. E.g., something like:
- --------------------------- %< ---------------------
class Blog:
...
def __getitem__(self, key):
if key == 'archive':
return Archive(self)
return super(Blog, self).__getitem__(key)
class Archive:
__name__ = 'archive'
YEARS = range(2010, 2100)
def __init__(self, blog):
self.__parent__ = self.blog = blog
def __getitem__(self, key):
try:
year = int(key)
except TypeError:
raise KeyError(key)
if year not in self.YEARS:
raise KyeError(key)
return ArchiveYear(self, year)
class ArchiveYear:
__name__ = 'archive'
MONTHS = range(1, 12)
def __init__(self, archive, year):
self.__parent__ = archive
self.__name__ = str(year)
self.blog = archive.blog
self.year = year
def __getitem__(self, key):
try:
mnnth = int(key)
except TypeError:
raise KeyError(key)
# or, return self.blog.entries[key]
if month not in self.MONTHS:
raise KeyError(key)
return ArchiveMonth(self, month)
class ArchiveMonth:
__name__ = 'archive'
MONTHS = range(1, 12)
def __init__(self, year, month):
self.__parent__ = year
self.__name__ = str(month)
self.blog = year.blog
self.month = month
def __getitem__(self, key):
return self.blog.entries[key]
- --------------------------- %< ---------------------
> The view would then call a method on the Archive context using
> arguments constructed from the request.subpath. The Archive would in
> turn make a single call to Mongo. A path like '/blog/archive/2011/01'
> would require some logic in the view, to choose a different Archive
> method and a different template to show a listing of posts for
> January, or perhaps redirect to /blog/month/2011/01 .
>
> So if you are using Traversal in this way, the logic for parsing the
> path lives in the view and/or the context. With URL Dispatch it's in
> the configuration.
Agreed. I find the traversal stuff much easier to test on this account.
The fact that I find it easier to think about is likely due to the warp
in my brain from years of heavy Zope usage. ;)
Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 [email protected]
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk1FjgoACgkQ+gerLs4ltQ4fuwCdE04ygTijKNcWg6mLsLK8Ruq3
ewUAoL+DDXtoQk9+idz4PrwS0E2DaQbJ
=g74T
-----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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-devel?hl=en.