author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sat, 01 Aug 2009 16:13:40 +0200 | |
changeset 2625 | d6012db7b93e |
parent 2613 | 5e19c2bb370e |
child 2647 | b0a2e779845c |
permissions | -rw-r--r-- |
0 | 1 |
"""Base class for entity objects manipulated in clients |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1904
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1904
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1154 | 10 |
from warnings import warn |
11 |
||
0 | 12 |
from logilab.common import interface |
13 |
from logilab.common.compat import all |
|
14 |
from logilab.common.decorators import cached |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2421
diff
changeset
|
15 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
16 |
from logilab.mtconverter import TransformData, TransformError, xml_escape |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
17 |
|
0 | 18 |
from rql.utils import rqlvar_maker |
19 |
||
20 |
from cubicweb import Unauthorized |
|
21 |
from cubicweb.rset import ResultSet |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
631
diff
changeset
|
22 |
from cubicweb.selectors import yes |
707 | 23 |
from cubicweb.appobject import AppRsetObject |
709 | 24 |
from cubicweb.schema import RQLVocabularyConstraint, RQLConstraint, bw_normalize_etype |
25 |
||
2421
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
26 |
from cubicweb.common.uilib import printable_value, soup2xhtml |
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
27 |
from cubicweb.common.mixins import MI_REL_TRIGGERS |
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
28 |
from cubicweb.common.mttransforms import ENGINE |
0 | 29 |
|
30 |
_marker = object() |
|
31 |
||
32 |
def greater_card(rschema, subjtypes, objtypes, index): |
|
33 |
for subjtype in subjtypes: |
|
34 |
for objtype in objtypes: |
|
35 |
card = rschema.rproperty(subjtype, objtype, 'cardinality')[index] |
|
36 |
if card in '+*': |
|
37 |
return card |
|
38 |
return '1' |
|
39 |
||
40 |
||
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
41 |
_MODE_TAGS = set(('link', 'create')) |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
42 |
_CATEGORY_TAGS = set(('primary', 'secondary', 'generic', 'generated')) # , 'metadata')) |
0 | 43 |
|
1154 | 44 |
try: |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
45 |
from cubicweb.web import formwidgets, uicfg |
0 | 46 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
47 |
def _dispatch_rtags(tags, rtype, role, stype, otype): |
1154 | 48 |
for tag in tags: |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
49 |
if tag in _MODE_TAGS: |
1739 | 50 |
uicfg.actionbox_appearsin_addmenu.tag_relation( |
51 |
(stype, rtype, otype, role), tag == 'create') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
52 |
elif tag in _CATEGORY_TAGS: |
1739 | 53 |
uicfg.autoform_section.tag_relation((stype, rtype, otype, role), |
54 |
tag) |
|
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
55 |
elif tag == 'inlineview': |
1739 | 56 |
uicfg.autoform_is_inlined.tag_relation((stype, rtype, otype, role), True) |
1154 | 57 |
else: |
58 |
raise ValueError(tag) |
|
1471
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
59 |
|
1154 | 60 |
except ImportError: |
1734
e8673144bc74
access to rwidgets through uicfg, removing buggy import breaking bw compat on the way
sylvain.thenault@logilab.fr
parents:
1721
diff
changeset
|
61 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
62 |
_dispatch_rtags = None |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
63 |
|
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
64 |
def _get_etype(bases, classdict): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
65 |
try: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
66 |
return classdict['id'] |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
67 |
except KeyError: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
68 |
for base in bases: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
69 |
etype = getattr(base, 'id', None) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
70 |
if etype and etype != 'Any': |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
71 |
return etype |
1474 | 72 |
|
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
73 |
def _get_defs(attr, name, bases, classdict): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
74 |
try: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
75 |
yield name, classdict.pop(attr) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
76 |
except KeyError: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
77 |
for base in bases: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
78 |
try: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
79 |
value = getattr(base, attr) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
80 |
delattr(base, attr) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
81 |
yield base.__name__, value |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
82 |
except AttributeError: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
83 |
continue |
1474 | 84 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
85 |
class _metaentity(type): |
0 | 86 |
"""this metaclass sets the relation tags on the entity class |
87 |
and deals with the `widgets` attribute |
|
88 |
""" |
|
89 |
def __new__(mcs, name, bases, classdict): |
|
90 |
# collect baseclass' rtags |
|
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
91 |
etype = _get_etype(bases, classdict) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
92 |
if etype and _dispatch_rtags is not None: |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
93 |
for name, rtags in _get_defs('__rtags__', name, bases, classdict): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
94 |
warn('%s: __rtags__ is deprecated' % name, DeprecationWarning) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
95 |
for relation, tags in rtags.iteritems(): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
96 |
# tags must become an iterable |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
97 |
if isinstance(tags, basestring): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
98 |
tags = (tags,) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
99 |
# relation must become a 3-uple (rtype, targettype, role) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
100 |
if isinstance(relation, basestring): |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
101 |
_dispatch_rtags(tags, relation, 'subject', etype, '*') |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
102 |
_dispatch_rtags(tags, relation, 'object', '*', etype) |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
103 |
elif len(relation) == 1: # useful ? |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
104 |
_dispatch_rtags(tags, relation[0], 'subject', etype, '*') |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
105 |
_dispatch_rtags(tags, relation[0], 'object', '*', etype) |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
106 |
elif len(relation) == 2: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
107 |
rtype, ttype = relation |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
108 |
ttype = bw_normalize_etype(ttype) # XXX bw compat |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
109 |
_dispatch_rtags(tags, rtype, 'subject', etype, ttype) |
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
110 |
_dispatch_rtags(tags, rtype, 'object', ttype, etype) |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
111 |
elif len(relation) == 3: |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
112 |
rtype, ttype, role = relation |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
113 |
ttype = bw_normalize_etype(ttype) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
114 |
if role == 'subject': |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
115 |
_dispatch_rtags(tags, rtype, 'subject', etype, ttype) |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
116 |
else: |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
117 |
_dispatch_rtags(tags, rtype, 'object', ttype, etype) |
1154 | 118 |
else: |
1177
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
119 |
raise ValueError('bad rtag definition (%r)' % (relation,)) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
120 |
for name, widgets in _get_defs('widgets', name, bases, classdict): |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
121 |
warn('%s: widgets is deprecated' % name, DeprecationWarning) |
7074698c6522
fix support for old __rtags__ and widgets
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
122 |
for rtype, wdgname in widgets.iteritems(): |
1804 | 123 |
if wdgname in ('URLWidget', 'EmbededURLWidget', 'RawDynamicComboBoxWidget'): |
1269 | 124 |
warn('%s widget is deprecated' % wdgname, DeprecationWarning) |
125 |
continue |
|
1313
9cff1eee0208
put class, not class name into rwidgets. New rfields rtags to specify a field for a relation
sylvain.thenault@logilab.fr
parents:
1269
diff
changeset
|
126 |
if wdgname == 'StringWidget': |
9cff1eee0208
put class, not class name into rwidgets. New rfields rtags to specify a field for a relation
sylvain.thenault@logilab.fr
parents:
1269
diff
changeset
|
127 |
wdgname = 'TextInput' |
9cff1eee0208
put class, not class name into rwidgets. New rfields rtags to specify a field for a relation
sylvain.thenault@logilab.fr
parents:
1269
diff
changeset
|
128 |
widget = getattr(formwidgets, wdgname) |
1582 | 129 |
assert hasattr(widget, 'render') |
1754
c9c7618a90de
autoform_widget superseeded by autoform_field_kwargs (api change addiction :-/)
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
130 |
uicfg.autoform_field_kwargs.tag_subject_of( |
c9c7618a90de
autoform_widget superseeded by autoform_field_kwargs (api change addiction :-/)
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
131 |
(etype, rtype, '*'), {'widget': widget}) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
132 |
return super(_metaentity, mcs).__new__(mcs, name, bases, classdict) |
0 | 133 |
|
134 |
||
135 |
class Entity(AppRsetObject, dict): |
|
136 |
"""an entity instance has e_schema automagically set on |
|
137 |
the class and instances has access to their issuing cursor. |
|
1474 | 138 |
|
0 | 139 |
A property is set for each attribute and relation on each entity's type |
140 |
class. Becare that among attributes, 'eid' is *NEITHER* stored in the |
|
141 |
dict containment (which acts as a cache for other attributes dynamically |
|
142 |
fetched) |
|
143 |
||
144 |
:type e_schema: `cubicweb.schema.EntitySchema` |
|
145 |
:ivar e_schema: the entity's schema |
|
146 |
||
147 |
:type rest_var: str |
|
148 |
:cvar rest_var: indicates which attribute should be used to build REST urls |
|
149 |
If None is specified, the first non-meta attribute will |
|
150 |
be used |
|
1474 | 151 |
|
0 | 152 |
:type skip_copy_for: list |
153 |
:cvar skip_copy_for: a list of relations that should be skipped when copying |
|
154 |
this kind of entity. Note that some relations such |
|
155 |
as composite relations or relations that have '?1' as object |
|
156 |
cardinality |
|
157 |
""" |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1474
diff
changeset
|
158 |
__metaclass__ = _metaentity |
0 | 159 |
__registry__ = 'etypes' |
717 | 160 |
__select__ = yes() |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
161 |
|
1474 | 162 |
# class attributes that must be set in class definition |
0 | 163 |
id = None |
164 |
rest_attr = None |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
165 |
fetch_attrs = None |
0 | 166 |
skip_copy_for = () |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
167 |
# class attributes set automatically at registration time |
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
168 |
e_schema = None |
1474 | 169 |
|
0 | 170 |
@classmethod |
171 |
def registered(cls, registry): |
|
172 |
"""build class using descriptor at registration time""" |
|
173 |
assert cls.id is not None |
|
174 |
super(Entity, cls).registered(registry) |
|
175 |
if cls.id != 'Any': |
|
176 |
cls.__initialize__() |
|
177 |
return cls |
|
1474 | 178 |
|
0 | 179 |
MODE_TAGS = set(('link', 'create')) |
180 |
CATEGORY_TAGS = set(('primary', 'secondary', 'generic', 'generated')) # , 'metadata')) |
|
181 |
@classmethod |
|
182 |
def __initialize__(cls): |
|
183 |
"""initialize a specific entity class by adding descriptors to access |
|
184 |
entity type's attributes and relations |
|
185 |
""" |
|
186 |
etype = cls.id |
|
187 |
assert etype != 'Any', etype |
|
188 |
cls.e_schema = eschema = cls.schema.eschema(etype) |
|
189 |
for rschema, _ in eschema.attribute_definitions(): |
|
190 |
if rschema.type == 'eid': |
|
191 |
continue |
|
192 |
setattr(cls, rschema.type, Attribute(rschema.type)) |
|
193 |
mixins = [] |
|
194 |
for rschema, _, x in eschema.relation_definitions(): |
|
195 |
if (rschema, x) in MI_REL_TRIGGERS: |
|
196 |
mixin = MI_REL_TRIGGERS[(rschema, x)] |
|
197 |
if not (issubclass(cls, mixin) or mixin in mixins): # already mixed ? |
|
198 |
mixins.append(mixin) |
|
199 |
for iface in getattr(mixin, '__implements__', ()): |
|
200 |
if not interface.implements(cls, iface): |
|
201 |
interface.extend(cls, iface) |
|
202 |
if x == 'subject': |
|
203 |
setattr(cls, rschema.type, SubjectRelation(rschema)) |
|
204 |
else: |
|
205 |
attr = 'reverse_%s' % rschema.type |
|
206 |
setattr(cls, attr, ObjectRelation(rschema)) |
|
207 |
if mixins: |
|
208 |
cls.__bases__ = tuple(mixins + [p for p in cls.__bases__ if not p is object]) |
|
209 |
cls.debug('plugged %s mixins on %s', mixins, etype) |
|
1474 | 210 |
|
0 | 211 |
@classmethod |
212 |
def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X', |
|
213 |
settype=True, ordermethod='fetch_order'): |
|
214 |
"""return a rql to fetch all entities of the class type""" |
|
215 |
restrictions = restriction or [] |
|
216 |
if settype: |
|
217 |
restrictions.append('%s is %s' % (mainvar, cls.id)) |
|
218 |
if fetchattrs is None: |
|
219 |
fetchattrs = cls.fetch_attrs |
|
220 |
selection = [mainvar] |
|
221 |
orderby = [] |
|
222 |
# start from 26 to avoid possible conflicts with X |
|
223 |
varmaker = rqlvar_maker(index=26) |
|
224 |
cls._fetch_restrictions(mainvar, varmaker, fetchattrs, selection, |
|
225 |
orderby, restrictions, user, ordermethod) |
|
226 |
rql = 'Any %s' % ','.join(selection) |
|
227 |
if orderby: |
|
228 |
rql += ' ORDERBY %s' % ','.join(orderby) |
|
229 |
rql += ' WHERE %s' % ', '.join(restrictions) |
|
230 |
return rql |
|
1474 | 231 |
|
0 | 232 |
@classmethod |
233 |
def _fetch_restrictions(cls, mainvar, varmaker, fetchattrs, |
|
234 |
selection, orderby, restrictions, user, |
|
235 |
ordermethod='fetch_order', visited=None): |
|
236 |
eschema = cls.e_schema |
|
237 |
if visited is None: |
|
238 |
visited = set((eschema.type,)) |
|
239 |
elif eschema.type in visited: |
|
240 |
# avoid infinite recursion |
|
241 |
return |
|
242 |
else: |
|
243 |
visited.add(eschema.type) |
|
244 |
_fetchattrs = [] |
|
245 |
for attr in fetchattrs: |
|
246 |
try: |
|
247 |
rschema = eschema.subject_relation(attr) |
|
248 |
except KeyError: |
|
249 |
cls.warning('skipping fetch_attr %s defined in %s (not found in schema)', |
|
250 |
attr, cls.id) |
|
251 |
continue |
|
252 |
if not user.matching_groups(rschema.get_groups('read')): |
|
253 |
continue |
|
254 |
var = varmaker.next() |
|
255 |
selection.append(var) |
|
256 |
restriction = '%s %s %s' % (mainvar, attr, var) |
|
257 |
restrictions.append(restriction) |
|
258 |
if not rschema.is_final(): |
|
259 |
# XXX this does not handle several destination types |
|
260 |
desttype = rschema.objects(eschema.type)[0] |
|
261 |
card = rschema.rproperty(eschema, desttype, 'cardinality')[0] |
|
262 |
if card not in '?1': |
|
263 |
selection.pop() |
|
264 |
restrictions.pop() |
|
265 |
continue |
|
266 |
if card == '?': |
|
267 |
restrictions[-1] += '?' # left outer join if not mandatory |
|
268 |
destcls = cls.vreg.etype_class(desttype) |
|
269 |
destcls._fetch_restrictions(var, varmaker, destcls.fetch_attrs, |
|
270 |
selection, orderby, restrictions, |
|
271 |
user, ordermethod, visited=visited) |
|
272 |
orderterm = getattr(cls, ordermethod)(attr, var) |
|
273 |
if orderterm: |
|
274 |
orderby.append(orderterm) |
|
275 |
return selection, orderby, restrictions |
|
276 |
||
1471
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
277 |
@classmethod |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
278 |
@cached |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
279 |
def parent_classes(cls): |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
280 |
parents = [cls.vreg.etype_class(e.type) for e in cls.e_schema.ancestors()] |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
281 |
parents.append(cls.vreg.etype_class('Any')) |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
282 |
return parents |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
283 |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
284 |
@classmethod |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
285 |
@cached |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
286 |
def _rest_attr_info(cls): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
287 |
mainattr, needcheck = 'eid', True |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
288 |
if cls.rest_attr: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
289 |
mainattr = cls.rest_attr |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
290 |
needcheck = not cls.e_schema.has_unique_values(mainattr) |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
291 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
292 |
for rschema in cls.e_schema.subject_relations(): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
293 |
if rschema.is_final() and rschema != 'eid' and cls.e_schema.has_unique_values(rschema): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
294 |
mainattr = str(rschema) |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
295 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
296 |
break |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
297 |
if mainattr == 'eid': |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
298 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
299 |
return mainattr, needcheck |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
300 |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1363
diff
changeset
|
301 |
def __init__(self, req, rset=None, row=None, col=0): |
1363
d8f2c3953eb5
AppRsetObject now handle row and col
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
302 |
AppRsetObject.__init__(self, req, rset, row, col) |
0 | 303 |
dict.__init__(self) |
304 |
self._related_cache = {} |
|
305 |
if rset is not None: |
|
306 |
self.eid = rset[row][col] |
|
307 |
else: |
|
308 |
self.eid = None |
|
309 |
self._is_saved = True |
|
1474 | 310 |
|
0 | 311 |
def __repr__(self): |
312 |
return '<Entity %s %s %s at %s>' % ( |
|
313 |
self.e_schema, self.eid, self.keys(), id(self)) |
|
314 |
||
315 |
def __nonzero__(self): |
|
316 |
return True |
|
317 |
||
318 |
def __hash__(self): |
|
319 |
return id(self) |
|
320 |
||
321 |
def pre_add_hook(self): |
|
322 |
"""hook called by the repository before doing anything to add the entity |
|
323 |
(before_add entity hooks have not been called yet). This give the |
|
324 |
occasion to do weird stuff such as autocast (File -> Image for instance). |
|
1474 | 325 |
|
0 | 326 |
This method must return the actual entity to be added. |
327 |
""" |
|
328 |
return self |
|
1474 | 329 |
|
0 | 330 |
def set_eid(self, eid): |
331 |
self.eid = self['eid'] = eid |
|
332 |
||
333 |
def has_eid(self): |
|
334 |
"""return True if the entity has an attributed eid (False |
|
335 |
meaning that the entity has to be created |
|
336 |
""" |
|
337 |
try: |
|
338 |
int(self.eid) |
|
339 |
return True |
|
340 |
except (ValueError, TypeError): |
|
341 |
return False |
|
342 |
||
343 |
def is_saved(self): |
|
344 |
"""during entity creation, there is some time during which the entity |
|
345 |
has an eid attributed though it's not saved (eg during before_add_entity |
|
346 |
hooks). You can use this method to ensure the entity has an eid *and* is |
|
347 |
saved in its source. |
|
348 |
""" |
|
349 |
return self.has_eid() and self._is_saved |
|
1474 | 350 |
|
0 | 351 |
@cached |
352 |
def metainformation(self): |
|
353 |
res = dict(zip(('type', 'source', 'extid'), self.req.describe(self.eid))) |
|
354 |
res['source'] = self.req.source_defs()[res['source']] |
|
355 |
return res |
|
356 |
||
475
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
357 |
def clear_local_perm_cache(self, action): |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
358 |
for rqlexpr in self.e_schema.get_rqlexprs(action): |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
359 |
self.req.local_perm_cache.pop((rqlexpr.eid, (('x', self.eid),)), None) |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
360 |
|
0 | 361 |
def check_perm(self, action): |
362 |
self.e_schema.check_perm(self.req, action, self.eid) |
|
363 |
||
364 |
def has_perm(self, action): |
|
365 |
return self.e_schema.has_perm(self.req, action, self.eid) |
|
1474 | 366 |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
367 |
def view(self, vid, **kwargs): |
0 | 368 |
"""shortcut to apply a view on this entity""" |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
369 |
return self.vreg.render(vid, self.req, rset=self.rset, |
0 | 370 |
row=self.row, col=self.col, **kwargs) |
371 |
||
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
372 |
def absolute_url(self, *args, **kwargs): |
0 | 373 |
"""return an absolute url to view this entity""" |
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
374 |
# use *args since we don't want first argument to be "anonymous" to |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
375 |
# avoid potential clash with kwargs |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
376 |
if args: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
377 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
378 |
method = args[0] |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
379 |
else: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
380 |
method = None |
0 | 381 |
# in linksearch mode, we don't want external urls else selecting |
382 |
# the object for use in the relation is tricky |
|
383 |
# XXX search_state is web specific |
|
384 |
if getattr(self.req, 'search_state', ('normal',))[0] == 'normal': |
|
385 |
kwargs['base_url'] = self.metainformation()['source'].get('base-url') |
|
1904
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
386 |
if method in (None, 'view'): |
1842
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
387 |
try: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
388 |
kwargs['_restpath'] = self.rest_path(kwargs.get('base_url')) |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
389 |
except TypeError: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
390 |
warn('%s: rest_path() now take use_ext_eid argument, ' |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
391 |
'please update' % self.id, DeprecationWarning) |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
392 |
kwargs['_restpath'] = self.rest_path() |
0 | 393 |
else: |
394 |
kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
|
395 |
return self.build_url(method, **kwargs) |
|
396 |
||
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
397 |
def rest_path(self, use_ext_eid=False): |
0 | 398 |
"""returns a REST-like (relative) path for this entity""" |
399 |
mainattr, needcheck = self._rest_attr_info() |
|
400 |
etype = str(self.e_schema) |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
401 |
path = etype.lower() |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
402 |
if mainattr != 'eid': |
0 | 403 |
value = getattr(self, mainattr) |
2125
19861294506f
https://www.logilab.net/cwo/ticket/343724
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
404 |
if value is None or unicode(value) == u'': |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
405 |
mainattr = 'eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
406 |
path += '/eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
407 |
elif needcheck: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
408 |
# make sure url is not ambiguous |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
409 |
rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % ( |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
410 |
etype, mainattr) |
1904
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
411 |
nbresults = self.req.execute(rql, {'value' : value})[0][0] |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
412 |
if nbresults != 1: # ambiguity? |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
413 |
mainattr = 'eid' |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
414 |
path += '/eid' |
0 | 415 |
if mainattr == 'eid': |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
416 |
if use_ext_eid: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
417 |
value = self.metainformation()['extid'] |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
418 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
419 |
value = self.eid |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
420 |
return '%s/%s' % (path, self.req.url_quote(value)) |
0 | 421 |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
422 |
def attr_metadata(self, attr, metadata): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
423 |
"""return a metadata for an attribute (None if unspecified)""" |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
424 |
value = getattr(self, '%s_%s' % (attr, metadata), None) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
425 |
if value is None and metadata == 'encoding': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
426 |
value = self.vreg.property_value('ui.encoding') |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
427 |
return value |
0 | 428 |
|
429 |
def printable_value(self, attr, value=_marker, attrtype=None, |
|
430 |
format='text/html', displaytime=True): |
|
431 |
"""return a displayable value (i.e. unicode string) which may contains |
|
432 |
html tags |
|
433 |
""" |
|
434 |
attr = str(attr) |
|
435 |
if value is _marker: |
|
436 |
value = getattr(self, attr) |
|
437 |
if isinstance(value, basestring): |
|
438 |
value = value.strip() |
|
439 |
if value is None or value == '': # don't use "not", 0 is an acceptable value |
|
440 |
return u'' |
|
441 |
if attrtype is None: |
|
442 |
attrtype = self.e_schema.destination(attr) |
|
443 |
props = self.e_schema.rproperties(attr) |
|
444 |
if attrtype == 'String': |
|
445 |
# internalinalized *and* formatted string such as schema |
|
446 |
# description... |
|
447 |
if props.get('internationalizable'): |
|
448 |
value = self.req._(value) |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
449 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 450 |
if attrformat: |
451 |
return self.mtc_transform(value, attrformat, format, |
|
452 |
self.req.encoding) |
|
453 |
elif attrtype == 'Bytes': |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
454 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 455 |
if attrformat: |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
456 |
encoding = self.attr_metadata(attr, 'encoding') |
0 | 457 |
return self.mtc_transform(value.getvalue(), attrformat, format, |
458 |
encoding) |
|
459 |
return u'' |
|
460 |
value = printable_value(self.req, attrtype, value, props, displaytime) |
|
461 |
if format == 'text/html': |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
462 |
value = xml_escape(value) |
0 | 463 |
return value |
464 |
||
465 |
def mtc_transform(self, data, format, target_format, encoding, |
|
466 |
_engine=ENGINE): |
|
467 |
trdata = TransformData(data, format, encoding, appobject=self) |
|
468 |
data = _engine.convert(trdata, target_format).decode() |
|
469 |
if format == 'text/html': |
|
1474 | 470 |
data = soup2xhtml(data, self.req.encoding) |
0 | 471 |
return data |
1474 | 472 |
|
0 | 473 |
# entity cloning ########################################################## |
474 |
||
475 |
def copy_relations(self, ceid): |
|
476 |
"""copy relations of the object with the given eid on this object |
|
477 |
||
478 |
By default meta and composite relations are skipped. |
|
479 |
Overrides this if you want another behaviour |
|
480 |
""" |
|
481 |
assert self.has_eid() |
|
482 |
execute = self.req.execute |
|
483 |
for rschema in self.e_schema.subject_relations(): |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2059
diff
changeset
|
484 |
if rschema.is_final() or rschema.meta: |
0 | 485 |
continue |
486 |
# skip already defined relations |
|
487 |
if getattr(self, rschema.type): |
|
488 |
continue |
|
489 |
if rschema.type in self.skip_copy_for: |
|
490 |
continue |
|
491 |
if rschema.type == 'in_state': |
|
492 |
# if the workflow is defining an initial state (XXX AND we are |
|
493 |
# not in the managers group? not done to be more consistent) |
|
494 |
# don't try to copy in_state |
|
495 |
if execute('Any S WHERE S state_of ET, ET initial_state S,' |
|
496 |
'ET name %(etype)s', {'etype': str(self.e_schema)}): |
|
497 |
continue |
|
498 |
# skip composite relation |
|
499 |
if self.e_schema.subjrproperty(rschema, 'composite'): |
|
500 |
continue |
|
501 |
# skip relation with card in ?1 else we either change the copied |
|
502 |
# object (inlined relation) or inserting some inconsistency |
|
503 |
if self.e_schema.subjrproperty(rschema, 'cardinality')[1] in '?1': |
|
504 |
continue |
|
505 |
rql = 'SET X %s V WHERE X eid %%(x)s, Y eid %%(y)s, Y %s V' % ( |
|
506 |
rschema.type, rschema.type) |
|
507 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
508 |
self.clear_related_cache(rschema.type, 'subject') |
|
509 |
for rschema in self.e_schema.object_relations(): |
|
510 |
if rschema.meta: |
|
511 |
continue |
|
512 |
# skip already defined relations |
|
513 |
if getattr(self, 'reverse_%s' % rschema.type): |
|
514 |
continue |
|
515 |
# skip composite relation |
|
516 |
if self.e_schema.objrproperty(rschema, 'composite'): |
|
517 |
continue |
|
518 |
# skip relation with card in ?1 else we either change the copied |
|
519 |
# object (inlined relation) or inserting some inconsistency |
|
520 |
if self.e_schema.objrproperty(rschema, 'cardinality')[0] in '?1': |
|
521 |
continue |
|
522 |
rql = 'SET V %s X WHERE X eid %%(x)s, Y eid %%(y)s, V %s Y' % ( |
|
523 |
rschema.type, rschema.type) |
|
524 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
525 |
self.clear_related_cache(rschema.type, 'object') |
|
526 |
||
527 |
# data fetching methods ################################################### |
|
528 |
||
529 |
@cached |
|
530 |
def as_rset(self): |
|
531 |
"""returns a resultset containing `self` information""" |
|
532 |
rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s', |
|
533 |
{'x': self.eid}, [(self.id,)]) |
|
534 |
return self.req.decorate_rset(rset) |
|
1474 | 535 |
|
0 | 536 |
def to_complete_relations(self): |
537 |
"""by default complete final relations to when calling .complete()""" |
|
538 |
for rschema in self.e_schema.subject_relations(): |
|
539 |
if rschema.is_final(): |
|
540 |
continue |
|
541 |
if len(rschema.objects(self.e_schema)) > 1: |
|
542 |
# ambigous relations, the querier doesn't handle |
|
543 |
# outer join correctly in this case |
|
544 |
continue |
|
545 |
if rschema.inlined: |
|
546 |
matching_groups = self.req.user.matching_groups |
|
547 |
if matching_groups(rschema.get_groups('read')) and \ |
|
548 |
all(matching_groups(es.get_groups('read')) |
|
549 |
for es in rschema.objects(self.e_schema)): |
|
550 |
yield rschema, 'subject' |
|
1474 | 551 |
|
0 | 552 |
def to_complete_attributes(self, skip_bytes=True): |
553 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
554 |
# skip binary data by default |
|
555 |
if skip_bytes and attrschema.type == 'Bytes': |
|
556 |
continue |
|
557 |
attr = rschema.type |
|
558 |
if attr == 'eid': |
|
559 |
continue |
|
560 |
# password retreival is blocked at the repository server level |
|
561 |
if not self.req.user.matching_groups(rschema.get_groups('read')) \ |
|
562 |
or attrschema.type == 'Password': |
|
563 |
self[attr] = None |
|
564 |
continue |
|
565 |
yield attr |
|
1474 | 566 |
|
0 | 567 |
def complete(self, attributes=None, skip_bytes=True): |
568 |
"""complete this entity by adding missing attributes (i.e. query the |
|
569 |
repository to fill the entity) |
|
570 |
||
571 |
:type skip_bytes: bool |
|
572 |
:param skip_bytes: |
|
573 |
if true, attribute of type Bytes won't be considered |
|
574 |
""" |
|
575 |
assert self.has_eid() |
|
576 |
varmaker = rqlvar_maker() |
|
577 |
V = varmaker.next() |
|
578 |
rql = ['WHERE %s eid %%(x)s' % V] |
|
579 |
selected = [] |
|
580 |
for attr in (attributes or self.to_complete_attributes(skip_bytes)): |
|
581 |
# if attribute already in entity, nothing to do |
|
582 |
if self.has_key(attr): |
|
583 |
continue |
|
584 |
# case where attribute must be completed, but is not yet in entity |
|
585 |
var = varmaker.next() |
|
586 |
rql.append('%s %s %s' % (V, attr, var)) |
|
587 |
selected.append((attr, var)) |
|
588 |
# +1 since this doen't include the main variable |
|
589 |
lastattr = len(selected) + 1 |
|
590 |
if attributes is None: |
|
591 |
# fetch additional relations (restricted to 0..1 relations) |
|
592 |
for rschema, role in self.to_complete_relations(): |
|
593 |
rtype = rschema.type |
|
594 |
if self.relation_cached(rtype, role): |
|
595 |
continue |
|
596 |
var = varmaker.next() |
|
597 |
if role == 'subject': |
|
598 |
targettype = rschema.objects(self.e_schema)[0] |
|
599 |
card = rschema.rproperty(self.e_schema, targettype, |
|
600 |
'cardinality')[0] |
|
601 |
if card == '1': |
|
602 |
rql.append('%s %s %s' % (V, rtype, var)) |
|
603 |
else: # '?" |
|
604 |
rql.append('%s %s %s?' % (V, rtype, var)) |
|
605 |
else: |
|
606 |
targettype = rschema.subjects(self.e_schema)[1] |
|
607 |
card = rschema.rproperty(self.e_schema, targettype, |
|
608 |
'cardinality')[1] |
|
609 |
if card == '1': |
|
610 |
rql.append('%s %s %s' % (var, rtype, V)) |
|
611 |
else: # '?" |
|
612 |
rql.append('%s? %s %s' % (var, rtype, V)) |
|
613 |
assert card in '1?', '%s %s %s %s' % (self.e_schema, rtype, |
|
614 |
role, card) |
|
615 |
selected.append(((rtype, role), var)) |
|
616 |
if selected: |
|
617 |
# select V, we need it as the left most selected variable |
|
618 |
# if some outer join are included to fetch inlined relations |
|
619 |
rql = 'Any %s,%s %s' % (V, ','.join(var for attr, var in selected), |
|
620 |
','.join(rql)) |
|
621 |
execute = getattr(self.req, 'unsafe_execute', self.req.execute) |
|
622 |
rset = execute(rql, {'x': self.eid}, 'x', build_descr=False)[0] |
|
623 |
# handle attributes |
|
624 |
for i in xrange(1, lastattr): |
|
625 |
self[str(selected[i-1][0])] = rset[i] |
|
626 |
# handle relations |
|
627 |
for i in xrange(lastattr, len(rset)): |
|
628 |
rtype, x = selected[i-1][0] |
|
629 |
value = rset[i] |
|
630 |
if value is None: |
|
631 |
rrset = ResultSet([], rql, {'x': self.eid}) |
|
632 |
self.req.decorate_rset(rrset) |
|
633 |
else: |
|
634 |
rrset = self.req.eid_rset(value) |
|
635 |
self.set_related_cache(rtype, x, rrset) |
|
1474 | 636 |
|
0 | 637 |
def get_value(self, name): |
638 |
"""get value for the attribute relation <name>, query the repository |
|
639 |
to get the value if necessary. |
|
640 |
||
641 |
:type name: str |
|
642 |
:param name: name of the attribute to get |
|
643 |
""" |
|
644 |
try: |
|
645 |
value = self[name] |
|
646 |
except KeyError: |
|
647 |
if not self.is_saved(): |
|
648 |
return None |
|
649 |
rql = "Any A WHERE X eid %%(x)s, X %s A" % name |
|
650 |
# XXX should we really use unsafe_execute here?? |
|
651 |
execute = getattr(self.req, 'unsafe_execute', self.req.execute) |
|
652 |
try: |
|
653 |
rset = execute(rql, {'x': self.eid}, 'x') |
|
654 |
except Unauthorized: |
|
655 |
self[name] = value = None |
|
656 |
else: |
|
657 |
assert rset.rowcount <= 1, (self, rql, rset.rowcount) |
|
658 |
try: |
|
659 |
self[name] = value = rset.rows[0][0] |
|
660 |
except IndexError: |
|
661 |
# probably a multisource error |
|
662 |
self.critical("can't get value for attribute %s of entity with eid %s", |
|
663 |
name, self.eid) |
|
664 |
if self.e_schema.destination(name) == 'String': |
|
2320 | 665 |
# XXX (syt) imo emtpy string is better |
0 | 666 |
self[name] = value = self.req._('unaccessible') |
667 |
else: |
|
668 |
self[name] = value = None |
|
669 |
return value |
|
670 |
||
671 |
def related(self, rtype, role='subject', limit=None, entities=False): |
|
672 |
"""returns a resultset of related entities |
|
1474 | 673 |
|
0 | 674 |
:param role: is the role played by 'self' in the relation ('subject' or 'object') |
675 |
:param limit: resultset's maximum size |
|
676 |
:param entities: if True, the entites are returned; if False, a result set is returned |
|
677 |
""" |
|
678 |
try: |
|
679 |
return self.related_cache(rtype, role, entities, limit) |
|
680 |
except KeyError: |
|
681 |
pass |
|
682 |
assert self.has_eid() |
|
683 |
rql = self.related_rql(rtype, role) |
|
684 |
rset = self.req.execute(rql, {'x': self.eid}, 'x') |
|
685 |
self.set_related_cache(rtype, role, rset) |
|
686 |
return self.related(rtype, role, limit, entities) |
|
687 |
||
688 |
def related_rql(self, rtype, role='subject'): |
|
689 |
rschema = self.schema[rtype] |
|
690 |
if role == 'subject': |
|
691 |
targettypes = rschema.objects(self.e_schema) |
|
692 |
restriction = 'E eid %%(x)s, E %s X' % rtype |
|
693 |
card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
|
694 |
else: |
|
695 |
targettypes = rschema.subjects(self.e_schema) |
|
696 |
restriction = 'E eid %%(x)s, X %s E' % rtype |
|
697 |
card = greater_card(rschema, targettypes, (self.e_schema,), 1) |
|
698 |
if len(targettypes) > 1: |
|
413
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
699 |
fetchattrs_list = [] |
0 | 700 |
for ttype in targettypes: |
701 |
etypecls = self.vreg.etype_class(ttype) |
|
413
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
702 |
fetchattrs_list.append(set(etypecls.fetch_attrs)) |
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
703 |
fetchattrs = reduce(set.intersection, fetchattrs_list) |
0 | 704 |
rql = etypecls.fetch_rql(self.req.user, [restriction], fetchattrs, |
705 |
settype=False) |
|
706 |
else: |
|
707 |
etypecls = self.vreg.etype_class(targettypes[0]) |
|
708 |
rql = etypecls.fetch_rql(self.req.user, [restriction], settype=False) |
|
709 |
# optimisation: remove ORDERBY if cardinality is 1 or ? (though |
|
710 |
# greater_card return 1 for those both cases) |
|
711 |
if card == '1': |
|
712 |
if ' ORDERBY ' in rql: |
|
713 |
rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0], |
|
714 |
rql.split(' WHERE ', 1)[1]) |
|
715 |
elif not ' ORDERBY ' in rql: |
|
716 |
args = tuple(rql.split(' WHERE ', 1)) |
|
717 |
rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % args |
|
718 |
return rql |
|
1474 | 719 |
|
0 | 720 |
# generic vocabulary methods ############################################## |
721 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2421
diff
changeset
|
722 |
@deprecated('see new form api') |
0 | 723 |
def vocabulary(self, rtype, role='subject', limit=None): |
724 |
"""vocabulary functions must return a list of couples |
|
725 |
(label, eid) that will typically be used to fill the |
|
726 |
edition view's combobox. |
|
1474 | 727 |
|
0 | 728 |
If `eid` is None in one of these couples, it should be |
729 |
interpreted as a separator in case vocabulary results are grouped |
|
730 |
""" |
|
1008
f54b4309a3f5
bw compat for vocabulary using new form
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
731 |
from logilab.common.testlib import mock_object |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
732 |
form = self.vreg.select('forms', 'edition', self.req, entity=self) |
1008
f54b4309a3f5
bw compat for vocabulary using new form
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
733 |
field = mock_object(name=rtype, role=role) |
1031
1a89683cb687
restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents:
1008
diff
changeset
|
734 |
return form.form_field_vocabulary(field, limit) |
1474 | 735 |
|
0 | 736 |
def unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
737 |
vocabconstraints=True): |
|
738 |
"""build a rql to fetch `targettype` entities unrelated to this entity |
|
739 |
using (rtype, role) relation |
|
740 |
""" |
|
741 |
ordermethod = ordermethod or 'fetch_unrelated_order' |
|
742 |
if isinstance(rtype, basestring): |
|
743 |
rtype = self.schema.rschema(rtype) |
|
744 |
if role == 'subject': |
|
745 |
evar, searchedvar = 'S', 'O' |
|
746 |
subjtype, objtype = self.e_schema, targettype |
|
747 |
else: |
|
748 |
searchedvar, evar = 'S', 'O' |
|
749 |
objtype, subjtype = self.e_schema, targettype |
|
750 |
if self.has_eid(): |
|
751 |
restriction = ['NOT S %s O' % rtype, '%s eid %%(x)s' % evar] |
|
752 |
else: |
|
753 |
restriction = [] |
|
754 |
constraints = rtype.rproperty(subjtype, objtype, 'constraints') |
|
755 |
if vocabconstraints: |
|
756 |
# RQLConstraint is a subclass for RQLVocabularyConstraint, so they |
|
757 |
# will be included as well |
|
758 |
restriction += [cstr.restriction for cstr in constraints |
|
759 |
if isinstance(cstr, RQLVocabularyConstraint)] |
|
760 |
else: |
|
761 |
restriction += [cstr.restriction for cstr in constraints |
|
762 |
if isinstance(cstr, RQLConstraint)] |
|
763 |
etypecls = self.vreg.etype_class(targettype) |
|
764 |
rql = etypecls.fetch_rql(self.req.user, restriction, |
|
765 |
mainvar=searchedvar, ordermethod=ordermethod) |
|
766 |
# ensure we have an order defined |
|
767 |
if not ' ORDERBY ' in rql: |
|
768 |
before, after = rql.split(' WHERE ', 1) |
|
769 |
rql = '%s ORDERBY %s WHERE %s' % (before, searchedvar, after) |
|
770 |
return rql |
|
1474 | 771 |
|
0 | 772 |
def unrelated(self, rtype, targettype, role='subject', limit=None, |
773 |
ordermethod=None): |
|
774 |
"""return a result set of target type objects that may be related |
|
775 |
by a given relation, with self as subject or object |
|
776 |
""" |
|
777 |
rql = self.unrelated_rql(rtype, targettype, role, ordermethod) |
|
778 |
if limit is not None: |
|
779 |
before, after = rql.split(' WHERE ', 1) |
|
780 |
rql = '%s LIMIT %s WHERE %s' % (before, limit, after) |
|
781 |
if self.has_eid(): |
|
782 |
return self.req.execute(rql, {'x': self.eid}) |
|
783 |
return self.req.execute(rql) |
|
1474 | 784 |
|
0 | 785 |
# relations cache handling ################################################ |
1474 | 786 |
|
0 | 787 |
def relation_cached(self, rtype, role): |
788 |
"""return true if the given relation is already cached on the instance |
|
789 |
""" |
|
790 |
return '%s_%s' % (rtype, role) in self._related_cache |
|
1474 | 791 |
|
0 | 792 |
def related_cache(self, rtype, role, entities=True, limit=None): |
793 |
"""return values for the given relation if it's cached on the instance, |
|
794 |
else raise `KeyError` |
|
795 |
""" |
|
796 |
res = self._related_cache['%s_%s' % (rtype, role)][entities] |
|
797 |
if limit: |
|
798 |
if entities: |
|
799 |
res = res[:limit] |
|
800 |
else: |
|
801 |
res = res.limit(limit) |
|
802 |
return res |
|
1474 | 803 |
|
0 | 804 |
def set_related_cache(self, rtype, role, rset, col=0): |
805 |
"""set cached values for the given relation""" |
|
806 |
if rset: |
|
807 |
related = list(rset.entities(col)) |
|
808 |
rschema = self.schema.rschema(rtype) |
|
809 |
if role == 'subject': |
|
810 |
rcard = rschema.rproperty(self.e_schema, related[0].e_schema, |
|
811 |
'cardinality')[1] |
|
812 |
target = 'object' |
|
813 |
else: |
|
814 |
rcard = rschema.rproperty(related[0].e_schema, self.e_schema, |
|
815 |
'cardinality')[0] |
|
816 |
target = 'subject' |
|
817 |
if rcard in '?1': |
|
818 |
for rentity in related: |
|
819 |
rentity._related_cache['%s_%s' % (rtype, target)] = (self.as_rset(), [self]) |
|
820 |
else: |
|
821 |
related = [] |
|
822 |
self._related_cache['%s_%s' % (rtype, role)] = (rset, related) |
|
1474 | 823 |
|
0 | 824 |
def clear_related_cache(self, rtype=None, role=None): |
825 |
"""clear cached values for the given relation or the entire cache if |
|
826 |
no relation is given |
|
827 |
""" |
|
828 |
if rtype is None: |
|
829 |
self._related_cache = {} |
|
830 |
else: |
|
831 |
assert role |
|
832 |
self._related_cache.pop('%s_%s' % (rtype, role), None) |
|
1474 | 833 |
|
0 | 834 |
# raw edition utilities ################################################### |
1474 | 835 |
|
0 | 836 |
def set_attributes(self, **kwargs): |
837 |
assert kwargs |
|
838 |
relations = [] |
|
839 |
for key in kwargs: |
|
840 |
relations.append('X %s %%(%s)s' % (key, key)) |
|
397
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
841 |
# update current local object |
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
842 |
self.update(kwargs) |
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
843 |
# and now update the database |
0 | 844 |
kwargs['x'] = self.eid |
845 |
self.req.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations), |
|
846 |
kwargs, 'x') |
|
1474 | 847 |
|
0 | 848 |
def delete(self): |
849 |
assert self.has_eid(), self.eid |
|
850 |
self.req.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema, |
|
851 |
{'x': self.eid}) |
|
1474 | 852 |
|
0 | 853 |
# server side utilities ################################################### |
1474 | 854 |
|
0 | 855 |
def set_defaults(self): |
856 |
"""set default values according to the schema""" |
|
857 |
self._default_set = set() |
|
858 |
for attr, value in self.e_schema.defaults(): |
|
859 |
if not self.has_key(attr): |
|
860 |
self[str(attr)] = value |
|
861 |
self._default_set.add(attr) |
|
862 |
||
863 |
def check(self, creation=False): |
|
864 |
"""check this entity against its schema. Only final relation |
|
865 |
are checked here, constraint on actual relations are checked in hooks |
|
866 |
""" |
|
867 |
# necessary since eid is handled specifically and yams require it to be |
|
868 |
# in the dictionary |
|
869 |
if self.req is None: |
|
870 |
_ = unicode |
|
871 |
else: |
|
872 |
_ = self.req._ |
|
873 |
self.e_schema.check(self, creation=creation, _=_) |
|
874 |
||
875 |
def fti_containers(self, _done=None): |
|
876 |
if _done is None: |
|
877 |
_done = set() |
|
878 |
_done.add(self.eid) |
|
879 |
containers = tuple(self.e_schema.fulltext_containers()) |
|
880 |
if containers: |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
881 |
yielded = False |
0 | 882 |
for rschema, target in containers: |
883 |
if target == 'object': |
|
884 |
targets = getattr(self, rschema.type) |
|
885 |
else: |
|
886 |
targets = getattr(self, 'reverse_%s' % rschema) |
|
887 |
for entity in targets: |
|
888 |
if entity.eid in _done: |
|
889 |
continue |
|
890 |
for container in entity.fti_containers(_done): |
|
891 |
yield container |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
892 |
yielded = True |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
893 |
if not yielded: |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
894 |
yield self |
0 | 895 |
else: |
896 |
yield self |
|
1474 | 897 |
|
0 | 898 |
def get_words(self): |
899 |
"""used by the full text indexer to get words to index |
|
900 |
||
901 |
this method should only be used on the repository side since it depends |
|
902 |
on the indexer package |
|
1474 | 903 |
|
0 | 904 |
:rtype: list |
905 |
:return: the list of indexable word of this entity |
|
906 |
""" |
|
907 |
from indexer.query_objects import tokenize |
|
908 |
words = [] |
|
909 |
for rschema in self.e_schema.indexable_attributes(): |
|
910 |
try: |
|
911 |
value = self.printable_value(rschema, format='text/plain') |
|
1132 | 912 |
except TransformError: |
0 | 913 |
continue |
914 |
except: |
|
915 |
self.exception("can't add value of %s to text index for entity %s", |
|
916 |
rschema, self.eid) |
|
917 |
continue |
|
918 |
if value: |
|
919 |
words += tokenize(value) |
|
1474 | 920 |
|
0 | 921 |
for rschema, role in self.e_schema.fulltext_relations(): |
922 |
if role == 'subject': |
|
923 |
for entity in getattr(self, rschema.type): |
|
924 |
words += entity.get_words() |
|
925 |
else: # if role == 'object': |
|
926 |
for entity in getattr(self, 'reverse_%s' % rschema.type): |
|
927 |
words += entity.get_words() |
|
928 |
return words |
|
929 |
||
930 |
||
931 |
# attribute and relation descriptors ########################################## |
|
932 |
||
933 |
class Attribute(object): |
|
934 |
"""descriptor that controls schema attribute access""" |
|
935 |
||
936 |
def __init__(self, attrname): |
|
937 |
assert attrname != 'eid' |
|
938 |
self._attrname = attrname |
|
939 |
||
940 |
def __get__(self, eobj, eclass): |
|
941 |
if eobj is None: |
|
942 |
return self |
|
943 |
return eobj.get_value(self._attrname) |
|
944 |
||
945 |
def __set__(self, eobj, value): |
|
946 |
# XXX bw compat |
|
947 |
# would be better to generate UPDATE queries than the current behaviour |
|
948 |
eobj.warning("deprecated usage, don't use 'entity.attr = val' notation)") |
|
949 |
eobj[self._attrname] = value |
|
950 |
||
951 |
||
952 |
class Relation(object): |
|
953 |
"""descriptor that controls schema relation access""" |
|
954 |
_role = None # for pylint |
|
955 |
||
956 |
def __init__(self, rschema): |
|
957 |
self._rschema = rschema |
|
958 |
self._rtype = rschema.type |
|
959 |
||
960 |
def __get__(self, eobj, eclass): |
|
961 |
if eobj is None: |
|
962 |
raise AttributeError('%s cannot be only be accessed from instances' |
|
963 |
% self._rtype) |
|
964 |
return eobj.related(self._rtype, self._role, entities=True) |
|
1474 | 965 |
|
0 | 966 |
def __set__(self, eobj, value): |
967 |
raise NotImplementedError |
|
968 |
||
969 |
||
970 |
class SubjectRelation(Relation): |
|
971 |
"""descriptor that controls schema relation access""" |
|
972 |
_role = 'subject' |
|
1474 | 973 |
|
0 | 974 |
class ObjectRelation(Relation): |
975 |
"""descriptor that controls schema relation access""" |
|
976 |
_role = 'object' |
|
977 |
||
978 |
from logging import getLogger |
|
979 |
from cubicweb import set_log_methods |
|
980 |
set_log_methods(Entity, getLogger('cubicweb.entity')) |