12 class SetUseEmailRelationOp(PreCommitOperation): |
12 class SetUseEmailRelationOp(PreCommitOperation): |
13 """delay this operation to commit to avoid conflict with a late rql query |
13 """delay this operation to commit to avoid conflict with a late rql query |
14 already setting the relation |
14 already setting the relation |
15 """ |
15 """ |
16 rtype = 'use_email' |
16 rtype = 'use_email' |
|
17 fromeid = toeid = None # make pylint happy |
17 |
18 |
18 def condition(self): |
19 def condition(self): |
19 """check entity has use_email set for the email address""" |
20 """check entity has use_email set for the email address""" |
20 return not self.session.unsafe_execute( |
21 return not self.session.unsafe_execute( |
21 'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s', |
22 'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s', |
22 {'x': self.fromeid, 'y': self.toeid}, 'x') |
23 {'x': self.fromeid, 'y': self.toeid}, 'x') |
23 |
24 |
24 def precommit_event(self): |
25 def precommit_event(self): |
25 session = self.session |
26 session = self.session |
26 if self.condition(): |
27 if self.condition(): |
27 session.unsafe_execute( |
28 session.unsafe_execute( |
28 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
29 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
29 {'x': self.fromeid, 'y': self.toeid}, 'x') |
30 {'x': self.fromeid, 'y': self.toeid}, 'x') |
30 |
31 |
31 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
32 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
32 rtype = 'primary_email' |
33 rtype = 'primary_email' |
33 |
34 |
34 def condition(self): |
35 def condition(self): |
35 """check entity has no primary_email set""" |
36 """check entity has no primary_email set""" |
36 return not self.session.unsafe_execute( |
37 return not self.session.unsafe_execute( |
37 'Any X WHERE X eid %(x)s, X primary_email Y', |
38 'Any X WHERE X eid %(x)s, X primary_email Y', |
38 {'x': self.fromeid}, 'x') |
39 {'x': self.fromeid}, 'x') |
39 |
40 |
40 |
41 |
41 class SetPrimaryEmailHook(Hook): |
42 class SetPrimaryEmailHook(Hook): |
42 """notify when a bug or story or version has its state modified""" |
43 """notify when a bug or story or version has its state modified""" |
43 events = ('after_add_relation',) |
44 events = ('after_add_relation',) |
44 accepts = ('use_email',) |
45 accepts = ('use_email',) |
45 |
46 |
46 def call(self, session, fromeid, rtype, toeid): |
47 def call(self, session, fromeid, rtype, toeid): |
47 subjtype = session.describe(fromeid)[0] |
48 subjtype = session.describe(fromeid)[0] |
48 eschema = self.vreg.schema[subjtype] |
49 eschema = self.vreg.schema[subjtype] |
49 if 'primary_email' in eschema.subject_relations(): |
50 if 'primary_email' in eschema.subject_relations(): |
50 SetPrimaryEmailRelationOp(session, vreg=self.vreg, |
51 SetPrimaryEmailRelationOp(session, vreg=self.vreg, |
51 fromeid=fromeid, toeid=toeid) |
52 fromeid=fromeid, toeid=toeid) |
52 |
53 |
53 class SetUseEmailHook(Hook): |
54 class SetUseEmailHook(Hook): |
54 """notify when a bug or story or version has its state modified""" |
55 """notify when a bug or story or version has its state modified""" |
55 events = ('after_add_relation',) |
56 events = ('after_add_relation',) |
56 accepts = ('primary_email',) |
57 accepts = ('primary_email',) |
57 |
58 |
58 def call(self, session, fromeid, rtype, toeid): |
59 def call(self, session, fromeid, rtype, toeid): |
59 subjtype = session.describe(fromeid)[0] |
60 subjtype = session.describe(fromeid)[0] |
60 eschema = self.vreg.schema[subjtype] |
61 eschema = self.vreg.schema[subjtype] |
61 if 'use_email' in eschema.subject_relations(): |
62 if 'use_email' in eschema.subject_relations(): |
62 SetUseEmailRelationOp(session, vreg=self.vreg, |
63 SetUseEmailRelationOp(session, vreg=self.vreg, |
63 fromeid=fromeid, toeid=toeid) |
64 fromeid=fromeid, toeid=toeid) |