[Cubicweb] RQLConstraint attributes validation

Aurélien Campéas aurelien.campeas at logilab.fr
Tue May 13 17:37:57 CEST 2014


On 13/05/2014 01:31, Carlos Balderas wrote:
> Hello List,

Hello Carlos,

> 
> I am failing to make a validation using RQLConstraint. What I am trying
> to accomplish is to validate dates among attributes of the same entity,
> making the following validations:
> 
>  (finished date >= prevision date) & (prevision date >= start date)
> 
> none of the dates are required, but if you need to set prevision date
> then you must set start date, same thing for finished date.
> 
> Here is the entity:
> 
> class ProjectStage(WorkflowableEntityType):
>     """a project stage groups a list of activities/todo's to be completed
>     in certain amount of time.
>     """
>     start = Datetime(description=_('start date'),
>                  constraints=[
>             RQLConstraint('(S start NULL)'
>                           ' OR (S start <= B, S prevision B)'
>                           ' OR (S prevision NULL)',
>                           msg=_('start date should be'
>                                 ' less than or equal to'
>                                 ' prevision date')),
>             RQLConstraint('(S start NULL)'
>                           ' OR (S start <= B, S finished B)'
>                           ' OR (S finished NULL)',
>                           msg=_('start date should be'
>                                 ' less than or equal to'
>                                 ' finish date'))])
>     prevision = Datetime(description=_('estimated finish date'),
>                      constraints=[
>             RQLConstraint('(S prevision NULL)'
>                           ' OR (S prevision >= B, S start B)'
>                           ' OR (S start NULL)',
>                           msg=_('prevision date should be'
>                                 ' greater than or equal to'
>                                 ' start date'))])
>     finished = Datetime(description=_('real finish date'),
>                         constraints=[
>             RQLConstraint('(S finished NULL)'
>                           ' OR (S finished >= B, S start B)'
>                           ' OR (S start NULL)',
>                           msg=_('finish date should be'
>                                 ' greater than or equal to'
>                                 ' start date'))])

Also given how you have:

* to defend against unset attributes
* 4 attributes

maybe it would be easier to implement with a hook on entity
creation/update (code-complexity-wise) ...

> 
> 
> At the time I am creating the ProjectStage entity I am getting the
> message of constraint violation even though the date values are correct
> according to the explained logic.
> 
> I understand this functionality can be covered by using hooks, but I
> really like to know if what I am trying to do is possible by using
> RQLConstraints or to find out what I am doing wrong.

You will probably want to debug this with DBG_OPS, and putting prints
in RepoEnforcedRQLConstraintMixIn.exec_query maybe (in cw/schema.py).

Also proofreading surely helps :)
Note, e.g. the following:

RQLConstraint('(S start NULL)'
              ' OR (S start <= B, S finished B)'
              ' OR (S finished NULL)')

Maybe you need:

 ((S start NULL) OR (S finished NULL)) OR (S start <= B, S finished B)

(or something).

Regards,
Aurélien.



More information about the Cubicweb mailing list