Hi all,

There is a discussion 'Class based generic views in 1.3?' in this
list. I would like to suggest an alternative: ModelViews. There
normally are multiple standard views associated with one model, and
such a class can take advantage of that. Its use should look something
like this:


    * in urls.py:
        (r'^blog/', BlogPostView),


    * in views.py
        class BlogPostView(ModelView):
            class Meta:
                model = BlogPost


The base class ModelView should be implemented something like this:


    class ModelView(object):
        def __init__(self, request, *args, **kwargs):
            super(ModelView, self).__init__(request, *args, **kwargs)
            # handle url configuration
            # handle Meta-class information (maybe this should be in
__new__ or ...)
            # etcetera

        @requesthandler
        def add(self, request, *args, **kwargs):
            ...

        @requesthandler
        def edit(self):
            ...

        @requesthandler
        def list(self):
            ...

        def page(self):
            # doesn't handle requests
            ...


The urls.py-file used by this class should probably be interpreted a
bit differently, I'm thinking of something like this:


    (r'^add/', self.add),
    (r'^edit/', self.edit),
    (r'^list/', self.list),


The requesthandler should be something like this:


    def requesthandler(method):
        # make a method that handles the request (currying)...
        def new_method(self):
            def actual_handler(request, *args, **kwargs):
                return method(self, request, *args, **kwargs):
            return actual_handler

        # ... and make it a property, so it can be decorated
        result = property(new_method)

        # ... it can be marked to be a request handler, or put in a
list of request handlers, or...
        result.is_request_handler = True

        return result


Anybody likes the idea? There are a few remarks I'd already like to
make:
- I'm not sure 'ModelView' is the right name for this, since not the
class itself but its requesthandler properties are the actual views.
On the other hand, the name is too nice not to be used.
- I'm no fan of 'class Meta' myself, but I've chosen it here to be
compatible with ModelForm


Cheers, Roald

-- 
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