server/hooks.py
branchtls-sprint
changeset 1098 739d4dce9b19
parent 1016 26387b836099
child 1132 96752791c2b6
equal deleted inserted replaced
1097:611bacbbe001 1098:739d4dce9b19
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 from datetime import datetime
    10 from datetime import datetime
    11 
    11 
    12 from cubicweb import UnknownProperty, ValidationError, BadConnectionId
    12 from cubicweb import UnknownProperty, ValidationError, BadConnectionId
    13 
       
    14 from cubicweb.common.uilib import soup2xhtml
       
    15 
    13 
    16 from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation
    14 from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation
    17 from cubicweb.server.hookhelper import (check_internal_entity, previous_state,
    15 from cubicweb.server.hookhelper import (check_internal_entity, previous_state,
    18                                      get_user_sessions, rproperty)
    16                                      get_user_sessions, rproperty)
    19 from cubicweb.server.repository import FTIndexEntityOp
    17 from cubicweb.server.repository import FTIndexEntityOp
   205                 msg = session._('the value "%s" is already used, use another one')
   203                 msg = session._('the value "%s" is already used, use another one')
   206                 raise ValidationError(entity.eid, {attr: msg % val})
   204                 raise ValidationError(entity.eid, {attr: msg % val})
   207 
   205 
   208 
   206 
   209 
   207 
   210 
       
   211 class tidy_html_fields(object):
       
   212     """tidy HTML in rich text strings
       
   213 
       
   214     FIXME: (adim) the whole idea of having a class is to store the
       
   215     event type. There might be another way to get dynamically the
       
   216     event inside the hook function.
       
   217     """
       
   218     # FIXME hooks manager use func_name to register
       
   219     func_name = 'tidy_html_field'
       
   220     
       
   221     def __init__(self, event):
       
   222         self.event = event
       
   223 
       
   224     def __call__(self, session, entity):
       
   225         for attr in entity.formatted_attrs():
       
   226             value = entity.get(attr)
       
   227             # text was not changed
       
   228             if self.event == 'before_add_entity':
       
   229                 fmt = entity.get('%s_format' % attr)
       
   230             else:
       
   231                 fmt = entity.get_value('%s_format' % attr)
       
   232             if value and fmt == 'text/html':
       
   233                 entity[attr] = soup2xhtml(value, session.encoding)
       
   234 
   208 
   235 
   209 
   236 class CheckRequiredRelationOperation(LateOperation):
   210 class CheckRequiredRelationOperation(LateOperation):
   237     """checking relation cardinality has to be done after commit in
   211     """checking relation cardinality has to be done after commit in
   238     case the relation is being replaced
   212     case the relation is being replaced
   313     hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
   287     hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
   314     hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
   288     hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
   315     hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '')
   289     hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '')
   316     hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
   290     hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
   317     hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
   291     hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
   318     hm.register_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '')
       
   319     hm.register_hook(tidy_html_fields('before_update_entity'), 'before_update_entity', '')
       
   320 
   292 
   321 
   293 
   322 # user/groups synchronisation #################################################
   294 # user/groups synchronisation #################################################
   323             
   295             
   324 class GroupOperation(Operation):
   296 class GroupOperation(Operation):