cubicweb/entity.py
changeset 12567 26744ad37953
parent 12542 85194bd49119
child 12880 59d4ad7e7df3
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    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 
    19 
    20 from six import text_type, string_types, integer_types
       
    21 from six.moves import range
       
    22 
       
    23 from logilab.common.decorators import cached
    20 from logilab.common.decorators import cached
    24 from logilab.common.registry import yes
    21 from logilab.common.registry import yes
    25 from logilab.mtconverter import TransformData, xml_escape
    22 from logilab.mtconverter import TransformData, xml_escape
    26 
    23 
    27 from rql.utils import rqlvar_maker
    24 from rql.utils import rqlvar_maker
    53 
    50 
    54 def can_use_rest_path(value):
    51 def can_use_rest_path(value):
    55     """return True if value can be used at the end of a Rest URL path"""
    52     """return True if value can be used at the end of a Rest URL path"""
    56     if value is None:
    53     if value is None:
    57         return False
    54         return False
    58     value = text_type(value)
       
    59     # the check for ?, /, & are to prevent problems when running
    55     # the check for ?, /, & are to prevent problems when running
    60     # behind Apache mod_proxy
    56     # behind Apache mod_proxy
    61     if value == u'' or u'?' in value or u'/' in value or u'&' in value:
    57     if value == u'' or u'?' in value or u'/' in value or u'&' in value:
    62         return False
    58         return False
    63     return True
    59     return True
   263                     settype=True, ordermethod='fetch_order'):
   259                     settype=True, ordermethod='fetch_order'):
   264         if select is None:
   260         if select is None:
   265             select = Select()
   261             select = Select()
   266             mainvar = select.get_variable(mainvar)
   262             mainvar = select.get_variable(mainvar)
   267             select.add_selected(mainvar)
   263             select.add_selected(mainvar)
   268         elif isinstance(mainvar, string_types):
   264         elif isinstance(mainvar, str):
   269             assert mainvar in select.defined_vars
   265             assert mainvar in select.defined_vars
   270             mainvar = select.get_variable(mainvar)
   266             mainvar = select.get_variable(mainvar)
   271         # eases string -> syntax tree test transition: please remove once stable
   267         # eases string -> syntax tree test transition: please remove once stable
   272         select._varmaker = rqlvar_maker(defined=select.defined_vars,
   268         select._varmaker = rqlvar_maker(defined=select.defined_vars,
   273                                         aliases=select.aliases, index=26)
   269                                         aliases=select.aliases, index=26)
   504 
   500 
   505     def __lt__(self, other):
   501     def __lt__(self, other):
   506         return NotImplemented
   502         return NotImplemented
   507 
   503 
   508     def __eq__(self, other):
   504     def __eq__(self, other):
   509         if isinstance(self.eid, integer_types):
   505         if isinstance(self.eid, int):
   510             return self.eid == other.eid
   506             return self.eid == other.eid
   511         return self is other
   507         return self is other
   512 
   508 
   513     def __hash__(self):
   509     def __hash__(self):
   514         if isinstance(self.eid, integer_types):
   510         if isinstance(self.eid, int):
   515             return self.eid
   511             return self.eid
   516         return super(Entity, self).__hash__()
   512         return super(Entity, self).__hash__()
   517 
   513 
   518     def _cw_update_attr_cache(self, attrcache):
   514     def _cw_update_attr_cache(self, attrcache):
   519         trdata = self._cw.transaction_data
   515         trdata = self._cw.transaction_data
   637         if mainattr == 'eid':
   633         if mainattr == 'eid':
   638             value = self.eid
   634             value = self.eid
   639         if path is None:
   635         if path is None:
   640             # fallback url: <base-url>/<eid> url is used as cw entities uri,
   636             # fallback url: <base-url>/<eid> url is used as cw entities uri,
   641             # prefer it to <base-url>/<etype>/eid/<eid>
   637             # prefer it to <base-url>/<etype>/eid/<eid>
   642             return text_type(value)
   638             return str(value)
   643         return u'%s/%s' % (path, self._cw.url_quote(value))
   639         return u'%s/%s' % (path, self._cw.url_quote(value))
   644 
   640 
   645     def cw_attr_metadata(self, attr, metadata):
   641     def cw_attr_metadata(self, attr, metadata):
   646         """return a metadata for an attribute (None if unspecified)"""
   642         """return a metadata for an attribute (None if unspecified)"""
   647         value = getattr(self, '%s_%s' % (attr, metadata), None)
   643         value = getattr(self, '%s_%s' % (attr, metadata), None)
   655         html tags
   651         html tags
   656         """
   652         """
   657         attr = str(attr)
   653         attr = str(attr)
   658         if value is _marker:
   654         if value is _marker:
   659             value = getattr(self, attr)
   655             value = getattr(self, attr)
   660         if isinstance(value, string_types):
   656         if isinstance(value, str):
   661             value = value.strip()
   657             value = value.strip()
   662         if value is None or value == '': # don't use "not", 0 is an acceptable value
   658         if value is None or value == '': # don't use "not", 0 is an acceptable value
   663             return u''
   659             return u''
   664         if attrtype is None:
   660         if attrtype is None:
   665             attrtype = self.e_schema.destination(attr)
   661             attrtype = self.e_schema.destination(attr)