In fact, what I want is to hijack a view:

def hijacker(request):
    if I_feel_its_necessary:
        hijack_caller_view_and_return(some_json_content)

def my_view(request):
    hijacker(request)
    ... # Continue as normal
    return some_html_content

I have some long views which I'd like to modify as little as possible
and insert some extra behaviour handled by 'hijacker'. Adding just a
call to 'hijacker' to all of these views would be ideal for me because
that would be quick to modify them and also easier to maintain.
I don't want the views to care about what 'hijacker' will do. If
'hijacker' thinks it's necessary, it would force returning a certain
value (some json content in this case), otherwise it would let the
caller view do its normal business.

Any idea on how to do that, if that's even possible?

Thanks!

Julien


On Mar 24, 10:35 pm, Ned Batchelder <[EMAIL PROTECTED]> wrote:
> I don't know how to do this in Python, if you even can.  Inspect may be
> just the dark scary corner to explore to find this capability if it
> exists.  But why would you want to do something like this?  Even if
> inspect could do this, it looks like called would have to have some very
> specific knowledge of caller's structure  to make it work.
>
> Much much better would be to get caller and called to cooperate, and
> have called simply return a value that caller would check.  Or for the
> two to be based on a template pattern (template as in the Gang of Four
> book, not as in a Django template).
>
> Maybe if you told us a bit more about the problem, we could help you
> find a solution.
>
> --Ned.http://nedbatchelder.com/blog
>
>
>
> Julien wrote:
> > Hi all,
>
> > This is more a Python issue than a Django one, but I thought the
> > Python-aware people that you are might be able to help me :)
>
> > I would like to do something like:
>
> > def called(arg)
> >     if arg==True:
> >         !!magic!!caller.return 1
>
> > def caller(arg)
> >     called(arg)
> >     return 2
>
> > Here, the fake !!!magic!!! represents a statement (which I ignore)
> > that would make the caller function return a value different from what
> > it'd return normally.
>
> > For example, caller(True) would return 1, and caller(False) would
> > return 2. The reason I want that is because I don't want the caller
> > function to know what's going on in the called function, and be
> > shortcut if the called function think it's necessary.
>
> > Would you know if that's possible, and if so, how?
>
> > I've done a bit of research and I think I've found some good pointers,
> > in particular using the 'inspect' library:
>
> > import inspect
>
> > def called(arg)
> >     if arg==True:
> >         caller_frame = inspect.stack()[1]
> >         ...
>
> > Here 'caller_frame' contains the frame of the caller function. Now,
> > how can I make that frame return a particular value?
>
> > Hope that was clear... :/
>
> > Thanks!
>
> > Julien
>
> --
> Ned Batchelder,http://nedbatchelder.com
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to