author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 22 Jul 2009 19:42:10 +0200 | |
changeset 2426 | 0209d9bc45a8 |
parent 2101 | 08003e0354a7 |
child 2495 | abcbadcc110a |
permissions | -rw-r--r-- |
0 | 1 |
"""various library content 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 |
||
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
10 |
from cubicweb.common.uilib import soup2xhtml |
0 | 11 |
from cubicweb.server.hooksmanager import Hook |
12 |
from cubicweb.server.pool import PreCommitOperation |
|
13 |
||
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
14 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
15 |
class AddUpdateCWUserHook(Hook): |
0 | 16 |
"""ensure user logins are stripped""" |
17 |
events = ('before_add_entity', 'before_update_entity',) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
18 |
accepts = ('CWUser',) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
19 |
|
0 | 20 |
def call(self, session, entity): |
21 |
if 'login' in entity and entity['login']: |
|
22 |
entity['login'] = entity['login'].strip() |
|
23 |
||
24 |
||
25 |
class AutoDeleteBookmark(PreCommitOperation): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
26 |
beid = None # make pylint happy |
0 | 27 |
def precommit_event(self): |
28 |
session = self.session |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
29 |
if not self.beid in session.transaction_data.get('pendingeids', ()): |
0 | 30 |
if not session.unsafe_execute('Any X WHERE X bookmarked_by U, X eid %(x)s', |
31 |
{'x': self.beid}, 'x'): |
|
32 |
session.unsafe_execute('DELETE Bookmark X WHERE X eid %(x)s', |
|
33 |
{'x': self.beid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
34 |
|
0 | 35 |
class DelBookmarkedByHook(Hook): |
36 |
"""ensure user logins are stripped""" |
|
37 |
events = ('after_delete_relation',) |
|
38 |
accepts = ('bookmarked_by',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
39 |
|
0 | 40 |
def call(self, session, subj, rtype, obj): |
41 |
AutoDeleteBookmark(session, beid=subj) |
|
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
42 |
|
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
43 |
|
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
44 |
class TidyHtmlFields(Hook): |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
45 |
"""tidy HTML in rich text strings""" |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
46 |
events = ('before_add_entity', 'before_update_entity') |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
47 |
accepts = ('Any',) |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
48 |
|
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
49 |
def call(self, session, entity): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
50 |
metaattrs = entity.e_schema.meta_attributes() |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
51 |
for metaattr, (metadata, attr) in metaattrs.iteritems(): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
52 |
if metadata == 'format': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
53 |
try: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
54 |
value = entity[attr] |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
55 |
except KeyError: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
56 |
continue # no text to tidy |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
57 |
if isinstance(value, unicode): # filter out None and Binary |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
58 |
if self.event == 'before_add_entity': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
59 |
fmt = entity.get(metaattr) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
60 |
else: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
61 |
fmt = entity.get_value(metaattr) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
62 |
if fmt == 'text/html': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
63 |
entity[attr] = soup2xhtml(value, session.encoding) |