[Grok-dev] Re: Does it make sense: registering factories that are functions not classes

Jan-Wijbrand Kolman janwijbrand at gmail.com
Tue Jun 5 11:44:08 EDT 2007


> I checked out the branch and tried the decorators. It seems then you can
> only use a method for the adaptor. Is this expected or is there a way to
> use classes that I'm missing? For example to get ISized I needed to use
> the method to return a class:
>
> @grok.adapter(JanW)
> @grok.implementer(ISized)
> def sizedForJanW(context):
>         return ActualSizedAdapterForJanW(context)
>
> Which gets me an adapter class *and* a decorated method. Surely just as
> easy to use an adapter sub-classing grok.Adapter. From memory all my
> adapters to date need to be classes.
>
> I'm sure I'm missing something and am keen to be enlightened.

It is complementary to grok.Adapter.

Sometimes its very useful to have a simple function that acts as an
adapter factory. That is what this decorator notation is for. One
real-world example for which this could be useful is:

  vocabulary = SimpleVocabulary.fromValues(('a', 'b', 'c', 'd', 'e'))

  @grok.adapter(ISet, IBrowserRequest)
  @grok.implementer(IInputWidget)
  def selection_widget(field, request):
      return MultiCheckBoxWidget(field, field.value_type.vocabulary, request)

  class Index(grok.Form):

      form_fields = grok.Fields(
          selection=schema.Set(
              title=u"Selection",
              value_type=schema.Choice(vocabulary=vocabulary))
          )

      @grok.action('Submit')
      def handle_submit(self, **data):
          pass

But see also the start post for this thread.

kind regards,
jw
-- 
Jan-Wijbrand Kolman


More information about the Grok-dev mailing list