On 4 jun, 12:10, Roald <downa...@gmail.com> wrote:
> On 3 jun, 19:33, Luke Plant <l.plant...@cantab.net> wrote:
> > On Thursday 03 June 2010 08:59:31 Roald wrote:
> > > > One reason against this is it makes it harder to re-use other
> > > > views from within the View.
> > > I don't really understand what you mean. Do you mean re-using
> > > 'good' old function-views?
> > Yes, or new ones, or any callable that returns an HttpResponse that I
> > want to return.  It is quite a common pattern to have one view wrap
> > another or *several* others, and also to use standard response objects
> > in some utility function or 'sub view'. e.g.
>
> > def my_view(request, *args):
> >    if some_condition():
> >       return some_view(request, *args)
> >    elif other_condition():
> >       return some_standard_response(request.user)
> >    else:
> >       ...
> >       return render_to_response(...)
>
> > def some_standard_response(user)
> >     return HttpResponseRedirect(...)
>
> > This would be massively uglified, at best, if we make View a subclass
> > of HttpResponse.
>
> You're right, I wouldn't know how to do this with __init__, and I
> think I would choose for a simple function, or maybe __new__. On the
> other hand: is that a problem? These classes are a solution to a
> problem that I have with a lot of my views, but not with all. It's not
> necessary to make every view a class.

I've thought about it a bit more, and from an object oriented point of
view, your 'my_view' should be a base class of 'some_view',
'some_standard_response' etcetera. ABCMeta gives us a way of adding
(abstract) base classes. In Python I think you would best describe
that with something like:


    class my_view(HttpResponse):
        __metaclass__ = ABCMeta

        def __init__(self, *args, **kwargs):
            print 'init my_view'

        def __new__(cls, *args, **kwargs):
            if some_condition():
                return some_view(*args, **kwargs)
            elif other_condition():
                return some_standard_response(*args, **kwargs)
            else:
                ...
                return object.__new__(cls)


    my_view.register(some_view)
    my_view.register(some_standard_response)


This (or something like it) seems to be able to take the best of the
__new__ and __init__ options.

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

Reply via email to