author | sylvain.thenault@logilab.fr |
Mon, 23 Mar 2009 18:20:23 +0100 | |
branch | tls-sprint |
changeset 1134 | f885df228fc0 |
parent 1107 | 961a478593a5 |
child 1149 | 1e19b6ef53a1 |
permissions | -rw-r--r-- |
0 | 1 |
"""base application's entities class implementation: `AnyEntity` |
2 |
||
3 |
:organization: Logilab |
|
479 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from warnings import warn |
|
10 |
||
11 |
from logilab.common.deprecation import deprecated_function |
|
12 |
from logilab.common.decorators import cached |
|
13 |
||
14 |
from cubicweb import Unauthorized, typed_eid |
|
713
5adb6d8e5fa7
update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
15 |
from cubicweb.entity import Entity |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
912
diff
changeset
|
16 |
from cubicweb.utils import dump_class |
0 | 17 |
from cubicweb.schema import FormatConstraint |
18 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
19 |
from cubicweb.interfaces import IBreadCrumbs, IFeed |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
20 |
|
0 | 21 |
|
22 |
class AnyEntity(Entity): |
|
23 |
"""an entity instance has e_schema automagically set on the class and |
|
24 |
instances have access to their issuing cursor |
|
25 |
""" |
|
26 |
id = 'Any' |
|
27 |
__rtags__ = { |
|
28 |
'is' : ('generated', 'link'), |
|
29 |
'is_instance_of' : ('generated', 'link'), |
|
30 |
'identity' : ('generated', 'link'), |
|
31 |
||
32 |
# use primary and not generated for eid since it has to be an hidden |
|
33 |
# field in edition |
|
34 |
('eid', '*', 'subject'): 'primary', |
|
35 |
('creation_date', '*', 'subject'): 'generated', |
|
36 |
('modification_date', '*', 'subject'): 'generated', |
|
37 |
('has_text', '*', 'subject'): 'generated', |
|
38 |
||
39 |
('require_permission', '*', 'subject') : ('generated', 'link'), |
|
40 |
('owned_by', '*', 'subject') : ('generated', 'link'), |
|
41 |
('created_by', '*', 'subject') : ('generated', 'link'), |
|
42 |
||
43 |
('wf_info_for', '*', 'subject') : ('generated', 'link'), |
|
44 |
('wf_info_for', '*', 'object') : ('generated', 'link'), |
|
45 |
||
46 |
('description', '*', 'subject'): 'secondary', |
|
47 |
||
48 |
# XXX should be moved in their respective cubes |
|
49 |
('filed_under', '*', 'subject') : ('generic', 'link'), |
|
50 |
('filed_under', '*', 'object') : ('generic', 'create'), |
|
51 |
# generated since there is a componant to handle comments |
|
52 |
('comments', '*', 'subject') : ('generated', 'link'), |
|
53 |
('comments', '*', 'object') : ('generated', 'link'), |
|
54 |
} |
|
55 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
56 |
__implements__ = (IBreadCrumbs, IFeed) |
0 | 57 |
|
58 |
@classmethod |
|
59 |
def selected(cls, etype): |
|
60 |
"""the special Any entity is used as the default factory, so |
|
61 |
the actual class has to be constructed at selection time once we |
|
62 |
have an actual entity'type |
|
63 |
""" |
|
64 |
if cls.id == etype: |
|
65 |
return cls |
|
66 |
usercls = dump_class(cls, etype) |
|
67 |
usercls.id = etype |
|
68 |
usercls.__initialize__() |
|
69 |
return usercls |
|
70 |
||
71 |
fetch_attrs = ('modification_date',) |
|
72 |
@classmethod |
|
73 |
def fetch_order(cls, attr, var): |
|
74 |
"""class method used to control sort order when multiple entities of |
|
75 |
this type are fetched |
|
76 |
""" |
|
77 |
return cls.fetch_unrelated_order(attr, var) |
|
78 |
||
79 |
@classmethod |
|
80 |
def fetch_unrelated_order(cls, attr, var): |
|
81 |
"""class method used to control sort order when multiple entities of |
|
82 |
this type are fetched to use in edition (eg propose them to create a |
|
83 |
new relation on an edited entity). |
|
84 |
""" |
|
85 |
if attr == 'modification_date': |
|
86 |
return '%s DESC' % var |
|
87 |
return None |
|
88 |
||
89 |
@classmethod |
|
90 |
def __initialize__(cls): |
|
91 |
super(ANYENTITY, cls).__initialize__() # XXX |
|
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
92 |
# set a default_ATTR method for rich text format fields |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
93 |
# XXX move this away once the old widgets have been dropped! |
0 | 94 |
eschema = cls.e_schema |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
95 |
for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems(): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
96 |
if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
97 |
setattr(cls, 'default_%s' % metaattr, cls._default_format) |
0 | 98 |
|
99 |
# meta data api ########################################################### |
|
100 |
||
101 |
def dc_title(self): |
|
102 |
"""return a suitable *unicode* title for this entity""" |
|
103 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
104 |
if rschema.meta: |
|
105 |
continue |
|
106 |
value = self.get_value(rschema.type) |
|
107 |
if value: |
|
108 |
# make the value printable (dates, floats, bytes, etc.) |
|
109 |
return self.printable_value(rschema.type, value, attrschema.type, |
|
110 |
format='text/plain') |
|
111 |
return u'%s #%s' % (self.dc_type(), self.eid) |
|
112 |
||
113 |
def dc_long_title(self): |
|
114 |
"""return a more detailled title for this entity""" |
|
115 |
return self.dc_title() |
|
116 |
||
117 |
def dc_description(self, format='text/plain'): |
|
118 |
"""return a suitable description for this entity""" |
|
253
57e88c0ba286
check we have a description *yams attribute*
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
142
diff
changeset
|
119 |
if self.e_schema.has_subject_relation('description'): |
0 | 120 |
return self.printable_value('description', format=format) |
121 |
return u'' |
|
122 |
||
123 |
def dc_authors(self): |
|
124 |
"""return a suitable description for the author(s) of the entity""" |
|
125 |
try: |
|
126 |
return ', '.join(u.name() for u in self.owned_by) |
|
127 |
except Unauthorized: |
|
128 |
return u'' |
|
129 |
||
130 |
def dc_creator(self): |
|
131 |
"""return a suitable description for the creator of the entity""" |
|
132 |
if self.creator: |
|
133 |
return self.creator.name() |
|
134 |
return u'' |
|
135 |
||
136 |
def dc_date(self, date_format=None):# XXX default to ISO 8601 ? |
|
137 |
"""return latest modification date of this entity""" |
|
138 |
return self.format_date(self.modification_date, date_format=date_format) |
|
139 |
||
140 |
def dc_type(self, form=''): |
|
141 |
"""return the display name for the type of this entity (translated)""" |
|
142 |
return self.e_schema.display_name(self.req, form) |
|
143 |
||
144 |
def dc_language(self): |
|
145 |
"""return language used by this entity (translated)""" |
|
146 |
# check if entities has internationalizable attributes |
|
147 |
# XXX one is enough or check if all String attributes are internationalizable? |
|
148 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
149 |
if rschema.rproperty(self.e_schema, attrschema, |
|
150 |
'internationalizable'): |
|
151 |
return self.req._(self.req.user.property_value('ui.language')) |
|
152 |
return self.req._(self.vreg.property_value('ui.language')) |
|
153 |
||
154 |
@property |
|
155 |
def creator(self): |
|
156 |
"""return the EUser entity which has created this entity, or None if |
|
157 |
unknown or if the curent user doesn't has access to this euser |
|
158 |
""" |
|
159 |
try: |
|
160 |
return self.created_by[0] |
|
161 |
except (Unauthorized, IndexError): |
|
162 |
return None |
|
163 |
||
164 |
def breadcrumbs(self, view=None, recurs=False): |
|
165 |
path = [self] |
|
166 |
if hasattr(self, 'parent'): |
|
167 |
parent = self.parent() |
|
168 |
if parent is not None: |
|
169 |
try: |
|
170 |
path = parent.breadcrumbs(view, True) + [self] |
|
171 |
except TypeError: |
|
172 |
warn("breadcrumbs method's now takes two arguments " |
|
173 |
"(view=None, recurs=False), please update", |
|
174 |
DeprecationWarning) |
|
175 |
path = parent.breadcrumbs(view) + [self] |
|
176 |
if not recurs: |
|
177 |
if view is None: |
|
178 |
if 'vtitle' in self.req.form: |
|
179 |
# embeding for instance |
|
180 |
path.append( self.req.form['vtitle'] ) |
|
181 |
elif view.id != 'primary' and hasattr(view, 'title'): |
|
182 |
path.append( self.req._(view.title) ) |
|
183 |
return path |
|
184 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
185 |
## IFeed interface ######################################################## |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
186 |
|
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
187 |
def rss_feed_url(self): |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
188 |
return self.absolute_url(vid='rss') |
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
189 |
|
0 | 190 |
# abstractions making the whole things (well, some at least) working ###### |
191 |
||
192 |
def sortvalue(self, rtype=None): |
|
193 |
"""return a value which can be used to sort this entity or given |
|
194 |
entity's attribute |
|
195 |
""" |
|
196 |
if rtype is None: |
|
197 |
return self.dc_title().lower() |
|
198 |
value = self.get_value(rtype) |
|
199 |
# do not restrict to `unicode` because Bytes will return a `str` value |
|
200 |
if isinstance(value, basestring): |
|
201 |
return self.printable_value(rtype, format='text/plain').lower() |
|
202 |
return value |
|
203 |
||
204 |
def add_related_schemas(self): |
|
205 |
"""this is actually used ui method to generate 'addrelated' actions from |
|
206 |
the schema. |
|
207 |
||
208 |
If you're using explicit 'addrelated' actions for an entity types, you |
|
209 |
should probably overrides this method to return an empty list else you |
|
210 |
may get some unexpected actions. |
|
211 |
""" |
|
212 |
req = self.req |
|
213 |
eschema = self.e_schema |
|
214 |
for role, rschemas in (('subject', eschema.subject_relations()), |
|
215 |
('object', eschema.object_relations())): |
|
216 |
for rschema in rschemas: |
|
217 |
if rschema.is_final(): |
|
218 |
continue |
|
219 |
# check the relation can be added as well |
|
220 |
if role == 'subject'and not rschema.has_perm(req, 'add', fromeid=self.eid): |
|
221 |
continue |
|
222 |
if role == 'object'and not rschema.has_perm(req, 'add', toeid=self.eid): |
|
223 |
continue |
|
224 |
# check the target types can be added as well |
|
225 |
for teschema in rschema.targets(eschema, role): |
|
226 |
if not self.relation_mode(rschema, teschema, role) == 'create': |
|
227 |
continue |
|
228 |
if teschema.has_local_role('add') or teschema.has_perm(req, 'add'): |
|
229 |
yield rschema, teschema, role |
|
230 |
||
231 |
def relation_mode(self, rtype, targettype, role='subject'): |
|
232 |
"""return a string telling if the given relation is usually created |
|
233 |
to a new entity ('create' mode) or to an existant entity ('link' mode) |
|
234 |
""" |
|
235 |
return self.rtags.get_mode(rtype, targettype, role) |
|
236 |
||
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
237 |
# edition helper functions ################################################ |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
238 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
239 |
def linked_to(self, rtype, target, remove=True): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
240 |
"""if entity should be linked to another using __linkto form param for |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
241 |
the given relation/target, return eids of related entities |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
242 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
243 |
This method is consuming matching link-to information from form params |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
244 |
if `remove` is True (by default). |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
245 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
246 |
try: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
247 |
return self.__linkto[(rtype, target)] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
248 |
except AttributeError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
249 |
self.__linkto = {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
250 |
except KeyError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
251 |
pass |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
252 |
linktos = list(self.req.list_form_param('__linkto')) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
253 |
linkedto = [] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
254 |
for linkto in linktos[:]: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
255 |
ltrtype, eid, lttarget = linkto.split(':') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
256 |
if rtype == ltrtype and target == lttarget: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
257 |
# delete __linkto from form param to avoid it being added as |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
258 |
# hidden input |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
259 |
if remove: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
260 |
linktos.remove(linkto) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
261 |
self.req.form['__linkto'] = linktos |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
262 |
linkedto.append(typed_eid(eid)) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
263 |
self.__linkto[(rtype, target)] = linkedto |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
264 |
return linkedto |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
265 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
266 |
# edit controller callbacks ############################################### |
0 | 267 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
268 |
def after_deletion_path(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
269 |
"""return (path, parameters) which should be used as redirect |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
270 |
information when this entity is being deleted |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
271 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
272 |
return str(self.e_schema).lower(), {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
273 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
274 |
def pre_web_edit(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
275 |
"""callback called by the web editcontroller when an entity will be |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
276 |
created/modified, to let a chance to do some entity specific stuff. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
277 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
278 |
Do nothing by default. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
279 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
280 |
pass |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
281 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
282 |
# server side helpers ##################################################### |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
283 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
284 |
def notification_references(self, view): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
285 |
"""used to control References field of email send on notification |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
286 |
for this entity. `view` is the notification view. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
287 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
288 |
Should return a list of eids which can be used to generate message ids |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
289 |
of previously sent email |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
290 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
291 |
return () |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
292 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
293 |
# XXX deprecates, may be killed once old widgets system is gone ########### |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
294 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
295 |
@classmethod |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
296 |
def get_widget(cls, rschema, x='subject'): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
297 |
"""return a widget to view or edit a relation |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
298 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
299 |
notice that when the relation support multiple target types, the widget |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
300 |
is necessarily the same for all those types |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
301 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
302 |
# let ImportError propage if web par isn't available |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
303 |
from cubicweb.web.widgets import widget |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
304 |
if isinstance(rschema, basestring): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
305 |
rschema = cls.schema.rschema(rschema) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
306 |
if x == 'subject': |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
307 |
tschema = rschema.objects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
308 |
wdg = widget(cls.vreg, cls, rschema, tschema, 'subject') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
309 |
else: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
310 |
tschema = rschema.subjects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
311 |
wdg = widget(cls.vreg, tschema, rschema, cls, 'object') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
312 |
return wdg |
0 | 313 |
def relations_by_category(self, categories=None, permission=None): |
314 |
if categories is not None: |
|
315 |
if not isinstance(categories, (list, tuple, set, frozenset)): |
|
316 |
categories = (categories,) |
|
317 |
if not isinstance(categories, (set, frozenset)): |
|
318 |
categories = frozenset(categories) |
|
319 |
eschema, rtags = self.e_schema, self.rtags |
|
320 |
if self.has_eid(): |
|
321 |
eid = self.eid |
|
322 |
else: |
|
323 |
eid = None |
|
324 |
for rschema, targetschemas, role in eschema.relation_definitions(True): |
|
325 |
if rschema in ('identity', 'has_text'): |
|
326 |
continue |
|
327 |
# check category first, potentially lower cost than checking |
|
328 |
# permission which may imply rql queries |
|
329 |
if categories is not None: |
|
330 |
targetschemas = [tschema for tschema in targetschemas |
|
331 |
if rtags.get_tags(rschema.type, tschema.type, role).intersection(categories)] |
|
332 |
if not targetschemas: |
|
333 |
continue |
|
334 |
tags = rtags.get_tags(rschema.type, role=role) |
|
335 |
if permission is not None: |
|
336 |
# tag allowing to hijack the permission machinery when |
|
337 |
# permission is not verifiable until the entity is actually |
|
338 |
# created... |
|
339 |
if eid is None and ('%s_on_new' % permission) in tags: |
|
340 |
yield (rschema, targetschemas, role) |
|
341 |
continue |
|
342 |
if rschema.is_final(): |
|
343 |
if not rschema.has_perm(self.req, permission, eid): |
|
344 |
continue |
|
345 |
elif role == 'subject': |
|
346 |
if not ((eid is None and rschema.has_local_role(permission)) or |
|
347 |
rschema.has_perm(self.req, permission, fromeid=eid)): |
|
348 |
continue |
|
349 |
# on relation with cardinality 1 or ?, we need delete perm as well |
|
350 |
# if the relation is already set |
|
351 |
if (permission == 'add' |
|
352 |
and rschema.cardinality(eschema, targetschemas[0], role) in '1?' |
|
353 |
and self.has_eid() and self.related(rschema.type, role) |
|
354 |
and not rschema.has_perm(self.req, 'delete', fromeid=eid, |
|
355 |
toeid=self.related(rschema.type, role)[0][0])): |
|
356 |
continue |
|
357 |
elif role == 'object': |
|
358 |
if not ((eid is None and rschema.has_local_role(permission)) or |
|
359 |
rschema.has_perm(self.req, permission, toeid=eid)): |
|
360 |
continue |
|
361 |
# on relation with cardinality 1 or ?, we need delete perm as well |
|
362 |
# if the relation is already set |
|
363 |
if (permission == 'add' |
|
364 |
and rschema.cardinality(targetschemas[0], eschema, role) in '1?' |
|
365 |
and self.has_eid() and self.related(rschema.type, role) |
|
366 |
and not rschema.has_perm(self.req, 'delete', toeid=eid, |
|
367 |
fromeid=self.related(rschema.type, role)[0][0])): |
|
368 |
continue |
|
369 |
yield (rschema, targetschemas, role) |
|
370 |
||
371 |
def srelations_by_category(self, categories=None, permission=None): |
|
372 |
result = [] |
|
373 |
for rschema, ttypes, target in self.relations_by_category(categories, |
|
374 |
permission): |
|
375 |
if rschema.is_final(): |
|
376 |
continue |
|
377 |
result.append( (rschema.display_name(self.req, target), rschema, target) ) |
|
378 |
return sorted(result) |
|
912 | 379 |
|
380 |
def _default_format(self): |
|
381 |
return self.req.property_value('ui.default-text-format') |
|
0 | 382 |
|
383 |
def attribute_values(self, attrname): |
|
384 |
if self.has_eid() or attrname in self: |
|
385 |
try: |
|
386 |
values = self[attrname] |
|
387 |
except KeyError: |
|
388 |
values = getattr(self, attrname) |
|
389 |
# actual relation return a list of entities |
|
390 |
if isinstance(values, list): |
|
391 |
return [v.eid for v in values] |
|
392 |
return (values,) |
|
393 |
# the entity is being created, try to find default value for |
|
394 |
# this attribute |
|
395 |
try: |
|
396 |
values = self.req.form[attrname] |
|
397 |
except KeyError: |
|
398 |
try: |
|
399 |
values = self[attrname] # copying |
|
400 |
except KeyError: |
|
401 |
values = getattr(self, 'default_%s' % attrname, |
|
402 |
self.e_schema.default(attrname)) |
|
403 |
if callable(values): |
|
404 |
values = values() |
|
405 |
if values is None: |
|
406 |
values = () |
|
407 |
elif not isinstance(values, (list, tuple)): |
|
408 |
values = (values,) |
|
409 |
return values |
|
410 |
||
912 | 411 |
def use_fckeditor(self, attr): |
412 |
"""return True if fckeditor should be used to edit entity's attribute named |
|
413 |
`attr`, according to user preferences |
|
0 | 414 |
""" |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
415 |
if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'): |
912 | 416 |
if self.has_eid() or '%s_format' % attr in self: |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
417 |
return self.attribute_metadata(attr, 'format') == 'text/html' |
1013
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
1010
diff
changeset
|
418 |
return self.req.property_value('ui.default-text-format') == 'text/html' |
912 | 419 |
return False |
0 | 420 |
|
421 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
|
422 |
# configuration and we need the actual reference to avoid infinite loops |
|
423 |
# in mro |
|
424 |
ANYENTITY = AnyEntity |
|
425 |
||
426 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
427 |
if pclass is ANYENTITY: |
|
428 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
429 |
if pclass is not None: |
|
430 |
fetchattrs += pclass.fetch_attrs |
|
431 |
if mainattr is None: |
|
432 |
mainattr = fetchattrs[0] |
|
433 |
@classmethod |
|
434 |
def fetch_order(cls, attr, var): |
|
435 |
if attr == mainattr: |
|
436 |
return '%s %s' % (var, order) |
|
437 |
return None |
|
438 |
return fetchattrs, fetch_order |