selectors.py
changeset 2613 5e19c2bb370e
parent 2514 c745dfb2734e
child 2650 18aec79ec3a3
equal deleted inserted replaced
2612:c3059d308dc2 2613:5e19c2bb370e
    44 
    44 
    45 import logging
    45 import logging
    46 from warnings import warn
    46 from warnings import warn
    47 
    47 
    48 from logilab.common.compat import all
    48 from logilab.common.compat import all
    49 from logilab.common.deprecation import deprecated_function
    49 from logilab.common.deprecation import deprecated
    50 from logilab.common.interface import implements as implements_iface
    50 from logilab.common.interface import implements as implements_iface
    51 
    51 
    52 from yams import BASE_TYPES
    52 from yams import BASE_TYPES
    53 
    53 
    54 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
    54 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
   974         self.score_entity = scorefunc
   974         self.score_entity = scorefunc
   975 
   975 
   976 
   976 
   977 # XXX DEPRECATED ##############################################################
   977 # XXX DEPRECATED ##############################################################
   978 
   978 
   979 yes_selector = deprecated_function(yes)
   979 yes_selector = deprecated()(yes)
   980 norset_selector = deprecated_function(none_rset)
   980 norset_selector = deprecated()(none_rset)
   981 rset_selector = deprecated_function(any_rset)
   981 rset_selector = deprecated()(any_rset)
   982 anyrset_selector = deprecated_function(nonempty_rset)
   982 anyrset_selector = deprecated()(nonempty_rset)
   983 emptyrset_selector = deprecated_function(empty_rset)
   983 emptyrset_selector = deprecated()(empty_rset)
   984 onelinerset_selector = deprecated_function(one_line_rset)
   984 onelinerset_selector = deprecated()(one_line_rset)
   985 twolinerset_selector = deprecated_function(two_lines_rset)
   985 twolinerset_selector = deprecated()(two_lines_rset)
   986 twocolrset_selector = deprecated_function(two_cols_rset)
   986 twocolrset_selector = deprecated()(two_cols_rset)
   987 largerset_selector = deprecated_function(paginated_rset)
   987 largerset_selector = deprecated()(paginated_rset)
   988 sortedrset_selector = deprecated_function(sorted_rset)
   988 sortedrset_selector = deprecated()(sorted_rset)
   989 oneetyperset_selector = deprecated_function(one_etype_rset)
   989 oneetyperset_selector = deprecated()(one_etype_rset)
   990 multitype_selector = deprecated_function(two_etypes_rset)
   990 multitype_selector = deprecated()(two_etypes_rset)
   991 anonymous_selector = deprecated_function(anonymous_user)
   991 anonymous_selector = deprecated()(anonymous_user)
   992 not_anonymous_selector = deprecated_function(authenticated_user)
   992 not_anonymous_selector = deprecated()(authenticated_user)
   993 primaryview_selector = deprecated_function(primary_view)
   993 primaryview_selector = deprecated()(primary_view)
   994 contextprop_selector = deprecated_function(match_context_prop)
   994 contextprop_selector = deprecated()(match_context_prop)
   995 
   995 
       
   996 @deprecated('use non_final_entity instead of %s')
   996 def nfentity_selector(cls, req, rset=None, row=None, col=0, **kwargs):
   997 def nfentity_selector(cls, req, rset=None, row=None, col=0, **kwargs):
   997     return non_final_entity()(cls, req, rset, row, col)
   998     return non_final_entity()(cls, req, rset, row, col)
   998 nfentity_selector = deprecated_function(nfentity_selector)
   999 
   999 
  1000 @deprecated('use implements instead of %s')
  1000 def implement_interface(cls, req, rset=None, row=None, col=0, **kwargs):
  1001 def implement_interface(cls, req, rset=None, row=None, col=0, **kwargs):
  1001     return implements(*cls.accepts_interfaces)(cls, req, rset, row, col)
  1002     return implements(*cls.accepts_interfaces)(cls, req, rset, row, col)
  1002 _interface_selector = deprecated_function(implement_interface)
  1003 _interface_selector = deprecated()(implement_interface)
  1003 interface_selector = deprecated_function(implement_interface)
  1004 interface_selector = deprecated()(implement_interface)
  1004 implement_interface = deprecated_function(implement_interface, 'use implements')
  1005 
  1005 
  1006 @deprecated('use specified_etype_implements instead of %s')
  1006 def accept_etype(cls, req, *args, **kwargs):
  1007 def accept_etype(cls, req, *args, **kwargs):
  1007     """check etype presence in request form *and* accepts conformance"""
  1008     """check etype presence in request form *and* accepts conformance"""
  1008     return specified_etype_implements(*cls.accepts)(cls, req, *args)
  1009     return specified_etype_implements(*cls.accepts)(cls, req, *args)
  1009 etype_form_selector = deprecated_function(accept_etype)
  1010 etype_form_selector = accept_etype
  1010 accept_etype = deprecated_function(accept_etype, 'use specified_etype_implements')
  1011 
  1011 
  1012 @deprecated('use match_search_state instead of %s')
  1012 def searchstate_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1013 def searchstate_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1013     return match_search_state(cls.search_states)(cls, req, rset, row, col)
  1014     return match_search_state(cls.search_states)(cls, req, rset, row, col)
  1014 searchstate_selector = deprecated_function(searchstate_selector)
  1015 
  1015 
  1016 @deprecated('use match_user_groups instead of %s')
  1016 def match_user_group(cls, req, rset=None, row=None, col=0, **kwargs):
  1017 def match_user_group(cls, req, rset=None, row=None, col=0, **kwargs):
  1017     return match_user_groups(*cls.require_groups)(cls, req, rset, row, col, **kwargs)
  1018     return match_user_groups(*cls.require_groups)(cls, req, rset, row, col, **kwargs)
  1018 in_group_selector = deprecated_function(match_user_group)
  1019 in_group_selector = match_user_group
  1019 match_user_group = deprecated_function(match_user_group)
  1020 
  1020 
  1021 @deprecated('use relation_possible instead of %s')
  1021 def has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1022 def has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1022     return relation_possible(cls.rtype, role(cls), cls.etype,
  1023     return relation_possible(cls.rtype, role(cls), cls.etype,
  1023                              getattr(cls, 'require_permission', 'read'))(cls, req, rset, row, col, **kwargs)
  1024                              getattr(cls, 'require_permission', 'read'))(cls, req, rset, row, col, **kwargs)
  1024 has_relation = deprecated_function(has_relation)
  1025 
  1025 
  1026 @deprecated('use relation_possible instead of %s')
  1026 def one_has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1027 def one_has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1027     return relation_possible(cls.rtype, role(cls), cls.etype,
  1028     return relation_possible(cls.rtype, role(cls), cls.etype,
  1028                              getattr(cls, 'require_permission', 'read',
  1029                              getattr(cls, 'require_permission', 'read',
  1029                                      once_is_enough=True))(cls, req, rset, row, col, **kwargs)
  1030                                      once_is_enough=True))(cls, req, rset, row, col, **kwargs)
  1030 one_has_relation = deprecated_function(one_has_relation, 'use relation_possible selector')
  1031 
  1031 
  1032 @deprecated('use implements instead of %s')
  1032 def accept_rset(cls, req, rset=None, row=None, col=0, **kwargs):
  1033 def accept_rset(cls, req, rset=None, row=None, col=0, **kwargs):
  1033     """simply delegate to cls.accept_rset method"""
  1034     """simply delegate to cls.accept_rset method"""
  1034     return implements(*cls.accepts)(cls, req, rset, row=row, col=col)
  1035     return implements(*cls.accepts)(cls, req, rset, row=row, col=col)
  1035 accept_rset_selector = deprecated_function(accept_rset)
  1036 accept_rset_selector = accept_rset
  1036 accept_rset = deprecated_function(accept_rset, 'use implements selector')
       
  1037 
  1037 
  1038 accept = chainall(non_final_entity(), accept_rset, name='accept')
  1038 accept = chainall(non_final_entity(), accept_rset, name='accept')
  1039 accept_selector = deprecated_function(accept)
  1039 accept = deprecated('use implements selector')(accept)
  1040 accept = deprecated_function(accept, 'use implements selector')
  1040 accept_selector = deprecated()(accept)
  1041 
  1041 
  1042 accept_one = deprecated_function(chainall(one_line_rset, accept,
  1042 accept_one = deprecated()(chainall(one_line_rset, accept,
  1043                                           name='accept_one'))
  1043                                           name='accept_one'))
  1044 accept_one_selector = deprecated_function(accept_one)
  1044 accept_one_selector = deprecated()(accept_one)
  1045 
  1045 
  1046 
  1046 
  1047 def _rql_condition(cls, req, rset=None, row=None, col=0, **kwargs):
  1047 def _rql_condition(cls, req, rset=None, row=None, col=0, **kwargs):
  1048     if cls.condition:
  1048     if cls.condition:
  1049         return rql_condition(cls.condition)(cls, req, rset, row, col)
  1049         return rql_condition(cls.condition)(cls, req, rset, row, col)
  1050     return 1
  1050     return 1
  1051 _rqlcondition_selector = deprecated_function(_rql_condition)
  1051 _rqlcondition_selector = deprecated()(_rql_condition)
  1052 
  1052 
  1053 rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition,
  1053 rqlcondition_selector = deprecated()(chainall(non_final_entity(), one_line_rset, _rql_condition,
  1054                          name='rql_condition'))
  1054                          name='rql_condition'))
  1055 
  1055 
       
  1056 @deprecated('use but_etype instead of %s')
  1056 def but_etype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1057 def but_etype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1057     return but_etype(cls.etype)(cls, req, rset, row, col)
  1058     return but_etype(cls.etype)(cls, req, rset, row, col)
  1058 but_etype_selector = deprecated_function(but_etype_selector)
       
  1059 
  1059 
  1060 @lltrace
  1060 @lltrace
  1061 def etype_rtype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1061 def etype_rtype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1062     schema = cls.schema
  1062     schema = cls.schema
  1063     perm = getattr(cls, 'require_permission', 'read')
  1063     perm = getattr(cls, 'require_permission', 'read')
  1068     if hasattr(cls, 'rtype'):
  1068     if hasattr(cls, 'rtype'):
  1069         rschema = schema.rschema(cls.rtype)
  1069         rschema = schema.rschema(cls.rtype)
  1070         if not (rschema.has_perm(req, perm) or rschema.has_local_role(perm)):
  1070         if not (rschema.has_perm(req, perm) or rschema.has_local_role(perm)):
  1071             return 0
  1071             return 0
  1072     return 1
  1072     return 1
  1073 etype_rtype_selector = deprecated_function(etype_rtype_selector)
  1073 etype_rtype_selector = deprecated()(etype_rtype_selector)
  1074 
  1074 
  1075 #req_form_params_selector = deprecated_function(match_form_params) # form_params
  1075 #req_form_params_selector = deprecated()(match_form_params) # form_params
  1076 #kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs
  1076 #kwargs_selector = deprecated()(match_kwargs) # expected_kwargs
  1077 
  1077 
  1078 # compound selectors ##########################################################
  1078 # compound selectors ##########################################################
  1079 
  1079 
  1080 searchstate_accept = chainall(nonempty_rset(), accept,
  1080 searchstate_accept = chainall(nonempty_rset(), accept,
  1081                               name='searchstate_accept')
  1081                               name='searchstate_accept')
  1082 searchstate_accept_selector = deprecated_function(searchstate_accept)
  1082 searchstate_accept_selector = deprecated()(searchstate_accept)
  1083 
  1083 
  1084 searchstate_accept_one = chainall(one_line_rset, accept, _rql_condition,
  1084 searchstate_accept_one = chainall(one_line_rset, accept, _rql_condition,
  1085                                   name='searchstate_accept_one')
  1085                                   name='searchstate_accept_one')
  1086 searchstate_accept_one_selector = deprecated_function(searchstate_accept_one)
  1086 searchstate_accept_one_selector = deprecated()(searchstate_accept_one)
  1087 
  1087 
  1088 searchstate_accept = deprecated_function(searchstate_accept)
  1088 searchstate_accept = deprecated()(searchstate_accept)
  1089 searchstate_accept_one = deprecated_function(searchstate_accept_one)
  1089 searchstate_accept_one = deprecated()(searchstate_accept_one)
  1090 
  1090 
       
  1091 # end of deprecation section ##################################################
  1091 
  1092 
  1092 def unbind_method(selector):
  1093 def unbind_method(selector):
  1093     def new_selector(registered):
  1094     def new_selector(registered):
  1094         # get the unbound method
  1095         # get the unbound method
  1095         if hasattr(registered, 'im_func'):
  1096         if hasattr(registered, 'im_func'):