Discussion:
dynamic view_name to match in traversal based app
Ron Drongowski
2014-03-11 14:49:24 UTC
Permalink
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
--
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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.
Michael Merickel
2014-03-11 21:14:59 UTC
Permalink
Here's the initial discussion on this, and alternatives.

https://github.com/Pylons/pyramid/issues/881
Post by Ron Drongowski
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
--
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
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.
--
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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.
Tres Seaver
2014-03-11 23:06:12 UTC
Permalink
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 tseaver-npLdOuuzvjyaMJb+***@public.gmane.org
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 pylons-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to pylons-discuss-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.
Dieter Van Eessen
2018-07-03 17:01:13 UTC
Permalink
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
Post by Tres Seaver
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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?
@view_config(context=MyModel, renderer='templates/mytemplate.pt')
end = request.subpath[-1:]
page = int(end[0].split('-')[1])
page = None
pages = []
url = request.resource_url(request.context)
page_url = '%spage-%d' % (url, i)
pages.append((i, page_url))
return {'project': 'ugh',
'page': page,
'pages': pages,
}
from pyramid.events import NewRequest
from pyramid.events import subscriber
@subscriber(NewRequest)
r = event.request
last = r.path_info.rsplit('/', 1)[1]
page = last.split('-')[1]
[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.
- --
===================================================================
<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 pylons-discuss+***@googlegroups.com.
To post to this group, send email to pylons-***@googlegroups.com.
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.
Dieter Van Eessen
2018-07-04 05:46:42 UTC
Permalink
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
Post by Dieter Van Eessen
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
Post by Tres Seaver
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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?
@view_config(context=MyModel, renderer='templates/mytemplate.pt')
end = request.subpath[-1:]
page = int(end[0].split('-')[1])
page = None
pages = []
url = request.resource_url(request.context)
page_url = '%spage-%d' % (url, i)
pages.append((i, page_url))
return {'project': 'ugh',
'page': page,
'pages': pages,
}
from pyramid.events import NewRequest
from pyramid.events import subscriber
@subscriber(NewRequest)
r = event.request
last = r.path_info.rsplit('/', 1)[1]
page = last.split('-')[1]
[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.
- --
===================================================================
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 pylons-discuss+***@googlegroups.com.
To post to this group, send email to pylons-***@googlegroups.com.
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.
Loading...