selectors.py
branchtls-sprint
changeset 649 e5956e9ebef1
parent 640 8e64f12be69c
child 650 75b4c661f71b
equal deleted inserted replaced
648:a89ba1054cb2 649:e5956e9ebef1
   615     def __init__(self, scorefunc):
   615     def __init__(self, scorefunc):
   616         self.score_entity = scorefunc
   616         self.score_entity = scorefunc
   617 
   617 
   618 
   618 
   619 class rql_condition(EntitySelector):
   619 class rql_condition(EntitySelector):
       
   620     """initializer takes a rql expression as argument (which should use X
       
   621     variable to represent the context entity).
       
   622 
       
   623     return the sum of the number of items returned by the rql condition as score
       
   624     or 0 at the first entity scoring to zero.
       
   625     """
   620     def __init__(self, expression):
   626     def __init__(self, expression):
   621         if 'U' in frozenset(split_expression(cls.condition)):
   627         if 'U' in frozenset(split_expression(expression)):
   622             rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
   628             rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
   623         else:
   629         else:
   624             rql = 'Any X WHERE X eid %%(x)s, %s' % expression
   630             rql = 'Any X WHERE X eid %%(x)s, %s' % expression
   625         self.rql = rql
   631         self.rql = rql
   626         
   632         
   627     def score(self, req, rset, row, col):
   633     def score(self, req, rset, row, col):
   628         try:
   634         try:
   629             return len(req.execute(self.rql, {'x': eid, 'u': req.user.eid}, 'x'))
   635             return len(req.execute(self.rql, {'x': eid, 'u': req.user.eid}, 'x'))
   630         except Unauthorized:
   636         except Unauthorized:
   631             return 0
   637             return 0
   632         
   638 
       
   639         
       
   640 class but_etype(EntitySelector):
       
   641     """initializer takes an entity type as argument.
       
   642 
       
   643     return 0 if an entity type is this type, else 1.
       
   644     """
       
   645     def __init__(self, etype):
       
   646         self.etype = etype
       
   647         
       
   648     def score(self, req, rset, row, col):
       
   649         if rset.description[row][col] == self.etype:
       
   650             return 0
       
   651         return 1
       
   652 
       
   653 
       
   654 class appobject_selectable(Selector):
       
   655     """initializer takes a registry and oid of another vobject
       
   656     
       
   657     return 1 if the given registry and object is selectable using selector's
       
   658     input context, else 0
       
   659     """
       
   660     def __init__(self, registry, oid):
       
   661         self.registry = registry
       
   662         self.oid = oid
       
   663         
       
   664     def __call__(self, cls, req, rset, *args, **kwargs):
       
   665         try:
       
   666             cls.vreg.select_object(self.registry, self.oid, req, rset, *args, **kwargs)
       
   667             return 1
       
   668         except NoSelectableObject:
       
   669             return 0
       
   670 
   633         
   671         
   634 # XXX not so basic selectors ######################################################
   672 # XXX not so basic selectors ######################################################
   635         
       
   636 @lltrace
       
   637 def but_etype(cls, req, rset, row=None, col=0, **kwargs):
       
   638     """restrict the searchstate_accept_one_selector to exclude entity's type
       
   639     refered by the .etype attribute
       
   640     """
       
   641     if rset.description[row or 0][col or 0] == cls.etype:
       
   642         return 0
       
   643     return 1
       
   644 but_etype_selector = deprecated_function(but_etype)
       
   645 
   673 
   646 @lltrace
   674 @lltrace
   647 def etype_rtype_selector(cls, req, rset, row=None, col=0, **kwargs):
   675 def etype_rtype_selector(cls, req, rset, row=None, col=0, **kwargs):
   648     """only check if the user has read access on the entity's type refered
   676     """only check if the user has read access on the entity's type refered
   649     by the .etype attribute and on the relations's type refered by the
   677     by the .etype attribute and on the relations's type refered by the
   685         return 0
   713         return 0
   686     return 1
   714     return 1
   687 contextprop_selector = deprecated_function(match_context_prop)
   715 contextprop_selector = deprecated_function(match_context_prop)
   688 
   716 
   689 @lltrace
   717 @lltrace
   690 def primary_view(cls, req, rset, row=None, col=0, view=None,
   718 def primary_view(cls, req, rset, row=None, col=0, view=None, **kwargs):
   691                           **kwargs):
       
   692     if view is not None and not view.is_primary():
   719     if view is not None and not view.is_primary():
   693         return 0
   720         return 0
   694     return 1
   721     return 1
   695 primaryview_selector = deprecated_function(primary_view)
   722 primaryview_selector = deprecated_function(primary_view)
   696 
       
   697 def appobject_selectable(registry, oid):
       
   698     """return a selector that will have a positive score if an object for the
       
   699     given registry and object id is selectable for the input context
       
   700     """
       
   701     @lltrace
       
   702     def selector(cls, req, rset, *args, **kwargs):
       
   703         try:
       
   704             cls.vreg.select_object(registry, oid, req, rset, *args, **kwargs)
       
   705             return 1
       
   706         except NoSelectableObject:
       
   707             return 0
       
   708     return selector
       
   709 
   723 
   710 
   724 
   711 
   725 
   712 # XXX DEPRECATED ##############################################################
   726 # XXX DEPRECATED ##############################################################
   713 
   727 
   760 accept_one = deprecated_function(chainall(one_line_rset, accept,
   774 accept_one = deprecated_function(chainall(one_line_rset, accept,
   761                                           name='accept_one'))
   775                                           name='accept_one'))
   762 accept_one_selector = deprecated_function(accept_one)
   776 accept_one_selector = deprecated_function(accept_one)
   763 
   777 
   764 
   778 
   765 @lltrace
       
   766 def _rql_condition(cls, req, rset, row=None, col=0, **kwargs):
   779 def _rql_condition(cls, req, rset, row=None, col=0, **kwargs):
   767     if cls.condition:
   780     if cls.condition:
   768         return rql_condition(cls.condition)(cls, req, rset, row, col)
   781         return rql_condition(cls.condition)(cls, req, rset, row, col)
   769     return 1
   782     return 1
   770 _rqlcondition_selector = deprecated_function(_rql_condition)
   783 _rqlcondition_selector = deprecated_function(_rql_condition)
   771 
   784 
   772 rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition,
   785 rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition,
   773                          name='rql_condition'))
   786                          name='rql_condition'))
       
   787     
       
   788 def but_etype_selector(cls, req, rset, row=None, col=0, **kwargs):
       
   789     return but_etype(cls.etype)(cls, req, rset, row, col)
       
   790 but_etype_selector = deprecated_function(but_etype_selector)
       
   791 
   774 
   792 
   775 #req_form_params_selector = deprecated_function(match_form_params) # form_params
   793 #req_form_params_selector = deprecated_function(match_form_params) # form_params
   776 #kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs
   794 #kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs
   777 
   795 
   778 # compound selectors ##########################################################
   796 # compound selectors ##########################################################