sobjects/hooks.py
author sylvain.thenault@logilab.fr
Thu, 12 Mar 2009 19:39:34 +0100
branchtls-sprint
changeset 1101 0c067de38e46
parent 1098 739d4dce9b19
child 1138 22f634977c95
permissions -rw-r--r--
unification of meta-attributes handling: * remove has_format, has_text_encoding, format, text_encoding methods on Entity * remove rich_text_fields method on EntitySchema * use instead has_metadata / meta_attribute on EntitySchema (from yams) and attribute_metadata on Entity
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""various library content hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
from cubicweb.server.hooksmanager import Hook
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from cubicweb.server.pool import PreCommitOperation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
1098
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    13
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
class AddUpdateEUserHook(Hook):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
    """ensure user logins are stripped"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
    events = ('before_add_entity', 'before_update_entity',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
    accepts = ('EUser',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
    def call(self, session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
        if 'login' in entity and entity['login']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
            entity['login'] = entity['login'].strip()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
class AutoDeleteBookmark(PreCommitOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
        session = self.session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
        if not self.beid in session.query_data('pendingeids', ()):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
            if not session.unsafe_execute('Any X WHERE X bookmarked_by U, X eid %(x)s',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
                                          {'x': self.beid}, 'x'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
                session.unsafe_execute('DELETE Bookmark X WHERE X eid %(x)s',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
                                       {'x': self.beid}, 'x')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
class DelBookmarkedByHook(Hook):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    """ensure user logins are stripped"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    events = ('after_delete_relation',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    accepts = ('bookmarked_by',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    def call(self, session, subj, rtype, obj):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        AutoDeleteBookmark(session, beid=subj)
1098
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    40
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
class TidyHtmlFields(Hook):
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    43
    """tidy HTML in rich text strings"""
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    44
    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
    45
    accepts = ('Any',)
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    46
739d4dce9b19 * turn tidy_html_hook into a regular application hook
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    47
    def call(self, session, entity):
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    48
        metaattrs = entity.e_schema.meta_attributes()
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    49
        for metaattr, (metadata, attr) in metaattrs.iteritems():
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    50
            if metadata == 'format':
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    51
                try:
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    52
                    value = entity[attr]
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    53
                except KeyError:
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    54
                    continue # no text to tidy
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    55
                if isinstance(value, unicode): # filter out None and Binary
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    56
                    if self.event == 'before_add_entity':
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    57
                        fmt = entity.get(metaattr)
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    58
                    else:
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    59
                        fmt = entity.get_value(metaattr)
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    60
                    if fmt == 'text/html':
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1098
diff changeset
    61
                        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
    62