Adding configuration to View to dispatch to any methods.

86 views
Skip to first unread message

Hiroki Kiyohara

unread,
Jan 2, 2013, 5:09:27 AM1/2/13
to django-d...@googlegroups.com
Hello, and Happy new year, everyone.
Please listen to my proposal.

I want django.views.generi.View.dispatch method to call methods in consideration of some config.
For example, calling methods depending on the value of the request parameter.

So I wrote this commit: https://github.com/hirokiky/django/commit/e3399495dca9a727568626f64e2fa276c2857da9

Description of this implementation
-----------------------------------------------
View.dispatch_config takes a dictonary. Keys of dispatch_config are HTTP method name as target.
Values are `predicate` dictionary. Key of `predicate` is method name which you want to call to. It's `receiver`.
And the value of `predicate` is `predicate function`. When the function returns True, dispatch method calls `reciver`(target method).

Since my English is not good, plead watch this Gist (for usage). https://gist.github.com/4433362

Solution I proposed may be not adequate. but I want dispatch method to be more flexible.

Thank you.

--------------------------
清原弘貴 (Hiroki KIYOHARA)
mail: hiro...@gmail.com
http://hirokiky.org/
@hirokiky

Russell Keith-Magee

unread,
Jan 2, 2013, 5:18:40 AM1/2/13
to django-d...@googlegroups.com
Hi Hiroki,

Thanks for the suggestion, but I'm afraid I don't see the benefit of doing this as configuration, rather than something in the dispatched method itself. Using your gist as an example -- why should this be something defined in a custom dispatching configuration format, rather than simply defining:

def get(self, request, *args, **kwargs):
    if kwargs.get('corn') == '1':
        return HttpResponse('pony with corn')
    return HttpResponse('pony')

Using this approach, you can be explicit about what you want to do; a developer reading the code requires no special knowledge to understand how it works (it's just standard Python code); you don't need to wrap the predicate up in to a function; it allows for more complex logic that can't be expressed as a simple predicate; and at the end of the day, it's less code as well. What's the advantage to your proposed approach?

Yours,
Russ Magee %-)


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


Hiroki Kiyohara

unread,
Jan 2, 2013, 1:46:06 PM1/2/13
to django-d...@googlegroups.com
Hi, Russ.

Thanks for your reply.That makes sense.I notice that the configuration of my suggestion is complex.
We need to know what the configuration to take the value.It is necessary to re-consider the implementation.

Advantage of my approach
-------------------------------------
My approaches enables to reuse the condition of the method calling (as `predicate function`).
For example, considering to header's value, session value or request.is_secure.
By dispatch_config, we can reuse these conditions.
Yes, in the example, the condition (kwargs.get('corn') == '1') is used only one time.

And more
--------------
Now the configuration takes one predicate function, however, thinking the further reusability,
We can enable the configuration to get more than one function to call one method, like...

dispatch_config = {'get': {'get_corn_1': (corn_predicate,),
'get_corn_1_secure': (corn_predicate, secure_predicate)}
}

It is hard to write all complex conditions in View.get method. and it can't be reuse.

What do you say?


2013年1月2日水曜日 19時18分40秒 UTC+9 Russell Keith-Magee:

Russell Keith-Magee

unread,
Jan 2, 2013, 6:23:55 PM1/2/13
to django-d...@googlegroups.com
On Thu, Jan 3, 2013 at 2:46 AM, Hiroki Kiyohara <hiro...@gmail.com> wrote:
Hi, Russ.

Thanks for your reply.That makes sense.I notice that the configuration of my suggestion is complex.
We need to know what the configuration to take the value.It is necessary to re-consider the implementation.

Advantage of my approach
-------------------------------------
My approaches enables to reuse the condition of the method calling (as `predicate function`).
For example, considering to header's value, session value or request.is_secure.
By dispatch_config, we can reuse these conditions.
Yes, in the example, the condition (kwargs.get('corn') == '1') is used only one time.

And more
--------------
Now the configuration takes one predicate function, however, thinking the further reusability,
We can enable the configuration to get more than one function to call one method, like...

dispatch_config = {'get': {'get_corn_1': (corn_predicate,),
'get_corn_1_secure': (corn_predicate, secure_predicate)}
}

It is hard to write all complex conditions in View.get method. and it can't be reuse.
 
What do you say?


I say:

def get(self, request, *args, **kwargs):
   if corn_predicate(request):
      if secure_predicate(request):
         return HttpResponse('secure pony with corn')
      return HttpResponse('pony with corn')
   return HttpResponse('pony')

If reuse of predicates is an issue for you, then by all means, reuse them. That doesn't mean you need to invent a new, inefficient, and highly restricted scheme for invoking those predicates.

Yours,
Russ Magee %-)

Hiroki Kiyohara

unread,
Jan 3, 2013, 12:21:33 AM1/3/13
to django-d...@googlegroups.com
Thank you, Russ.

I notice that It is exaggerated to change View.dispatch method.
My proposal can be implemented by inheriting View.
With reference to your opinion, I wrote this PredicateProcessView:

    https://gist.github.com/4440994

Inheritin this View, We can benefit as well as my proposal.

This discussion was informative for me.

Thank you.

--------------------------
清原弘貴 (Hiroki KIYOHARA)
mail: hiro...@gmail.com
http://hirokiky.org/
@hirokiky


2013年1月3日木曜日 8時23分55秒 UTC+9 Russell Keith-Magee:
Reply all
Reply to author
Forward
0 new messages