1 """various library content hooks |
1 """various library content hooks |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
|
9 from cubicweb.common.uilib import soup2xhtml |
9 from cubicweb.server.hooksmanager import Hook |
10 from cubicweb.server.hooksmanager import Hook |
10 from cubicweb.server.pool import PreCommitOperation |
11 from cubicweb.server.pool import PreCommitOperation |
|
12 |
11 |
13 |
12 class AddUpdateEUserHook(Hook): |
14 class AddUpdateEUserHook(Hook): |
13 """ensure user logins are stripped""" |
15 """ensure user logins are stripped""" |
14 events = ('before_add_entity', 'before_update_entity',) |
16 events = ('before_add_entity', 'before_update_entity',) |
15 accepts = ('EUser',) |
17 accepts = ('EUser',) |
33 events = ('after_delete_relation',) |
35 events = ('after_delete_relation',) |
34 accepts = ('bookmarked_by',) |
36 accepts = ('bookmarked_by',) |
35 |
37 |
36 def call(self, session, subj, rtype, obj): |
38 def call(self, session, subj, rtype, obj): |
37 AutoDeleteBookmark(session, beid=subj) |
39 AutoDeleteBookmark(session, beid=subj) |
|
40 |
|
41 |
|
42 class TidyHtmlFields(Hook): |
|
43 """tidy HTML in rich text strings""" |
|
44 events = ('before_add_entity', 'before_update_entity') |
|
45 accepts = ('Any',) |
|
46 |
|
47 def call(self, session, entity): |
|
48 for formatattr, attr in entity.e_schema.format_fields.iteritems(): |
|
49 try: |
|
50 value = entity[attr] |
|
51 except KeyError: |
|
52 continue # no text to tidy |
|
53 if isinstance(value, unicode): # filter out None and Binary |
|
54 if self.event == 'before_add_entity': |
|
55 fmt = entity.get(formatattr) |
|
56 else: |
|
57 fmt = entity.get_value(formatattr) |
|
58 if fmt == 'text/html': |
|
59 entity[attr] = soup2xhtml(value, session.encoding) |
|
60 |