[entity] add an official ``cw_etype`` property for entity
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 23 Apr 2013 15:16:36 +0200
changeset 8899 c7a95ebcc093
parent 8898 c570d15dce7b
child 8900 010a59e12d89
[entity] add an official ``cw_etype`` property for entity This property holds the CWEtype id of the entity. This information was previously available through __regid__ but this is an ugly public API. So we have a new clean public API. The ``dc_type()`` method could not be used as it is translated. (closes #2793792)
entities/__init__.py
entities/test/unittest_base.py
--- a/entities/__init__.py	Tue Apr 23 12:38:27 2013 +0200
+++ b/entities/__init__.py	Tue Apr 23 15:16:36 2013 +0200
@@ -22,7 +22,7 @@
 from warnings import warn
 
 from logilab.common.deprecation import deprecated
-from logilab.common.decorators import cached
+from logilab.common.decorators import cached, classproperty
 
 from cubicweb import Unauthorized
 from cubicweb.entity import Entity
@@ -60,6 +60,11 @@
 
     # meta data api ###########################################################
 
+    @classproperty
+    def cw_etype(self):
+        """entity Etype as a string"""
+        return self.__regid__
+
     def dc_title(self):
         """return a suitable *unicode* title for this entity"""
         for rschema, attrschema in self.e_schema.attribute_definitions():
--- a/entities/test/unittest_base.py	Tue Apr 23 12:38:27 2013 +0200
+++ b/entities/test/unittest_base.py	Tue Apr 23 15:16:36 2013 +0200
@@ -48,8 +48,13 @@
         self.assertEqual(entity.dc_creator(), u'member')
 
     def test_type(self):
+        #dc_type may be translated
         self.assertEqual(self.member.dc_type(), 'CWUser')
 
+    def test_cw_etype(self):
+        #cw_etype is never translated
+        self.assertEqual(self.member.cw_etype, 'CWUser')
+
     def test_entity_meta_attributes(self):
         # XXX move to yams
         self.assertEqual(self.schema['CWUser'].meta_attributes(), {})
@@ -172,7 +177,7 @@
                 self.assertEqual(eclass.__bases__[0].__bases__, (Foo,))
         # check Division eclass is still selected for plain Division entities
         eclass = self.select_eclass('Division')
-        self.assertEqual(eclass.__regid__, 'Division')
+        self.assertEqual(eclass.cw_etype, 'Division')
 
 if __name__ == '__main__':
     unittest_main()