I actually have the same question with the additional twist that I do not
know how long the path will be.
example url: '/bar/baz/8' or '/foo/3'. For any of these urls the resource
/bar/baz or /foo can be found through traversal,
but the last part of the url (8, 3 or any number) should be view_name.
Currently trying to solve this by catching the KeyError in __getitem__ and
manipulating the resource if key.isdigit().
But I would like to move manipulation of resource/model to another place.
Model/Resource is the abstration of data in the database,
therefore manipulation of resources after you've fetched them based on info
in url should be done elsewhere. (in my opinion)
I know it's an old topic, perhaps anyone may lead me to a newer solution
(google is your friend, but it only helps you when you already know what
you're looking for :)
Kind regards,
Dieter
On Wednesday, March 12, 2014 at 12:06:12 AM UTC+1, Tres Seaver wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 03/11/2014 10:49 AM, Ron Drongowski wrote:
> > We are creating a traversal based pyramid-app using our ZCA-based
> > components we designed for our CMS. So for a regular article an url
> > would look like this: /path/to/my_article. Traversal ends and we can
> > register a default view (name=''). This is easy. But, since we have a
> > lot of SEOs around us, we have very specific needs for custom-urls. We
> > were asked ro realize the scheme /path/to/my_article/page-2 for the
> > second page of an article. This would lead to an dynamic view_name,
> > which is currently not possible - even in an hybrid application, if I
> > understood the documentation right. I assume you could solve this
> > problem by either registering a lot of views, extend the traversal
> > (implement an __getitem__ for the article) or implement your own
> > request factory. All of which do not seem to be the right thing, while
> > a regex view-name seems to be th eright thing. Is there a solution for
> > this?
>
> You need 'request.subpath'[2]::
>
> @view_config(context=MyModel, renderer='templates/mytemplate.pt')
> def my_view(request):
> end = request.subpath[-1:]
> if end and end[0].startswith('page-'):
> page = int(end[0].split('-')[1])
> else:
> page = None
> pages = []
> url = request.resource_url(request.context)
> for i in range(10):
> page_url = '%spage-%d' % (url, i)
> pages.append((i, page_url))
> return {'project': 'ugh',
> 'page': page,
> 'pages': pages,
> }
>
>
> This would match for URLs such as '/@@/page-1' on the "ZODB starter" app.
>
> If you want to avoid the '@@' element in the visible path, you can add
> it via a new-request subscriber[2]::
>
> from pyramid.events import NewRequest
> from pyramid.events import subscriber
>
> @subscriber(NewRequest)
> def mysubscriber(event):
> r = event.request
> last = r.path_info.rsplit('/', 1)[1]
> if last.startswith('page-'):
> page = last.split('-')[1]
> r.path_info = r.path_info[:-7] + '@@' + '/page-%s' % page
>
>
>
> [1]
>
> http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.subpath
>
>
> [2]
>
> http://docs.pylonsproject.org/projects/pyramid/en/latest/api/events.html#pyramid.events.subscriber
>
>
>
> Tres.
> - --
> ===================================================================
> Tres Seaver +1 540-429-0999 [email protected]
> <javascript:>
> Palladion Software "Excellence by Design" http://palladion.com
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iEYEARECAAYFAlMfluQACgkQ+gerLs4ltQ73gwCg3Gn44Z9BuFnGdLTZzUr8q+y8
> o1QAoMI2oW0jlzYRgtyl8Gs8pjMUKMjP
> =gIOO
> -----END PGP SIGNATURE-----
>
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/ab69beda-74bd-4dff-b271-8d7af03e0f65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.