WARNING: This server is unstable and will be retired in the next days. If you want to keep this forum available, please request immediately a migration on the Nabble Support forum. Forums that don't receive any migration request will be deleted forever.

 « Return to Thread: CrudRestController and @before_validate

Re: CrudRestController and @before_validate

by Moritz Schlarb :: Rate this Message:

| View in Thread

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 <mail@...> 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 turbogears@....
> To unsubscribe from this group, send email to
> turbogears%2Bunsubscribe@....
> 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 turbogears@....
To unsubscribe from this group, send email to turbogears+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

 « Return to Thread: CrudRestController and @before_validate