That shouldn't be an issue, you can access the currently dispatched
controller using tg.dispatched_controller()
This way you will be able to access the controller instance and
retrieve the property you need even from the class method.

About the behavior of the before_validate parameters, that is actually
the right one.
I used *args and **kw just because I'm lazy and didn't want to specify
the right parameters.

The expected before_validate hook signature is:

def before_validate_hook(remainder, params):

if you use *args and **kw you simply get the remainder and the params
inside the args argument :D

On Wed, Apr 18, 2012 at 10:02 AM, Moritz Schlarb <[email protected]> wrote:
> I wanted to do it that way, but since I need a property of the class that I
> want to "inject" into kw, I can't do it as a classmethod....
> But it doesn't really matter if it gets injected twenty times...
>
> Thanks anyway!
>
> Am Dienstag, 17. April 2012 10:39:00 UTC+2 schrieb Alessandro Molina:
>>
>> Also, forgot to say that in this case the before_put method has to be
>> a static/class method as you are registering it without self.
>>
>> On Tue, Apr 17, 2012 at 10:35 AM, Alessandro Molina
>> <[email protected]> wrote:
>> > Sorry, my fault.
>> > I forgot that Pylons based versions of TG reallocate the controller on
>> > each request, as you are hooking the before_validate inside the
>> > __init__ this has the side effect of registering the validate hook
>> > each time you get a request.
>> >
>> > Change it to something like:
>> >
>> > class MyGroupRestController(EasyCrudRestController):
>> >   model = model.Group
>> >
>> >   def before_put(self, *args, **kw):
>> >       print args, kw
>> >
>> >
>> > before_validate(MyGroupRestController.before_put)(MyGroupRestController.put)
>> >
>> > This is one of the things that are going to change on in 2.3+ which
>> > allocates the controllers only on application startup, in the mean
>> > time you must avoid doing anything that changes the controller class
>> > inside the init itself.
>> >
>> > On Tue, Apr 17, 2012 at 10:07 AM, Moritz Schlarb
>> > <[email protected]> wrote:
>> >> Alessandro, a few additional questions came up ;):
>> >>
>> >> I got:
>> >>
>> >> class LessonsCrudController(FilteredCrudRestController):
>> >>     model = Lesson
>> >>     __table_options__ = {...}
>> >>     __form_options__ = {...}
>> >>
>> >>     def inject_event(self, *args, **kw):
>> >>         log.debug('inject_event')
>> >>         for a in args:
>> >>             log.debug(a)
>> >>         for k in kw:
>> >>             log.debug('%s: %s' % (k,kw[k]))
>> >>
>> >>     def __init__(self, *args, **kw):
>> >>         super(LessonsCrudController, self).__init__(*args, **kw)
>> >>         before_validate(self.inject_event)(self.post)
>> >>
>> >> And that is what I got on the logs:
>> >>
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,751 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,752 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,753 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] inject_event
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] []
>> >> 09:54:46,754 DEBUG [sauce.controllers.crc] {'event': u'', 'name':
>> >> u'text',
>> >> 'teacher': u'3', 'lesson_id': u'text', 'sprox
>> >> _id': u'', 'id': u''}
>> >>
>> >> So you see, the hook gets called very often! It's not event a constant
>> >> number, at first I counted 6 calls, now its more than 10.
>> >> And the arguments are (not merely wrong but) unexected both packed in
>> >> args
>> >> like args = [[], {}].
>> >>
>> >> What am I doing wrong NOW? :D
>> >>
>> >> Thanks a lot,
>> >> best wishes,
>> >> Moritz
>> >>
>> >> Am Dienstag, 17. April 2012 01:28:36 UTC+2 schrieb Moritz Schlarb:
>> >>>
>> >>> Ooooh noo....
>> >>> I think I was doing something incredibily stupid...
>> >>>
>> >>> I'm not sure what I coded yesterday, I don't seem to have saved it,
>> >>> but I
>> >>> think I was using the decorator wrongly.
>> >>>
>> >>> I think it was something like:
>> >>>
>> >>> @before_validate()
>> >>> def before_put(...):
>> >>>
>> >>> Thanks a lot!
>> >>>
>> >>> Am Montag, 16. April 2012 10:36:07 UTC+2 schrieb Alessandro Molina:
>> >>>>
>> >>>> How are you registering the decorator? It should work correctly.
>> >>>> I tested it on the fly and it seems to correctly work as expected.
>> >>>>
>> >>>> class MyGroupRestController(EasyCrudRestController):
>> >>>>     model = model.Group
>> >>>>
>> >>>>     def before_put(self, *args, **kw):
>> >>>>         print args, kw
>> >>>>
>> >>>>     def __init__(self, *args, **kw):
>> >>>>         super(MyGroupRestController, self).__init__(*args, **kw)
>> >>>>         before_validate(self.before_put)(self.put)
>> >>>>
>> >>>> There are other ways to register the hook, but they should all work
>> >>>> correctly.
>> >>>>
>> >>>> On Sun, Apr 15, 2012 at 11:54 PM, Moritz Schlarb
>> >>>> <[email protected]>
>> >>>> wrote:
>> >>>> > Hi there!
>> >>>> >
>> >>>> > I'm subclassing EasyCrudRestController and now I have an attribute
>> >>>> > self.event, that I don't want to be changed in the add and edit
>> >>>> > forms,
>> >>>> > but
>> >>>> > that needs to be present when writing the form to the database.
>> >>>> > I thought about using a @before_validate hook to simply insert it
>> >>>> > into
>> >>>> > kwargs, but that doesn't work since @before_validate doesn't seem
>> >>>> > to
>> >>>> > get
>> >>>> > called (no logging).
>> >>>> > Another alternative would be using a HiddenField, but then I would
>> >>>> > have
>> >>>> > to
>> >>>> > insert the value into the AddForm, which I don't know how to do.
>> >>>> >
>> >>>> > Any help would be nice! ;)
>> >>>> >
>> >>>> > Thanks and good night,
>> >>>> > Moritz
>> >>>> >
>> >>>> > --
>> >>>> > You received this message because you are subscribed to the Google
>> >>>> > Groups
>> >>>> > "TurboGears" group.
>> >>>> > To view this discussion on the web visit
>> >>>> > https://groups.google.com/d/msg/turbogears/-/qTqkd_d9t0gJ.
>> >>>> > To post to this group, send email to [email protected].
>> >>>> > To unsubscribe from this group, send email to
>> >>>> > [email protected].
>> >>>> > For more options, visit this group at
>> >>>> > http://groups.google.com/group/turbogears?hl=en.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "TurboGears" group.
>> >> To view this discussion on the web visit
>> >> https://groups.google.com/d/msg/turbogears/-/LUazTYzarrcJ.
>> >>
>> >> To post to this group, send email to [email protected].
>> >> To unsubscribe from this group, send email to
>> >> [email protected].
>> >> For more options, visit this group at
>> >> http://groups.google.com/group/turbogears?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/Dgo6nOyTDfQJ.
>
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to