[Cubicweb] Proposal for an enrichment en cubicweb/schema.py

St├ęphane Bugat stephane.bugat at free.fr
Tue Sep 18 15:26:11 CEST 2012

Following a discussion we had syt, auc and I on the public tchat, I put here the following modification for cubicweb/schema.py. The idea is to have a RQLConstraint that allows to specify a minimum and/or maximum value for a relation.

For that, we define a new constraint, RQLArbitraryConstraint, as::

    class RQLArbitraryConstraint(RQLUniqueConstraint):

        def __init__(self, expression, mainvars=None, msg=None, nbmin=0, nbmax=1):
            super(RQLArbitraryConstraint, self).__init__(expression, mainvars, msg)
            self.nbmin = nbmin
            self.nbmax = nbmax

        def match_condition(self, session, eidfrom, eidto):
            nb = len(self.exec_query(session, eidfrom, eidto))
            return (nb >= self.nbmin & nb <= self.nbmax)

    CONSTRAINTS['RQLArbitraryConstraint'] = RQLArbitraryConstraint

The constraints is used afterwards like RQLUniqueConstraint but by passing 2 more arguments, nbmin and nbmax::

        constraints = [RQLArbitraryConstraint('S in_contact_with A', nbmin=0, nbmax=20,
                       msg=_('You cannot have more than 20 persons inn your network'),)]

Best regards


More information about the Cubicweb mailing list