author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 27 Nov 2009 09:14:31 +0100 | |
branch | stable |
changeset 3922 | 69020a7c234a |
parent 3869 | ec6463886ac4 |
child 3877 | 7ca53fc72a0a |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""helper functions for application hooks |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from cubicweb import RepositoryError |
|
1132 | 11 |
from cubicweb.server.pool import SingleLastOperation |
0 | 12 |
|
3869
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
13 |
def entity_oldnewvalue(entity, attr): |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
14 |
"""returns the couple (old attr value, new attr value) |
0 | 15 |
|
3869
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
16 |
NOTE: will only work in a before_update_entity hook |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
17 |
""" |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
18 |
# get new value and remove from local dict to force a db query to |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
19 |
# fetch old value |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
20 |
newvalue = entity.pop(attr, None) |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
21 |
oldvalue = getattr(entity, attr) |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
22 |
if newvalue is not None: |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
23 |
entity[attr] = newvalue |
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
24 |
return oldvalue, newvalue |
0 | 25 |
|
26 |
def rproperty(session, rtype, eidfrom, eidto, rprop): |
|
27 |
rschema = session.repo.schema[rtype] |
|
28 |
subjtype = session.describe(eidfrom)[0] |
|
29 |
objtype = session.describe(eidto)[0] |
|
30 |
return rschema.rproperty(subjtype, objtype, rprop) |
|
31 |
||
32 |
def check_internal_entity(session, eid, internal_names): |
|
33 |
"""check that the entity's name is not in the internal_names list. |
|
34 |
raise a RepositoryError if so, else return the entity's name |
|
35 |
""" |
|
3869
ec6463886ac4
[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2876
diff
changeset
|
36 |
name = session.entity_from_eid(eid).name |
0 | 37 |
if name in internal_names: |
38 |
raise RepositoryError('%s entity can\'t be deleted' % name) |
|
39 |
return name |
|
40 |
||
41 |
def get_user_sessions(repo, ueid): |
|
42 |
for session in repo._sessions.values(): |
|
43 |
if ueid == session.user.eid: |
|
44 |
yield session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
45 |
|
0 | 46 |
|
47 |
# mail related ################################################################ |
|
48 |
||
49 |
class SendMailOp(SingleLastOperation): |
|
50 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
|
51 |
# may not specify msg yet, as |
|
52 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
|
53 |
if msg is not None: |
|
54 |
assert recipients |
|
55 |
self.to_send = [(msg, recipients)] |
|
56 |
else: |
|
57 |
assert recipients is None |
|
58 |
self.to_send = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
59 |
super(SendMailOp, self).__init__(session, **kwargs) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
60 |
|
0 | 61 |
def register(self, session): |
62 |
previous = super(SendMailOp, self).register(session) |
|
63 |
if previous: |
|
64 |
self.to_send = previous.to_send + self.to_send |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
65 |
|
0 | 66 |
def commit_event(self): |
67 |
self.repo.threaded_task(self.sendmails) |
|
68 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
69 |
def sendmails(self): |
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2102
diff
changeset
|
70 |
self.config.sendmails(self.to_send) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
71 |
|
0 | 72 |
|
73 |
# state related ############################################################### |
|
74 |
||
75 |
def previous_state(session, eid): |
|
76 |
"""return the state of the entity with the given eid, |
|
77 |
usually since it's changing in the current transaction. Due to internal |
|
78 |
relation hooks, the relation may has been deleted at this point, so |
|
79 |
we have handle that |
|
80 |
""" |
|
2876
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
81 |
# don't check eid in session.transaction_data.get('neweids', ()), we don't |
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
82 |
# want to miss previous state of entity whose state change in the same |
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
83 |
# transaction as it's being created |
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
84 |
pending = session.transaction_data.get('pendingrelations', ()) |
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
85 |
for eidfrom, rtype, eidto in reversed(pending): |
0 | 86 |
if rtype == 'in_state' and eidfrom == eid: |
87 |
rset = session.execute('Any S,N WHERE S eid %(x)s, S name N', |
|
88 |
{'x': eidto}, 'x') |
|
89 |
return rset.get_entity(0, 0) |
|
90 |
rset = session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N', |
|
91 |
{'x': eid}, 'x') |
|
92 |
if rset: |
|
93 |
return rset.get_entity(0, 0) |