Monday, April 26, 2010

Re: feed decorator

Have you tried invoking the pyhton debugger inside _wrapped to find
out the actual values of req_format and fmt and what's happening in
there?

Nuno

On Mon, Apr 19, 2010 at 4:15 AM, Streamweaver <streamweaver@gmail.com> wrote:
> I'm having trouble developing a feed decorator. I feel like the
> decorator I've come up with should work but isn't and I wanted to ask
> if someone can spot my error.
>
> I have two view methods one that returns a normal HTML view and
> another that returns an rss feed of the same data.  They both take the
> same arguments.  I am trying to create a decorator that checks the
> request objects for a format variable in the querystring and returns
> the feed view instead of the html view.  Like so:
>
> my_view_feed
>
> @format_req('rss', my_view_feed)
> my_view
>
> So a request for '/site/foo/bar' would return 'my_view' but '/site/foo/
> bar?format=rss' would return 'my_view_feed'
>
> This is the decorator method I created and I would think would work
> but I'm still struggling a bit with decorators with arguments, even
> after reading Bruce's excellent post (http://www.artima.com/weblogs/
> viewpost.jsp?thread=240845)
>
> def format_req(fmt, new_fn):
>
>    def _decorator(view_fn):
>
>        def _wraped(request, *args, **kwargs):
>            req_format = request.GET.get('format', None)
>            if req_format == fmt:
>                return new_fn(request, *args, **kwargs)
>            # Default to returning the original method
>            return view_fn(request, *args, **kwargs)
>
>        return _wraped
>
>    return _decorator
>
> I would have though a request with 'format=rss' would return new_view
> but no matter what I do I always get view_fn returned.
>
> Can anyone point me toward where I'm going wrong.  As usual I'll post
> the final code once I get it working for reference.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate