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
On Tuesday, July 3, 2018 at 7:39:04 PM UTC+2, Dieter Van Eessen wrote:
>
> 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]
>> 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/93b2debe-dcf9-4cf4-9b09-8905a86e784b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.