selectors.py
branchstable
changeset 7362 b9813c9d32ac
parent 7345 b9eaad6e34c3
child 7428 5338d895b891
equal deleted inserted replaced
7357:5ad3154a8810 7362:b9813c9d32ac
  1158         benefit from the ORM's request entities cache.
  1158         benefit from the ORM's request entities cache.
  1159 
  1159 
  1160     See :class:`~cubicweb.selectors.EntitySelector` documentation for entity
  1160     See :class:`~cubicweb.selectors.EntitySelector` documentation for entity
  1161     lookup / score rules according to the input context.
  1161     lookup / score rules according to the input context.
  1162     """
  1162     """
  1163     def __init__(self, expression, once_is_enough=False):
  1163     def __init__(self, expression, once_is_enough=False, user_condition=False):
  1164         super(rql_condition, self).__init__(once_is_enough)
  1164         super(rql_condition, self).__init__(once_is_enough)
  1165         if 'U' in frozenset(split_expression(expression)):
  1165         self.user_condition = user_condition
       
  1166         if user_condition:
       
  1167             rql = 'Any COUNT(U) WHERE U eid %%(u)s, %s' % expression
       
  1168         elif 'U' in frozenset(split_expression(expression)):
  1166             rql = 'Any COUNT(X) WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
  1169             rql = 'Any COUNT(X) WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression
  1167         else:
  1170         else:
  1168             rql = 'Any COUNT(X) WHERE X eid %%(x)s, %s' % expression
  1171             rql = 'Any COUNT(X) WHERE X eid %%(x)s, %s' % expression
  1169         self.rql = rql
  1172         self.rql = rql
  1170 
  1173 
  1171     def __str__(self):
  1174     def __str__(self):
  1172         return '%s(%r)' % (self.__class__.__name__, self.rql)
  1175         return '%s(%r)' % (self.__class__.__name__, self.rql)
       
  1176 
       
  1177     @lltrace
       
  1178     def __call__(self, cls, req, **kwargs):
       
  1179         if self.user_condition:
       
  1180             try:
       
  1181                 return req.execute(self.rql, {'u': req.user.eid})[0][0]
       
  1182             except Unauthorized:
       
  1183                 return 0
       
  1184         else:
       
  1185             return super(rql_condition, self).__call__(cls, req, **kwargs)
  1173 
  1186 
  1174     def _score(self, req, eid):
  1187     def _score(self, req, eid):
  1175         try:
  1188         try:
  1176             return req.execute(self.rql, {'x': eid, 'u': req.user.eid})[0][0]
  1189             return req.execute(self.rql, {'x': eid, 'u': req.user.eid})[0][0]
  1177         except Unauthorized:
  1190         except Unauthorized: