14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """Base class for entity objects manipulated in clients""" |
18 """Base class for entity objects manipulated in clients""" |
19 |
|
20 from warnings import warn |
|
21 |
19 |
22 from six import text_type, string_types, integer_types |
20 from six import text_type, string_types, integer_types |
23 from six.moves import range |
21 from six.moves import range |
24 |
22 |
25 from logilab.common.decorators import cached |
23 from logilab.common.decorators import cached |
361 else: |
359 else: |
362 fetchattrs = etypecls.fetch_attrs |
360 fetchattrs = etypecls.fetch_attrs |
363 etypecls._fetch_restrictions(var, select, fetchattrs, |
361 etypecls._fetch_restrictions(var, select, fetchattrs, |
364 user, None, visited=visited) |
362 user, None, visited=visited) |
365 if ordermethod is not None: |
363 if ordermethod is not None: |
366 try: |
364 cmeth = getattr(cls, 'cw_' + ordermethod) |
367 cmeth = getattr(cls, ordermethod) |
365 cmeth(select, attr, var) |
368 warn('[3.14] %s %s class method should be renamed to cw_%s' |
|
369 % (cls.__regid__, ordermethod, ordermethod), |
|
370 DeprecationWarning) |
|
371 except AttributeError: |
|
372 cmeth = getattr(cls, 'cw_' + ordermethod) |
|
373 if support_args(cmeth, 'select'): |
|
374 cmeth(select, attr, var) |
|
375 else: |
|
376 warn('[3.14] %s should now take (select, attr, var) and ' |
|
377 'modify the syntax tree when desired instead of ' |
|
378 'returning something' % cmeth, DeprecationWarning) |
|
379 orderterm = cmeth(attr, var.name) |
|
380 if orderterm is not None: |
|
381 try: |
|
382 var, order = orderterm.split() |
|
383 except ValueError: |
|
384 if '(' in orderterm: |
|
385 cls.error('ignore %s until %s is upgraded', |
|
386 orderterm, cmeth) |
|
387 orderterm = None |
|
388 elif not ' ' in orderterm.strip(): |
|
389 var = orderterm |
|
390 order = 'ASC' |
|
391 if orderterm is not None: |
|
392 select.add_sort_var(select.get_variable(var), |
|
393 order=='ASC') |
|
394 |
366 |
395 @classmethod |
367 @classmethod |
396 @cached |
368 @cached |
397 def cw_rest_attr_info(cls): |
369 def cw_rest_attr_info(cls): |
398 """this class method return an attribute name to be used in URL for |
370 """this class method return an attribute name to be used in URL for |
638 kwargs['_restpath'] = self.rest_path() |
610 kwargs['_restpath'] = self.rest_path() |
639 else: |
611 else: |
640 kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
612 kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
641 return self._cw.build_url(method, **kwargs) |
613 return self._cw.build_url(method, **kwargs) |
642 |
614 |
643 def rest_path(self, *args, **kwargs): # XXX cw_rest_path |
615 def rest_path(self): # XXX cw_rest_path |
644 """returns a REST-like (relative) path for this entity""" |
616 """returns a REST-like (relative) path for this entity""" |
645 if args or kwargs: |
|
646 warn("[3.24] rest_path doesn't take parameters anymore", DeprecationWarning) |
|
647 mainattr, needcheck = self.cw_rest_attr_info() |
617 mainattr, needcheck = self.cw_rest_attr_info() |
648 etype = str(self.e_schema) |
618 etype = str(self.e_schema) |
649 path = etype.lower() |
619 path = etype.lower() |
650 fallback = False |
620 fallback = False |
651 if mainattr != 'eid': |
621 if mainattr != 'eid': |