On Jan 30, 3:56 pm, oO <oliv...@ozoux.com> wrote:
> Thanks for the info. I'm following up on the idea of using
> request.subpath, but at what point does this property get created?
> This is my understanding of the call stack:
>
> 1. the RootFactory is called, with the request object as a parameter
> 2. the RootFactory returns the root resource
> 3. Pyramid recursively calls the __getitem__ method on the resource
> until it runs out of parts or a resource returns a KeyError
> 4. ...
> 5. a view gets called with the request and context
>
> How do I access the request object from within the __getitem__ method
> of my resources? If I'm to dynamically return resource objects based
> on the subpath, then I need to somehow access the request object at
> that point
>
> Currently I'm storing the request object I got in step 1 it inside my
> RootFactory
>
> root_factory.request = request
>
> and every resource object is constructed with
>
> Resource.__name__
> Resource.__parent__
> Resource.root_factory = root_factory
>
> Which allows me to access the request object from inside the
> __getitem__ method, but I get an attribute error that request.subpath
> doesn't exist. Does it only get created later?
>
> I'm guessing that for the archive object to be the valid context and a
> view to be called, it should raise a KeyError  during traversal, but
> if I do this, I just get a 404 error without getting a view called
> that would be able to access the request.subpath property.
>
> Sorry if I'm creating more confusion on the subject.
>
> oO
>

Hello oO,

As you have discovered, the subpath is not easily available during
traversal, when the context objects are being chosen; it's the view
callable that gets the subpath, after traversal is finished.

I thought about it some more and, if it was me, I would follow the
"transient context classes" pattern described by Tres in this thread
today. Something like this:


class Root(dict):
    __name__ = None
    __parent__ = None


class Archive(object):
    def __init__(self, name=None, parent=None):
        self.__name__ = name
        self.__parent__ = parent

    def __getitem__(self, key):
        yearval = int(key)
        return Year(yearval=yearval, name=key, parent=self)

class Year(object):
    def __init__(self, yearval, name=None, parent=None):
        self.yearval = year
        self.__name__ = name
        self.__parent__ = parent

    def __getitem__(self, key):
        monthval = int(key)
        return Month(monthval=monthval, yearval=self.yearval,
name=key, parent=self)

    def listposts(self):
        """Default view might call this"""
        return ask_mongo_for_this_years_links(self.year)

class Month(object):
    def __init__(self, monthval, yearval, name=None, parent=None):
        self.monthval = monthval
        self.yearval = yearval
        self.__name__ = name
        self.__parent__ = parent

    def __getitem__(self, key):
        dayval = int(key)
        return Day(dayval=dayval, monthval=self.monthval,
yearval=self.yearval, name=key, parent=self)

    def listposts(self):
        """Default view might call this"""
        return ask_mongo_for_this_months_links(self.year, self.month)

class Day(object):
    def __init__(self, dayval, monthval, yearval, name=None,
parent=None):
        self.dayval = dayval
        self.monthval = monthval
        self.yearval = yearval
        self.__name__ = name
        self.__parent__ = parent

    def __getitem__(self, key):
        slug = key
        return Post(slug, dayval=self.dayval, monthval=self.monthval,
yearval=self.yearval, name=key, parent=self)

    def listposts(self):
        """Default view might call this"""
        return ask_mongo_for_this_days_links(self.year, self.month,
self.day)

class Post(object)
    def __init__(self, slug, dayval, monthval, yearval, name=None,
parent=None):
        self.dayval
        self.monthval = monthval
        self.yearval = yearval
        self.__name__ = name
        self.__parent__ = parent
        self.content = ask_mongo_for_blogpost(self.year, self.month,
self.day, slug)

root = Root()
root['archive'] = Archive(name='archive', parent=root)

###

Now you don't have to be concerned with subpaths at all.

-- Wade



-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to