[Twisted-Python] interface for Last-Modified

Kevin Turner kevin.m.turner at pobox.com
Tue Sep 24 23:50:18 EDT 2002

itamar has led me to what looks to be a much more elegant solution. 
Since the only resources that worry about receiving conditional requests
are those that set cache validation headers (such as "Last-Modified"),
the check to see if you're dealing with a conditional request can go in
the L{http.Request.setLastModified} method.  setLastModified's return
value can then let the caller know if they date they gave was older than
the one in the conditional request, so they don't have to render the
body of the request.  And this works, because Last-Modified being a
header and all, you're having to set it before you write the request
body anyway.

So this works pretty well::

    class SimpleResource(resource.Resource):
        def render(self, request):
            if http.CACHED in (request.setLastModified(10),
                return ''
                return "correct"

and I was all ready to check it in, until I tried to apply it in my
actual use-case, using it in a superclass::

    class AbstractCommitList(widgets.Widget, mvc.View):
        def display(self, request):
            if request.setLastModified(self.lastModified) is http.CACHED:
                # Body should be empty.
                return ''
                # Body is rendered by subclass.
                return ''
            # That wasn't a very effective conditional.

    class RssChannel(AbstractCommitList):
        def display(self, request):
            s = AbstractCommitList.display(self, request)
            # Now how do I know if I'm supposed to render the body or not?

I guess I could add Request.wantsABody() and have all display subclasses
call that.  Or maybe we've succeeded in moving the problem up above the
Resource.render level to higher-level things like widgets, so it's
something to be solved there.

Any thoughts?

The moon is waning gibbous, 90.6% illuminated, 17.7 days old.

More information about the Twisted-Python mailing list