selectors.py
branchtls-sprint
changeset 766 33ede72b22b8
parent 764 9de67b681624
child 769 2b49c9932d8c
equal deleted inserted replaced
765:8fda14081686 766:33ede72b22b8
   382     
   382     
   383     :param expected: either 'normal' or 'linksearch' (eg searching for an
   383     :param expected: either 'normal' or 'linksearch' (eg searching for an
   384                      object to create a relation with another)
   384                      object to create a relation with another)
   385     """
   385     """
   386     def __init__(self, *expected):
   386     def __init__(self, *expected):
   387         self.expected = expected
   387         assert expected
       
   388         self.expected = frozenset(expected)
   388         
   389         
   389     @lltrace
   390     @lltrace
   390     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   391     def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
   391         try:
   392         try:
   392             if not req.search_state[0] in self.expected:
   393             if not req.search_state[0] in self.expected:
   429             if not arg in kwargs:
   430             if not arg in kwargs:
   430                 return 0
   431                 return 0
   431         return len(self.expected)
   432         return len(self.expected)
   432 
   433 
   433 
   434 
   434 class match_user_groups(Selector):
   435 class match_user_groups(match_search_state):
   435     """accept if logged users is in at least one of the given groups. Returned
   436     """accept if logged users is in at least one of the given groups. Returned
   436     score is the number of groups in which the user is.
   437     score is the number of groups in which the user is.
   437     
   438     
   438     If the special 'owners' group is given:
   439     If the special 'owners' group is given:
   439     * if row is specified check the entity at the given row/col is owned by the
   440     * if row is specified check the entity at the given row/col is owned by the
   443 
   444 
   444     :param *required_groups: name of groups (`basestring`) in which the logged
   445     :param *required_groups: name of groups (`basestring`) in which the logged
   445                              user should be
   446                              user should be
   446     """
   447     """
   447     
   448     
   448     def __init__(self, *required_groups):
       
   449         self.required_groups = required_groups
       
   450     
       
   451     @lltrace
   449     @lltrace
   452     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   450     def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
   453         user = req.user
   451         user = req.user
       
   452         print 'match_user_groups', user.login, user._groups, self.expected
   454         if user is None:
   453         if user is None:
   455             return int('guests' in self.required_groups)
   454             return int('guests' in self.expected)
   456         score = user.matching_groups(self.required_groups)
   455         score = user.matching_groups(self.expected)
   457         if not score and 'owners' in self.required_groups and rset:
   456         if not score and 'owners' in self.expected and rset:
   458             nbowned = 0
       
   459             if row is not None:
   457             if row is not None:
   460                 if not user.owns(rset[row][col]):
   458                 if not user.owns(rset[row][col]):
   461                     return 0
   459                     return 0
   462                 score = 1
   460                 score = 1
   463             else:
   461             else:
   464                 score = all(user.owns(r[col or 0]) for r in rset)
   462                 score = all(user.owns(r[col]) for r in rset)
   465         return 0
   463         return score
   466 
   464 
   467 
   465 
   468 class appobject_selectable(Selector):
   466 class appobject_selectable(Selector):
   469     """accept with another appobject is selectable using selector's input
   467     """accept with another appobject is selectable using selector's input
   470     context.
   468     context.
   759                        to represent the logged user
   757                        to represent the logged user
   760 
   758 
   761     return the sum of the number of items returned by the rql condition as score
   759     return the sum of the number of items returned by the rql condition as score
   762     or 0 at the first entity scoring to zero.
   760     or 0 at the first entity scoring to zero.
   763     """
   761     """
   764     def __init__(self, expression):
   762     def __init__(self, expression, once_is_enough=False):
       
   763         super(rql_condition, self).__init__(once_is_enough)
   765         if 'U' in frozenset(split_expression(expression)):
   764         if 'U' in frozenset(split_expression(expression)):
   766             rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
   765             rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
   767         else:
   766         else:
   768             rql = 'Any X WHERE X eid %%(x)s, %s' % expression
   767             rql = 'Any X WHERE X eid %%(x)s, %s' % expression
   769         self.rql = rql
   768         self.rql = rql
   911 #req_form_params_selector = deprecated_function(match_form_params) # form_params
   910 #req_form_params_selector = deprecated_function(match_form_params) # form_params
   912 #kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs
   911 #kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs
   913 
   912 
   914 # compound selectors ##########################################################
   913 # compound selectors ##########################################################
   915 
   914 
   916 searchstate_accept = chainall(nonempty_rset, match_search_state, accept,
   915 searchstate_accept = chainall(nonempty_rset(), accept,
   917                               name='searchstate_accept')
   916                               name='searchstate_accept')
   918 searchstate_accept_selector = deprecated_function(searchstate_accept)
   917 searchstate_accept_selector = deprecated_function(searchstate_accept)
   919 
   918 
   920 searchstate_accept_one = chainall(one_line_rset, match_search_state,
   919 searchstate_accept_one = chainall(one_line_rset, accept, _rql_condition,
   921                                   accept, _rql_condition,
       
   922                                   name='searchstate_accept_one')
   920                                   name='searchstate_accept_one')
   923 searchstate_accept_one_selector = deprecated_function(searchstate_accept_one)
   921 searchstate_accept_one_selector = deprecated_function(searchstate_accept_one)
   924 
   922 
   925 searchstate_accept = deprecated_function(searchstate_accept)
   923 searchstate_accept = deprecated_function(searchstate_accept)
   926 searchstate_accept_one = deprecated_function(searchstate_accept_one)
   924 searchstate_accept_one = deprecated_function(searchstate_accept_one)