provide a new RichString attribute type tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 16 Feb 2009 12:33:14 +0100
branchtls-sprint
changeset 624 258e5692ae06
parent 622 2d30c5e1a7d2
child 627 36ade1128af7
provide a new RichString attribute type The follwing declaration: class Card(EntityType): content = RichString(fulltextindexed=True, default_format='text/rest') is equivalent to : class Card(EntityType): content_format = String(meta=True, internationalizable=True, default='text/rest', constraints=[format_constraint]) content = String(fulltextindexed=True)
schema.py
schemas/Card.py
test/unittest_schema.py
--- a/schema.py	Fri Feb 13 16:34:05 2009 +0100
+++ b/schema.py	Mon Feb 16 12:33:14 2009 +0100
@@ -10,7 +10,7 @@
 import re
 from logging import getLogger
 
-from logilab.common.decorators import cached, clear_cache
+from logilab.common.decorators import cached, clear_cache, monkeypatch
 from logilab.common.compat import any
 
 from yams import BadSchemaDefinition, buildobjs as ybo
@@ -68,6 +68,42 @@
     return (etype,)
 ybo.RelationDefinition._actual_types = _actual_types
 
+
+## cubicweb provides a RichString class for convenience
+class RichString(ybo.String):
+    """Convenience RichString attribute type
+    The follwing declaration::
+      
+      class Card(EntityType):
+          content = RichString(fulltextindexed=True, default_format='text/rest')
+          
+    is equivalent to::
+      
+      class Card(EntityType):
+          content_format = String(meta=True, internationalizable=True,
+                                 default='text/rest', constraints=[format_constraint])
+          content  = String(fulltextindexed=True)
+    """
+    def __init__(self, default_format='text/plain', format_constraints=None, **kwargs):
+        self.default_format = default_format
+        self.format_constraints = format_constraints or [format_constraint]
+        super(RichString, self).__init__(**kwargs)
+
+PyFileReader.context['RichString'] = RichString
+
+## need to monkeypatch yams' _add_relation function to handle RichString
+yams_add_relation = ybo._add_relation
+@monkeypatch(ybo)
+def _add_relation(relations, rdef, name=None, insertidx=None):
+    if isinstance(rdef, RichString):
+        default_format = rdef.default_format
+        format_attrdef = ybo.String(meta=True, internationalizable=True,
+                                    default=rdef.default_format, maxsize=50,
+                                    constraints=rdef.format_constraints)
+        yams_add_relation(relations, format_attrdef, name+'_format', insertidx)
+    yams_add_relation(relations, rdef, name, insertidx)
+
+    
 def display_name(req, key, form=''):
     """return a internationalized string for the key (schema entity or relation
     name) in a given form
--- a/schemas/Card.py	Fri Feb 13 16:34:05 2009 +0100
+++ b/schemas/Card.py	Mon Feb 16 12:33:14 2009 +0100
@@ -1,4 +1,4 @@
-from cubicweb.schema import format_constraint
+# from cubicweb.schema import format_constraint
 
 class Card(EntityType):
     """a card is a textual content used as documentation, reference, procedure reminder"""
@@ -12,7 +12,5 @@
     title    = String(required=True, fulltextindexed=True, maxsize=256)
     synopsis = String(fulltextindexed=True, maxsize=512,
                       description=_("an abstract for this card"))
-    content_format = String(meta=True, internationalizable=True, maxsize=50,
-                            default='text/rest', constraints=[format_constraint])
-    content  = String(fulltextindexed=True)
+    content = RichString(fulltextindexed=True, default_format='text/rest')
     wikiid = String(maxsize=64, indexed=True)
--- a/test/unittest_schema.py	Fri Feb 13 16:34:05 2009 +0100
+++ b/test/unittest_schema.py	Mon Feb 16 12:33:14 2009 +0100
@@ -134,7 +134,6 @@
         self.assertListEquals([basename(f) for f in schema_files], ['Bookmark.py'])
 
     def test_knownValues_load_schema(self):
-        """read an url and return a Schema instance"""
         schema = loader.load(config)
         self.assert_(isinstance(schema, CubicWebSchema))
         self.assertEquals(schema.name, 'data')