Thanks Rob,

I'll look into using the predicates to narrow down the traversal.
I guess in the future there will be higher-level wrappers built
into pyramid to hide the resource tree etc when it is only
used for URL matching.

P.S The reason I'm not using URL dispatch is because my problem
is not properly defined yet. Traversal seems more powerful than
URL dispatch even though I can't fully yet appreciate/comprehend
using traversal as a persistent data store, so better safe than sorry.

Happy New Year!


On 31 December 2010 05:23, Rob Miller <[email protected]> wrote:
> On 12/30/10 6:43 AM, Burak Cankurtaran wrote:
>>
>> Hi,
>>
>> I'm new to python and pyramid. I am currently trying to get traversal
>> working. I started with
>> the "pyramid_starter" project template. Currently there is the default
>> view generated by the
>> template and a Welcome view I created. Question at bottom of post.
>>
>> The views.py module:
>>
>> #########
>> from pyramid.response import Response
>>
>> def my_view(request):
>>     return {'project':'test'}
>>
>> def welcome(request):
>>     return Response("Welcome")
>> #######
>>
>> My resource tree is generated in the resources.py module:
>>
>> #######
>> class Resource(dict):
>>     pass
>>
>> root = Resource({'Welcome': Resource()})
>>
>> def get_root(request):
>>     return root
>>
>> #######
>>
>> and the views are configured in the __init__ module as:
>>
>> #######
>> from pyramid.config import Configurator
>> from test.resources import get_root, Resource
>>
>> def main(global_config, **settings):
>>     """ This function returns a Pyramid WSGI application.
>>     """
>>     config = Configurator(root_factory=get_root, settings=settings)
>>
>>     config.add_view('test.views.my_view',
>>                     context='test.Resource',
>>                     renderer='test:templates/mytemplate.pt')
>
> if i'm understanding correctly, the context here should actually be
> 'test:resources.Resource', correct?
>
>>     config.add_view('test.views.welcome',
>>                     context='test:resources.Resource',
>>                     renderer='welcome.mak')
>>
>>     config.add_static_view('static', 'test:static')
>>     return config.make_wsgi_app()
>> ######
>>
>> I am having issues with the context of the welcome view configuration.
>> As you can see it is
>> defined as context='test:resources.Resource' which is the same as
>> my_view.
>>
>> I realise that the context must somehow reference the Welcome resource
>> in the resource tree.
>
> Actually, this isn't quite true.  A view context must be a class or an
> interface, not an instance.  Another way to put this is that, with the
> context predicate, you're registering views against particular _types_ of
> objects, not specific instances.
>
> Let's suppose that you do want to register the welcome view so that it only
> shows up on your Welcome object and no other one.  You've got a couple of
> options.  You could define a Welcome class::
>
>  class Welcome(Resource):
>      """Empty subclass used for view registration"""
>
> then you'd define your root like so::
>
>  root = Resource({'Welcome': Welcome()})
>
> and your view registration would look like this::
>
>  config.add_view('test.views.welcome',
>                  context='test:resources.Welcome',
>                  renderer='welcome.mak')
>
> That'd work, but if that's the only ``Welcome`` object you're going to have
> in the entire site, it smells a bit funny to be creating a subclass just for
> the view registration.  Luckily, you can use any of the other view
> predicates (see
> http://docs.pylonshq.com/pyramid/dev/narr/views.html#predicate-arguments).
>  In this case, you'd still use ``test:resources.Resource`` as the context.
>  But then you could use the ``path_info`` predicate to check that the
> request path starts with ``/Welcome``.  Or you could pass in a
> ``custom_predicates`` with a single callable that will simply check to see
> whether the context is the object you expect it to be.
>
>> However, I cannot use something like test:resources.Resource.Welcome
>> as Welcome is not
>> a member of the Resource class.
>>
>> Can somebody point out what I should do get this simple case working.
>> I am having difficulties
>> in constructing a proper and well defined resource tree. Maybe I don't
>> quite fully comprehend the
>> concept of a context.
>
> hopefully this helps,
>
> -r
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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-discuss?hl=en.

Reply via email to