[Cubicweb] Correct use of the 'person' cube

Stéphane Bugat stephane.bugat at free.fr
Tue Jul 17 07:54:40 CEST 2012


I've just seen your answer in the cw digest...
Thanks for the help about the use of match_user_groups. The difference between client/server side operations was really not clear to me although chapter 6.2 of cw doc gives some details about it. But a small scheme could provide some useful help for cubicweb dumbs like me ;-)

However, as you mention that the relation is built *after* the entities are created, in such case could we use a hook based on the relation with a selector like `match_rtype('in_group')` and a `after_add_relation` event? A test could be performed on the cwuser group using the `self.eidto` attribute...


----- Mail original -----
De: "Stéphane Bugat" <stephane.bugat at free.fr>
À: "Nicolas Chauvat" <nicolas.chauvat at logilab.fr>
Cc: cubicweb at lists.cubicweb.org
Envoyé: Dimanche 15 Juillet 2012 16:31:07
Objet: Re: [Cubicweb] Correct use of the 'person' cube

Ok, Nicolas. I still encounter a last problem with my hook. I try to select only CWUser belonging to the 'users' group. For that, my hook selector looks like this:

class createProfileHook(Hook):
    """automatically creates a profile, a person and a blog when a user is created"""

    __regid__ = 'create_profile'
    __select__ = Hook.__select__ & is_instance('CWUser') & match_user_groups('users')
    events = ('after_add_entity',)

    def __call__(self):

Obviously this does not work and the hook is not called when creating a new CWUser with something like:
rset = rql(u'INSERT CWUser X: X login "jdoe", X upassword "foobar", '
    u'X firstname "John", X surname "Doe", X in_group G '
    u'WHERE G name "users"')

If I remove the 'match_user_groups' selector it is actually called... Does this mean that this selector cannot apply in a hook?
If so, how can I check the user group in the __call__ method? Using a test on self.entity.in_group[0]?


----- Mail original -----
De: "Nicolas Chauvat" <nicolas.chauvat at logilab.fr>
À: "Stéphane Bugat" <stephane.bugat at free.fr>
Cc: cubicweb at lists.cubicweb.org
Envoyé: Vendredi 13 Juillet 2012 18:53:00
Objet: Re: [Cubicweb] Correct use of the 'person' cube

On Fri, Jul 13, 2012 at 08:23:52AM +0200, Stéphane Bugat wrote:

> One last question: I'd like to sync 'surname', 'firstname'
> attributes and email relations of both Person and CWUser in one and
> unique hook. For that I propose to select with a Hook.__select__ &
> (is_instance('Person') | is_instance('CWUser')) and to select on
> events ('after_add_entity', 'after_update_entity').

is_instance('Person', 'CWUser') would be better

> Is there a way to retrieve the selected event and instance in the
> __call__() method of the hook?

According to http://docs.cubicweb.org/devrepo/repo/hooks I would say
you are looking for self.entity in YourOwnHook.__call__()

Nicolas Chauvat

logilab.fr - services en informatique scientifique et gestion de connaissances  

More information about the Cubicweb mailing list