dynamic view_name to match in traversal based app

41 views
Skip to first unread message

Ron Drongowski

unread,
Mar 11, 2014, 10:49:24 AM3/11/14
to pylons-...@googlegroups.com
Hi,

I suppose this question has been answered somewhere but I could not find an explantion. 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?

Thanks a lot. Greetings, Ron

Michael Merickel

unread,
Mar 11, 2014, 5:14:59 PM3/11/14
to Pylons
Here's the initial discussion on this, and alternatives.



--
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 pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Tres Seaver

unread,
Mar 11, 2014, 7:06:12 PM3/11/14
to pylons-...@googlegroups.com
-----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 tse...@palladion.com
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-----

Dieter Van Eessen

unread,
Jul 3, 2018, 1:39:04 PM7/3/18
to pylons-discuss
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

Dieter Van Eessen

unread,
Jul 4, 2018, 1:46:42 AM7/4/18
to pylons-discuss
Alternative solution to the original problem:
replace url '/path/to/my-article/page-2' with '/path/to/my-article/page/2'. 'page' is view_name, 2 is subpath

Alternative solution to my problem:
Create own traverser inheriting from pyramid.traversal.ResourceTreeTraverser, using
'traversing = super().__call__(request)' and manipulating traversing['view_name'] if it isDigit()

Hope it's still relevant,
kind regards,
Dieter
Reply all
Reply to author
Forward
0 new messages