8 |
8 |
9 import warnings |
9 import warnings |
10 import re |
10 import re |
11 from logging import getLogger |
11 from logging import getLogger |
12 |
12 |
13 from logilab.common.decorators import cached, clear_cache |
13 from logilab.common.decorators import cached, clear_cache, monkeypatch |
14 from logilab.common.compat import any |
14 from logilab.common.compat import any |
15 |
15 |
16 from yams import BadSchemaDefinition, buildobjs as ybo |
16 from yams import BadSchemaDefinition, buildobjs as ybo |
17 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema |
17 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema |
18 from yams.constraints import BaseConstraint, StaticVocabularyConstraint |
18 from yams.constraints import BaseConstraint, StaticVocabularyConstraint |
66 etypes += tuple(system_etypes(schema)) |
66 etypes += tuple(system_etypes(schema)) |
67 return etypes |
67 return etypes |
68 return (etype,) |
68 return (etype,) |
69 ybo.RelationDefinition._actual_types = _actual_types |
69 ybo.RelationDefinition._actual_types = _actual_types |
70 |
70 |
|
71 |
|
72 ## cubicweb provides a RichString class for convenience |
|
73 class RichString(ybo.String): |
|
74 """Convenience RichString attribute type |
|
75 The follwing declaration:: |
|
76 |
|
77 class Card(EntityType): |
|
78 content = RichString(fulltextindexed=True, default_format='text/rest') |
|
79 |
|
80 is equivalent to:: |
|
81 |
|
82 class Card(EntityType): |
|
83 content_format = String(meta=True, internationalizable=True, |
|
84 default='text/rest', constraints=[format_constraint]) |
|
85 content = String(fulltextindexed=True) |
|
86 """ |
|
87 def __init__(self, default_format='text/plain', format_constraints=None, **kwargs): |
|
88 self.default_format = default_format |
|
89 self.format_constraints = format_constraints or [format_constraint] |
|
90 super(RichString, self).__init__(**kwargs) |
|
91 |
|
92 PyFileReader.context['RichString'] = RichString |
|
93 |
|
94 ## need to monkeypatch yams' _add_relation function to handle RichString |
|
95 yams_add_relation = ybo._add_relation |
|
96 @monkeypatch(ybo) |
|
97 def _add_relation(relations, rdef, name=None, insertidx=None): |
|
98 if isinstance(rdef, RichString): |
|
99 default_format = rdef.default_format |
|
100 format_attrdef = ybo.String(meta=True, internationalizable=True, |
|
101 default=rdef.default_format, maxsize=50, |
|
102 constraints=rdef.format_constraints) |
|
103 yams_add_relation(relations, format_attrdef, name+'_format', insertidx) |
|
104 yams_add_relation(relations, rdef, name, insertidx) |
|
105 |
|
106 |
71 def display_name(req, key, form=''): |
107 def display_name(req, key, form=''): |
72 """return a internationalized string for the key (schema entity or relation |
108 """return a internationalized string for the key (schema entity or relation |
73 name) in a given form |
109 name) in a given form |
74 """ |
110 """ |
75 assert form in ('', 'plural', 'subject', 'object') |
111 assert form in ('', 'plural', 'subject', 'object') |