author | sylvain.thenault@logilab.fr |
Fri, 24 Apr 2009 09:20:43 +0200 | |
branch | tls-sprint |
changeset 1469 | ba9759972b52 |
parent 1398 | 5fe84a5f7035 |
child 1802 | d628defebc17 |
permissions | -rw-r--r-- |
0 | 1 |
"""various library content hooks |
2 |
||
3 |
:organization: Logilab |
|
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
9 |
from cubicweb.common.uilib import soup2xhtml |
0 | 10 |
from cubicweb.server.hooksmanager import Hook |
11 |
from cubicweb.server.pool import PreCommitOperation |
|
12 |
||
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
13 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
14 |
class AddUpdateCWUserHook(Hook): |
0 | 15 |
"""ensure user logins are stripped""" |
16 |
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
|
17 |
accepts = ('CWUser',) |
0 | 18 |
|
19 |
def call(self, session, entity): |
|
20 |
if 'login' in entity and entity['login']: |
|
21 |
entity['login'] = entity['login'].strip() |
|
22 |
||
23 |
||
24 |
class AutoDeleteBookmark(PreCommitOperation): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
25 |
beid = None # make pylint happy |
0 | 26 |
def precommit_event(self): |
27 |
session = self.session |
|
28 |
if not self.beid in session.query_data('pendingeids', ()): |
|
29 |
if not session.unsafe_execute('Any X WHERE X bookmarked_by U, X eid %(x)s', |
|
30 |
{'x': self.beid}, 'x'): |
|
31 |
session.unsafe_execute('DELETE Bookmark X WHERE X eid %(x)s', |
|
32 |
{'x': self.beid}, 'x') |
|
33 |
||
34 |
class DelBookmarkedByHook(Hook): |
|
35 |
"""ensure user logins are stripped""" |
|
36 |
events = ('after_delete_relation',) |
|
37 |
accepts = ('bookmarked_by',) |
|
38 |
||
39 |
def call(self, session, subj, rtype, obj): |
|
40 |
AutoDeleteBookmark(session, beid=subj) |
|
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
41 |
|
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 |
class TidyHtmlFields(Hook): |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
44 |
"""tidy HTML in rich text strings""" |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
45 |
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
|
46 |
accepts = ('Any',) |
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
47 |
|
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
48 |
def call(self, session, entity): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
49 |
metaattrs = entity.e_schema.meta_attributes() |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
50 |
for metaattr, (metadata, attr) in metaattrs.iteritems(): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
51 |
if metadata == 'format': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
52 |
try: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
53 |
value = entity[attr] |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
54 |
except KeyError: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
55 |
continue # no text to tidy |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
56 |
if isinstance(value, unicode): # filter out None and Binary |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
57 |
if self.event == 'before_add_entity': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
58 |
fmt = entity.get(metaattr) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
59 |
else: |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
60 |
fmt = entity.get_value(metaattr) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
61 |
if fmt == 'text/html': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
62 |
entity[attr] = soup2xhtml(value, session.encoding) |
1098
739d4dce9b19
* turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |