selectors.py
branchstable
changeset 2161 200481e7b156
parent 2003 e194335569d1
child 2181 94ca417b9b07
child 2254 f632b06058c4
equal deleted inserted replaced
2160:17d624698661 2161:200481e7b156
   187     """
   187     """
   188     def __init__(self, once_is_enough=False):
   188     def __init__(self, once_is_enough=False):
   189         self.once_is_enough = once_is_enough
   189         self.once_is_enough = once_is_enough
   190 
   190 
   191     @lltrace
   191     @lltrace
   192     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   192     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   193         if not rset:
   193         if not rset:
   194             return 0
   194             return 0
   195         score = 0
   195         score = 0
   196         if row is None:
   196         if row is None:
   197             for etype in rset.column_types(col):
   197             for etype in rset.column_types(col):
   236     note: None values (resulting from some outer join in the query) are not
   236     note: None values (resulting from some outer join in the query) are not
   237           considered.
   237           considered.
   238     """
   238     """
   239 
   239 
   240     @lltrace
   240     @lltrace
   241     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   241     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   242         if not rset and not kwargs.get('entity'):
   242         if not rset and not kwargs.get('entity'):
   243             return 0
   243             return 0
   244         score = 0
   244         score = 0
   245         if kwargs.get('entity'):
   245         if kwargs.get('entity'):
   246             score = self.score_entity(kwargs['entity'])
   246             score = self.score_entity(kwargs['entity'])
   285     def __call__(self, *args, **kwargs):
   285     def __call__(self, *args, **kwargs):
   286         return self.score
   286         return self.score
   287 
   287 
   288 @objectify_selector
   288 @objectify_selector
   289 @lltrace
   289 @lltrace
   290 def none_rset(cls, req, rset, *args, **kwargs):
   290 def none_rset(cls, req, rset=None, *args, **kwargs):
   291     """accept no result set (e.g. given rset is None)"""
   291     """accept no result set (e.g. given rset is None)"""
   292     if rset is None:
   292     if rset is None:
   293         return 1
   293         return 1
   294     return 0
   294     return 0
   295 
   295 
   296 @objectify_selector
   296 @objectify_selector
   297 @lltrace
   297 @lltrace
   298 def any_rset(cls, req, rset, *args, **kwargs):
   298 def any_rset(cls, req, rset=None, *args, **kwargs):
   299     """accept result set, whatever the number of result it contains"""
   299     """accept result set, whatever the number of result it contains"""
   300     if rset is not None:
   300     if rset is not None:
   301         return 1
   301         return 1
   302     return 0
   302     return 0
   303 
   303 
   304 @objectify_selector
   304 @objectify_selector
   305 @lltrace
   305 @lltrace
   306 def nonempty_rset(cls, req, rset, *args, **kwargs):
   306 def nonempty_rset(cls, req, rset=None, *args, **kwargs):
   307     """accept any non empty result set"""
   307     """accept any non empty result set"""
   308     if rset is not None and rset.rowcount:
   308     if rset is not None and rset.rowcount:
   309         return 1
   309         return 1
   310     return 0
   310     return 0
   311 
   311 
   312 @objectify_selector
   312 @objectify_selector
   313 @lltrace
   313 @lltrace
   314 def empty_rset(cls, req, rset, *args, **kwargs):
   314 def empty_rset(cls, req, rset=None, *args, **kwargs):
   315     """accept empty result set"""
   315     """accept empty result set"""
   316     if rset is not None and rset.rowcount == 0:
   316     if rset is not None and rset.rowcount == 0:
   317         return 1
   317         return 1
   318     return 0
   318     return 0
   319 
   319 
   320 @objectify_selector
   320 @objectify_selector
   321 @lltrace
   321 @lltrace
   322 def one_line_rset(cls, req, rset, row=None, *args, **kwargs):
   322 def one_line_rset(cls, req, rset=None, row=None, *args, **kwargs):
   323     """if row is specified, accept result set with a single line of result,
   323     """if row is specified, accept result set with a single line of result,
   324     else accepts anyway
   324     else accepts anyway
   325     """
   325     """
   326     if rset is not None and (row is not None or rset.rowcount == 1):
   326     if rset is not None and (row is not None or rset.rowcount == 1):
   327         return 1
   327         return 1
   328     return 0
   328     return 0
   329 
   329 
   330 @objectify_selector
   330 @objectify_selector
   331 @lltrace
   331 @lltrace
   332 def two_lines_rset(cls, req, rset, *args, **kwargs):
   332 def two_lines_rset(cls, req, rset=None, *args, **kwargs):
   333     """accept result set with *at least* two lines of result"""
   333     """accept result set with *at least* two lines of result"""
   334     if rset is not None and rset.rowcount > 1:
   334     if rset is not None and rset.rowcount > 1:
   335         return 1
   335         return 1
   336     return 0
   336     return 0
   337 
   337 
   338 @objectify_selector
   338 @objectify_selector
   339 @lltrace
   339 @lltrace
   340 def two_cols_rset(cls, req, rset, *args, **kwargs):
   340 def two_cols_rset(cls, req, rset=None, *args, **kwargs):
   341     """accept result set with at least one line and two columns of result"""
   341     """accept result set with at least one line and two columns of result"""
   342     if rset is not None and rset.rowcount and len(rset.rows[0]) > 1:
   342     if rset is not None and rset.rowcount and len(rset.rows[0]) > 1:
   343         return 1
   343         return 1
   344     return 0
   344     return 0
   345 
   345 
   346 @objectify_selector
   346 @objectify_selector
   347 @lltrace
   347 @lltrace
   348 def paginated_rset(cls, req, rset, *args, **kwargs):
   348 def paginated_rset(cls, req, rset=None, *args, **kwargs):
   349     """accept result set with more lines than the page size.
   349     """accept result set with more lines than the page size.
   350 
   350 
   351     Page size is searched in (respecting order):
   351     Page size is searched in (respecting order):
   352     * a page_size argument
   352     * a page_size argument
   353     * a page_size form parameters
   353     * a page_size form parameters
   364         return 0
   364         return 0
   365     return 1
   365     return 1
   366 
   366 
   367 @objectify_selector
   367 @objectify_selector
   368 @lltrace
   368 @lltrace
   369 def sorted_rset(cls, req, rset, row=None, col=0, **kwargs):
   369 def sorted_rset(cls, req, rset=None, row=None, col=0, **kwargs):
   370     """accept sorted result set"""
   370     """accept sorted result set"""
   371     rqlst = rset.syntax_tree()
   371     rqlst = rset.syntax_tree()
   372     if len(rqlst.children) > 1 or not rqlst.children[0].orderby:
   372     if len(rqlst.children) > 1 or not rqlst.children[0].orderby:
   373         return 0
   373         return 0
   374     return 2
   374     return 2
   375 
   375 
   376 @objectify_selector
   376 @objectify_selector
   377 @lltrace
   377 @lltrace
   378 def one_etype_rset(cls, req, rset, row=None, col=0, *args, **kwargs):
   378 def one_etype_rset(cls, req, rset=None, row=None, col=0, *args, **kwargs):
   379     """accept result set where entities in the specified column (or 0) are all
   379     """accept result set where entities in the specified column (or 0) are all
   380     of the same type
   380     of the same type
   381     """
   381     """
   382     if rset is None:
   382     if rset is None:
   383         return 0
   383         return 0
   385         return 0
   385         return 0
   386     return 1
   386     return 1
   387 
   387 
   388 @objectify_selector
   388 @objectify_selector
   389 @lltrace
   389 @lltrace
   390 def two_etypes_rset(cls, req, rset, row=None, col=0, **kwargs):
   390 def two_etypes_rset(cls, req, rset=None, row=None, col=0, **kwargs):
   391     """accept result set where entities in the specified column (or 0) are not
   391     """accept result set where entities in the specified column (or 0) are not
   392     of the same type
   392     of the same type
   393     """
   393     """
   394     if rset:
   394     if rset:
   395         etypes = rset.column_types(col)
   395         etypes = rset.column_types(col)
   418 def anonymous_user():
   418 def anonymous_user():
   419     return ~ authenticated_user()
   419     return ~ authenticated_user()
   420 
   420 
   421 @objectify_selector
   421 @objectify_selector
   422 @lltrace
   422 @lltrace
   423 def primary_view(cls, req, rset, row=None, col=0, view=None, **kwargs):
   423 def primary_view(cls, req, rset=None, row=None, col=0, view=None, **kwargs):
   424     """accept if view given as named argument is a primary view, or if no view
   424     """accept if view given as named argument is a primary view, or if no view
   425     is given
   425     is given
   426     """
   426     """
   427     if view is not None and not view.is_primary():
   427     if view is not None and not view.is_primary():
   428         return 0
   428         return 0
   429     return 1
   429     return 1
   430 
   430 
   431 @objectify_selector
   431 @objectify_selector
   432 @lltrace
   432 @lltrace
   433 def match_context_prop(cls, req, rset, row=None, col=0, context=None,
   433 def match_context_prop(cls, req, rset=None, row=None, col=0, context=None,
   434                        **kwargs):
   434                        **kwargs):
   435     """accept if:
   435     """accept if:
   436     * no context given
   436     * no context given
   437     * context (`basestring`) is matching the context property value for the
   437     * context (`basestring`) is matching the context property value for the
   438       given cls
   438       given cls
   459     def __str__(self):
   459     def __str__(self):
   460         return '%s(%s)' % (self.__class__.__name__,
   460         return '%s(%s)' % (self.__class__.__name__,
   461                            ','.join(sorted(str(s) for s in self.expected)))
   461                            ','.join(sorted(str(s) for s in self.expected)))
   462 
   462 
   463     @lltrace
   463     @lltrace
   464     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   464     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   465         try:
   465         try:
   466             if not req.search_state[0] in self.expected:
   466             if not req.search_state[0] in self.expected:
   467                 return 0
   467                 return 0
   468         except AttributeError:
   468         except AttributeError:
   469             return 1 # class doesn't care about search state, accept it
   469             return 1 # class doesn't care about search state, accept it
   552 class match_view(match_search_state):
   552 class match_view(match_search_state):
   553     """accept if the current view is in one of the expected vid given to the
   553     """accept if the current view is in one of the expected vid given to the
   554     initializer
   554     initializer
   555     """
   555     """
   556     @lltrace
   556     @lltrace
   557     def __call__(self, cls, req, rset, row=None, col=0, view=None, **kwargs):
   557     def __call__(self, cls, req, rset=None, row=None, col=0, view=None, **kwargs):
   558         if view is None or not view.id in self.expected:
   558         if view is None or not view.id in self.expected:
   559             return 0
   559             return 0
   560         return 1
   560         return 1
   561 
   561 
   562 
   562 
   569     """
   569     """
   570     def __init__(self, registry, oid):
   570     def __init__(self, registry, oid):
   571         self.registry = registry
   571         self.registry = registry
   572         self.oid = oid
   572         self.oid = oid
   573 
   573 
   574     def __call__(self, cls, req, rset, *args, **kwargs):
   574     def __call__(self, cls, req, rset=None, *args, **kwargs):
   575         try:
   575         try:
   576             cls.vreg.select_object(self.registry, self.oid, req, rset, *args, **kwargs)
   576             cls.vreg.select_object(self.registry, self.oid, req, rset, *args, **kwargs)
   577             return 1
   577             return 1
   578         except NoSelectableObject:
   578         except NoSelectableObject:
   579             return 0
   579             return 0
   848     def __init__(self, action, once_is_enough=False):
   848     def __init__(self, action, once_is_enough=False):
   849         super(has_permission, self).__init__(once_is_enough)
   849         super(has_permission, self).__init__(once_is_enough)
   850         self.action = action
   850         self.action = action
   851 
   851 
   852     @lltrace
   852     @lltrace
   853     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   853     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   854         if rset is None:
   854         if rset is None:
   855             return 0
   855             return 0
   856         user = req.user
   856         user = req.user
   857         action = self.action
   857         action = self.action
   858         if row is None:
   858         if row is None:
   983 anonymous_selector = deprecated_function(anonymous_user)
   983 anonymous_selector = deprecated_function(anonymous_user)
   984 not_anonymous_selector = deprecated_function(authenticated_user)
   984 not_anonymous_selector = deprecated_function(authenticated_user)
   985 primaryview_selector = deprecated_function(primary_view)
   985 primaryview_selector = deprecated_function(primary_view)
   986 contextprop_selector = deprecated_function(match_context_prop)
   986 contextprop_selector = deprecated_function(match_context_prop)
   987 
   987 
   988 def nfentity_selector(cls, req, rset, row=None, col=0, **kwargs):
   988 def nfentity_selector(cls, req, rset=None, row=None, col=0, **kwargs):
   989     return non_final_entity()(cls, req, rset, row, col)
   989     return non_final_entity()(cls, req, rset, row, col)
   990 nfentity_selector = deprecated_function(nfentity_selector)
   990 nfentity_selector = deprecated_function(nfentity_selector)
   991 
   991 
   992 def implement_interface(cls, req, rset, row=None, col=0, **kwargs):
   992 def implement_interface(cls, req, rset=None, row=None, col=0, **kwargs):
   993     return implements(*cls.accepts_interfaces)(cls, req, rset, row, col)
   993     return implements(*cls.accepts_interfaces)(cls, req, rset, row, col)
   994 _interface_selector = deprecated_function(implement_interface)
   994 _interface_selector = deprecated_function(implement_interface)
   995 interface_selector = deprecated_function(implement_interface)
   995 interface_selector = deprecated_function(implement_interface)
   996 implement_interface = deprecated_function(implement_interface, 'use implements')
   996 implement_interface = deprecated_function(implement_interface, 'use implements')
   997 
   997 
   999     """check etype presence in request form *and* accepts conformance"""
   999     """check etype presence in request form *and* accepts conformance"""
  1000     return specified_etype_implements(*cls.accepts)(cls, req, *args)
  1000     return specified_etype_implements(*cls.accepts)(cls, req, *args)
  1001 etype_form_selector = deprecated_function(accept_etype)
  1001 etype_form_selector = deprecated_function(accept_etype)
  1002 accept_etype = deprecated_function(accept_etype, 'use specified_etype_implements')
  1002 accept_etype = deprecated_function(accept_etype, 'use specified_etype_implements')
  1003 
  1003 
  1004 def searchstate_selector(cls, req, rset, row=None, col=0, **kwargs):
  1004 def searchstate_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1005     return match_search_state(cls.search_states)(cls, req, rset, row, col)
  1005     return match_search_state(cls.search_states)(cls, req, rset, row, col)
  1006 searchstate_selector = deprecated_function(searchstate_selector)
  1006 searchstate_selector = deprecated_function(searchstate_selector)
  1007 
  1007 
  1008 def match_user_group(cls, req, rset=None, row=None, col=0, **kwargs):
  1008 def match_user_group(cls, req, rset=None, row=None, col=0, **kwargs):
  1009     return match_user_groups(*cls.require_groups)(cls, req, rset, row, col, **kwargs)
  1009     return match_user_groups(*cls.require_groups)(cls, req, rset, row, col, **kwargs)
  1010 in_group_selector = deprecated_function(match_user_group)
  1010 in_group_selector = deprecated_function(match_user_group)
  1011 match_user_group = deprecated_function(match_user_group)
  1011 match_user_group = deprecated_function(match_user_group)
  1012 
  1012 
  1013 def has_relation(cls, req, rset, row=None, col=0, **kwargs):
  1013 def has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1014     return relation_possible(cls.rtype, role(cls), cls.etype,
  1014     return relation_possible(cls.rtype, role(cls), cls.etype,
  1015                              getattr(cls, 'require_permission', 'read'))(cls, req, rset, row, col, **kwargs)
  1015                              getattr(cls, 'require_permission', 'read'))(cls, req, rset, row, col, **kwargs)
  1016 has_relation = deprecated_function(has_relation)
  1016 has_relation = deprecated_function(has_relation)
  1017 
  1017 
  1018 def one_has_relation(cls, req, rset, row=None, col=0, **kwargs):
  1018 def one_has_relation(cls, req, rset=None, row=None, col=0, **kwargs):
  1019     return relation_possible(cls.rtype, role(cls), cls.etype,
  1019     return relation_possible(cls.rtype, role(cls), cls.etype,
  1020                              getattr(cls, 'require_permission', 'read',
  1020                              getattr(cls, 'require_permission', 'read',
  1021                                      once_is_enough=True))(cls, req, rset, row, col, **kwargs)
  1021                                      once_is_enough=True))(cls, req, rset, row, col, **kwargs)
  1022 one_has_relation = deprecated_function(one_has_relation, 'use relation_possible selector')
  1022 one_has_relation = deprecated_function(one_has_relation, 'use relation_possible selector')
  1023 
  1023 
  1024 def accept_rset(cls, req, rset, row=None, col=0, **kwargs):
  1024 def accept_rset(cls, req, rset=None, row=None, col=0, **kwargs):
  1025     """simply delegate to cls.accept_rset method"""
  1025     """simply delegate to cls.accept_rset method"""
  1026     return implements(*cls.accepts)(cls, req, rset, row=row, col=col)
  1026     return implements(*cls.accepts)(cls, req, rset, row=row, col=col)
  1027 accept_rset_selector = deprecated_function(accept_rset)
  1027 accept_rset_selector = deprecated_function(accept_rset)
  1028 accept_rset = deprecated_function(accept_rset, 'use implements selector')
  1028 accept_rset = deprecated_function(accept_rset, 'use implements selector')
  1029 
  1029 
  1034 accept_one = deprecated_function(chainall(one_line_rset, accept,
  1034 accept_one = deprecated_function(chainall(one_line_rset, accept,
  1035                                           name='accept_one'))
  1035                                           name='accept_one'))
  1036 accept_one_selector = deprecated_function(accept_one)
  1036 accept_one_selector = deprecated_function(accept_one)
  1037 
  1037 
  1038 
  1038 
  1039 def _rql_condition(cls, req, rset, row=None, col=0, **kwargs):
  1039 def _rql_condition(cls, req, rset=None, row=None, col=0, **kwargs):
  1040     if cls.condition:
  1040     if cls.condition:
  1041         return rql_condition(cls.condition)(cls, req, rset, row, col)
  1041         return rql_condition(cls.condition)(cls, req, rset, row, col)
  1042     return 1
  1042     return 1
  1043 _rqlcondition_selector = deprecated_function(_rql_condition)
  1043 _rqlcondition_selector = deprecated_function(_rql_condition)
  1044 
  1044 
  1045 rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition,
  1045 rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition,
  1046                          name='rql_condition'))
  1046                          name='rql_condition'))
  1047 
  1047 
  1048 def but_etype_selector(cls, req, rset, row=None, col=0, **kwargs):
  1048 def but_etype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1049     return but_etype(cls.etype)(cls, req, rset, row, col)
  1049     return but_etype(cls.etype)(cls, req, rset, row, col)
  1050 but_etype_selector = deprecated_function(but_etype_selector)
  1050 but_etype_selector = deprecated_function(but_etype_selector)
  1051 
  1051 
  1052 @lltrace
  1052 @lltrace
  1053 def etype_rtype_selector(cls, req, rset, row=None, col=0, **kwargs):
  1053 def etype_rtype_selector(cls, req, rset=None, row=None, col=0, **kwargs):
  1054     schema = cls.schema
  1054     schema = cls.schema
  1055     perm = getattr(cls, 'require_permission', 'read')
  1055     perm = getattr(cls, 'require_permission', 'read')
  1056     if hasattr(cls, 'etype'):
  1056     if hasattr(cls, 'etype'):
  1057         eschema = schema.eschema(cls.etype)
  1057         eschema = schema.eschema(cls.etype)
  1058         if not (eschema.has_perm(req, perm) or eschema.has_local_role(perm)):
  1058         if not (eschema.has_perm(req, perm) or eschema.has_local_role(perm)):