Your Ad Here

Tuesday, January 10, 2012

Re: How to mix in class based views from pluggable apps?

Each plugin should call super() on their own definition of the get_context_data method -- which will return a dictionary --, then update and return a modified dictionary.

There's no need to explicitly call each plugin by the name in subclasses. It works the other way around.


Cheers,
AT

On Tue, Jan 10, 2012 at 4:55 AM, Roland van Laar <roland@micite.net> wrote:
On 01/09/2012 10:36 PM, jrief wrote:
Hi,
currently I am writing a Django applications built up from loosely coupled plug-ins. Each of these plug-ins shall offer a class based view to handle get and post requests. For get requests the context shall be populated with plug-in specific data. For post requests, the plug-in specific posted data shall be handled by the corresponding view class. 

This of course is not difficult to achieve. The view class of the final app, which combines all these plugins, can overload the methods get_context_data() and post() and dispatch the requests to functions offered by these plug-ins. But I do not like this approach because it does not separate concerns and the author of the final app has to remember, how to dispatch these requests manually to the plug-ins.

Is there a way to know which plugin is needed?


My question is, if there is there a more elegant solution, say a pattern, which does not require to duplicate the dispatching code for the mixin classes?

Let me explain using some sample code:

class MainAppDetailView(SomeBaseDetailView, PluginAMixin, PluginBMixin):
    model = MyModel
    template_name = "my_detail.html"

    def get_context_data(self, **kwargs):
        context = super(FinalAppDetailView, self).get_context_data(**kwargs)
        PluginAMixin(self).update_context(context)
        PluginBMixin(self).update_context(context)
        return context

    def post(self, *args, **kwargs):
        post_request = self.request.POST
Instead of:

        response = PluginAMixin(self).handle_post(post_request)
        if issubclass(response, HTTPResponse):
            return response
        response = PluginBMixin(self).handle_post(post_request)
        if issubclass(response, HTTPResponse):
            return response
you could use:

for plugin in (PluginAMixin, PluginBMixin):
    response = super(plugin, self).handle_post(post_request)

    if issubclass(response, HTTPResponse):
        return response



        # handle post request for the main app
        ...
        return response

For my point of view this example contains too much code duplication. Is there a pattern, so that I only have to modify the class declaration of my FinalAppDetailView or even better, only in my settings.py?

Cheers, Jacob
--
Roland

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