[Cubicweb] nested relation / hook validation
Carlos Balderas
carlos.balderas at gmail.com
Tue Jan 25 16:30:18 CET 2011
Hey Syt, thank you very much for such a complete explanation.
In case I use the Operation option, I am getting like the entity ProcessRole
has been already related to Process. e.i.
class CheckJobTitleinProcess(hook.Operation):
"""checks job title is not repeated in a process
"""
def precommit_event(self):
"""validates jobtitle before commit
"""
for eid in
self.session.transaction_data.pop('pending_jobtitle_checking'):
process_role = self.session.entity_from_eid(eid)
process = process_role.reverse_has_role[0]
if process.is_jobtitle_related(process_role):
msgerror = u'%s' % self.session._('the process can have '
'just one jobtitle')
raise ValidationError(self, {'error': msgerror})
then this is the Process method:
By the time I get to "is_jobtitle_related" method the ProcessRole already is
related to Process, and I am not sure if it is one of the commited
ProcessRole in database or is the one I am trying to add.
So I made this "dirty" trick to make it work...
def is_jobtitle_related(self, process_role):
"""validates if jobtitle exists
"""
jobtitle = process_role.has_jobtitle[0]
counter = -1
for role in self.has_role:
if role.has_jobtitle[0].eid == jobtitle.eid:
counter += 1
if counter > 0:
return True
return False
I got the feeling this is not the right way =).
What do you think?
Regards
Carlos BALDERAS
On Tue, Jan 25, 2011 at 1:47 AM, Sylvain Thénault <
sylvain.thenault at logilab.fr> wrote:
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubicweb.org/pipermail/cubicweb/attachments/20110125/11ae2f1c/attachment-0186.html>
More information about the Cubicweb
mailing list