[Cubicweb] nested relation / hook validation

Sylvain Thénault sylvain.thenault at logilab.fr
Tue Jan 25 08:47:08 CET 2011


On 25 janvier 00:29, Carlos Balderas wrote:
> Hi List!

Hi Carlos,
 
> I am trying to make a hook validation but I can figure out how this can be
> done.
> 
> Here the explanation of the schema
> 
> * *ProcessRole* is a composite entity of *Process*.
> * *ProcessRole* has a relation to *JobTitle* relation.
> 
> I want to validate that a *ProcessRole* shouldn't be added to a *Process*,
> if the *JobTitle* already exists in other *ProcessRole* in the same *Process
> *.
> 
> e.g.
> Process = Certification One
> which has a :
>                  ProcessRole = Validator
>                 JobTitle = Human R. 1
> Then, I don't want any other ProcessRole to be added with the JobTitle =
> Human R.1 to Certification One Process.
> 
> I tried to make this validation in the event "before/after" add relation at
> the moment to add JobTitle to ProcessRole and what I found at this moment I
> could not rich the Process (empty relation) entity which ProcessRole will be
> added.
> 
> I also tried to make the validation in the event "before/after" add relation
> at the moment to add ProcessRole to Process, but at this moment I could not
> rich the JobTitle (empty relation) entity to validate if this JobTitle
> exists already related to the Process.

That's why Operation exists :) For such case where you may miss some 
information that may be set later in the transaction, you should instantiate
an operation in the hook. Operation are then processed at transaction end time
(commit or rollback). See
http://docs.cubicweb.org/devrepo/repo/hooks.html#cubicweb.server.hook.Operation

Also, what you want may be acheived using schema RQLUniqueConstraint. Something
like:

class JobTitle(EntityType):
    name = String(
        required=True, indexed=True, maxsize=128,
        constraints=[RQLUniqueConstraint('SPR process P, SPR job_title S, S name N'
                                         'PR process P, PR job_title X, X name N', 'X', 
                                         _('only one process role with a given job title is accepted'))]
        )

class ProcessRole(EntityType):
    job_title = SubjectRelation(
        'JobTitle',
        constraints=[RQLUniqueConstraint('S process P, O name N,'
                                         'PR process P, PR job_title X, X name N', 'X', 
                                         _('only one process role with a given job title is accepted'))]
        )
    process = SubjectRelation(
        'Process',
        constraints=[RQLUniqueConstraint('O job_title X1, X1 name N,'
                                         'PR process S, PR job_title X2, X2 name N,'
                                         'X1 eid > X2', ('X1', 'X2'),
                                         _('only one process role with a given job title is accepted'))]
        )

should do the trick nicely.

-- 
Sylvain Thénault                               LOGILAB, Paris (France)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org




More information about the Cubicweb mailing list