author | sylvain.thenault@logilab.fr |
Wed, 15 Apr 2009 14:41:06 +0200 | |
branch | tls-sprint |
changeset 1360 | 13ae1121835e |
parent 1182 | 78e4080252be |
child 1398 | 5fe84a5f7035 |
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 |
||
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
11 |
from logilab.common.deprecation import deprecated_function, obsolete |
0 | 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 |
""" |
|
1154 | 26 |
id = 'Any' |
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
27 |
__implements__ = (IBreadCrumbs, IFeed) |
0 | 28 |
|
29 |
@classmethod |
|
30 |
def selected(cls, etype): |
|
31 |
"""the special Any entity is used as the default factory, so |
|
32 |
the actual class has to be constructed at selection time once we |
|
33 |
have an actual entity'type |
|
34 |
""" |
|
35 |
if cls.id == etype: |
|
36 |
return cls |
|
37 |
usercls = dump_class(cls, etype) |
|
38 |
usercls.id = etype |
|
39 |
usercls.__initialize__() |
|
40 |
return usercls |
|
41 |
||
42 |
fetch_attrs = ('modification_date',) |
|
43 |
@classmethod |
|
44 |
def fetch_order(cls, attr, var): |
|
45 |
"""class method used to control sort order when multiple entities of |
|
46 |
this type are fetched |
|
47 |
""" |
|
48 |
return cls.fetch_unrelated_order(attr, var) |
|
49 |
||
50 |
@classmethod |
|
51 |
def fetch_unrelated_order(cls, attr, var): |
|
52 |
"""class method used to control sort order when multiple entities of |
|
53 |
this type are fetched to use in edition (eg propose them to create a |
|
54 |
new relation on an edited entity). |
|
55 |
""" |
|
56 |
if attr == 'modification_date': |
|
57 |
return '%s DESC' % var |
|
58 |
return None |
|
59 |
||
60 |
@classmethod |
|
61 |
def __initialize__(cls): |
|
62 |
super(ANYENTITY, cls).__initialize__() # XXX |
|
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
63 |
# set a default_ATTR method for rich text format fields |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
64 |
# XXX move this away once the old widgets have been dropped! |
0 | 65 |
eschema = cls.e_schema |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
66 |
for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems(): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
67 |
if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr): |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1086
diff
changeset
|
68 |
setattr(cls, 'default_%s' % metaattr, cls._default_format) |
0 | 69 |
|
70 |
# meta data api ########################################################### |
|
71 |
||
72 |
def dc_title(self): |
|
73 |
"""return a suitable *unicode* title for this entity""" |
|
74 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
75 |
if rschema.meta: |
|
76 |
continue |
|
77 |
value = self.get_value(rschema.type) |
|
78 |
if value: |
|
79 |
# make the value printable (dates, floats, bytes, etc.) |
|
80 |
return self.printable_value(rschema.type, value, attrschema.type, |
|
81 |
format='text/plain') |
|
82 |
return u'%s #%s' % (self.dc_type(), self.eid) |
|
83 |
||
84 |
def dc_long_title(self): |
|
85 |
"""return a more detailled title for this entity""" |
|
86 |
return self.dc_title() |
|
87 |
||
88 |
def dc_description(self, format='text/plain'): |
|
89 |
"""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
|
90 |
if self.e_schema.has_subject_relation('description'): |
0 | 91 |
return self.printable_value('description', format=format) |
92 |
return u'' |
|
93 |
||
94 |
def dc_authors(self): |
|
95 |
"""return a suitable description for the author(s) of the entity""" |
|
96 |
try: |
|
97 |
return ', '.join(u.name() for u in self.owned_by) |
|
98 |
except Unauthorized: |
|
99 |
return u'' |
|
100 |
||
101 |
def dc_creator(self): |
|
102 |
"""return a suitable description for the creator of the entity""" |
|
103 |
if self.creator: |
|
104 |
return self.creator.name() |
|
105 |
return u'' |
|
106 |
||
107 |
def dc_date(self, date_format=None):# XXX default to ISO 8601 ? |
|
108 |
"""return latest modification date of this entity""" |
|
109 |
return self.format_date(self.modification_date, date_format=date_format) |
|
110 |
||
111 |
def dc_type(self, form=''): |
|
112 |
"""return the display name for the type of this entity (translated)""" |
|
113 |
return self.e_schema.display_name(self.req, form) |
|
114 |
||
115 |
def dc_language(self): |
|
116 |
"""return language used by this entity (translated)""" |
|
117 |
# check if entities has internationalizable attributes |
|
118 |
# XXX one is enough or check if all String attributes are internationalizable? |
|
119 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
120 |
if rschema.rproperty(self.e_schema, attrschema, |
|
121 |
'internationalizable'): |
|
122 |
return self.req._(self.req.user.property_value('ui.language')) |
|
123 |
return self.req._(self.vreg.property_value('ui.language')) |
|
124 |
||
125 |
@property |
|
126 |
def creator(self): |
|
127 |
"""return the EUser entity which has created this entity, or None if |
|
128 |
unknown or if the curent user doesn't has access to this euser |
|
129 |
""" |
|
130 |
try: |
|
131 |
return self.created_by[0] |
|
132 |
except (Unauthorized, IndexError): |
|
133 |
return None |
|
134 |
||
135 |
def breadcrumbs(self, view=None, recurs=False): |
|
136 |
path = [self] |
|
137 |
if hasattr(self, 'parent'): |
|
138 |
parent = self.parent() |
|
139 |
if parent is not None: |
|
140 |
try: |
|
141 |
path = parent.breadcrumbs(view, True) + [self] |
|
142 |
except TypeError: |
|
143 |
warn("breadcrumbs method's now takes two arguments " |
|
144 |
"(view=None, recurs=False), please update", |
|
145 |
DeprecationWarning) |
|
146 |
path = parent.breadcrumbs(view) + [self] |
|
147 |
if not recurs: |
|
148 |
if view is None: |
|
149 |
if 'vtitle' in self.req.form: |
|
150 |
# embeding for instance |
|
151 |
path.append( self.req.form['vtitle'] ) |
|
152 |
elif view.id != 'primary' and hasattr(view, 'title'): |
|
153 |
path.append( self.req._(view.title) ) |
|
154 |
return path |
|
155 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
156 |
## IFeed interface ######################################################## |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
157 |
|
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
158 |
def rss_feed_url(self): |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
159 |
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
|
160 |
|
0 | 161 |
# abstractions making the whole things (well, some at least) working ###### |
162 |
||
163 |
def sortvalue(self, rtype=None): |
|
164 |
"""return a value which can be used to sort this entity or given |
|
165 |
entity's attribute |
|
166 |
""" |
|
167 |
if rtype is None: |
|
168 |
return self.dc_title().lower() |
|
169 |
value = self.get_value(rtype) |
|
170 |
# do not restrict to `unicode` because Bytes will return a `str` value |
|
171 |
if isinstance(value, basestring): |
|
172 |
return self.printable_value(rtype, format='text/plain').lower() |
|
173 |
return value |
|
174 |
||
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
175 |
# edition helper functions ################################################ |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
176 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
177 |
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
|
178 |
"""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
|
179 |
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
|
180 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
181 |
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
|
182 |
if `remove` is True (by default). |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
183 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
184 |
try: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
185 |
return self.__linkto[(rtype, target)] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
186 |
except AttributeError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
187 |
self.__linkto = {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
188 |
except KeyError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
189 |
pass |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
190 |
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
|
191 |
linkedto = [] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
192 |
for linkto in linktos[:]: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
193 |
ltrtype, eid, lttarget = linkto.split(':') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
194 |
if rtype == ltrtype and target == lttarget: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
195 |
# 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
|
196 |
# hidden input |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
197 |
if remove: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
198 |
linktos.remove(linkto) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
199 |
self.req.form['__linkto'] = linktos |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
200 |
linkedto.append(typed_eid(eid)) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
201 |
self.__linkto[(rtype, target)] = linkedto |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
202 |
return linkedto |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
203 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
204 |
# edit controller callbacks ############################################### |
0 | 205 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
206 |
def after_deletion_path(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
207 |
"""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
|
208 |
information when this entity is being deleted |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
209 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
210 |
return str(self.e_schema).lower(), {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
211 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
212 |
def pre_web_edit(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
213 |
"""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
|
214 |
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
|
215 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
216 |
Do nothing by default. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
217 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
218 |
pass |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
219 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
220 |
# server side helpers ##################################################### |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
221 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
222 |
def notification_references(self, view): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
223 |
"""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
|
224 |
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
|
225 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
226 |
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
|
227 |
of previously sent email |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
228 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
229 |
return () |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
230 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
231 |
# 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
|
232 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
233 |
@classmethod |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
234 |
def get_widget(cls, rschema, x='subject'): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
235 |
"""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
|
236 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
240 |
# 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
|
241 |
from cubicweb.web.widgets import widget |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
242 |
if isinstance(rschema, basestring): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
243 |
rschema = cls.schema.rschema(rschema) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
244 |
if x == 'subject': |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
245 |
tschema = rschema.objects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
246 |
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
|
247 |
else: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
248 |
tschema = rschema.subjects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
249 |
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
|
250 |
return wdg |
1154 | 251 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
252 |
@obsolete('use EntityFieldsForm.subject_relation_vocabulary') |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
253 |
def subject_relation_vocabulary(self, rtype, limit): |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
254 |
from cubicweb.web.form import EntityFieldsForm |
1182 | 255 |
return EntityFieldsForm(self.req, None, entity=self).subject_relation_vocabulary(rtype, limit) |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
256 |
|
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
257 |
@obsolete('use EntityFieldsForm.object_relation_vocabulary') |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
258 |
def object_relation_vocabulary(self, rtype, limit): |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
259 |
from cubicweb.web.form import EntityFieldsForm |
1182 | 260 |
return EntityFieldsForm(self.req, None, entity=self).object_relation_vocabulary(rtype, limit) |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
261 |
|
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
262 |
@obsolete('use AutomaticEntityForm.[e]relations_by_category') |
0 | 263 |
def relations_by_category(self, categories=None, permission=None): |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
264 |
from cubicweb.web.views.editforms import AutomaticEntityForm |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
265 |
return AutomaticEntityForm.erelations_by_category(self, categories, permission) |
0 | 266 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
267 |
@obsolete('use AutomaticEntityForm.[e]srelations_by_category') |
0 | 268 |
def srelations_by_category(self, categories=None, permission=None): |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
269 |
from cubicweb.web.views.editforms import AutomaticEntityForm |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
270 |
return AutomaticEntityForm.esrelations_by_category(self, categories, permission) |
912 | 271 |
|
272 |
def _default_format(self): |
|
273 |
return self.req.property_value('ui.default-text-format') |
|
0 | 274 |
|
275 |
def attribute_values(self, attrname): |
|
276 |
if self.has_eid() or attrname in self: |
|
277 |
try: |
|
278 |
values = self[attrname] |
|
279 |
except KeyError: |
|
280 |
values = getattr(self, attrname) |
|
281 |
# actual relation return a list of entities |
|
282 |
if isinstance(values, list): |
|
283 |
return [v.eid for v in values] |
|
284 |
return (values,) |
|
285 |
# the entity is being created, try to find default value for |
|
286 |
# this attribute |
|
287 |
try: |
|
288 |
values = self.req.form[attrname] |
|
289 |
except KeyError: |
|
290 |
try: |
|
291 |
values = self[attrname] # copying |
|
292 |
except KeyError: |
|
293 |
values = getattr(self, 'default_%s' % attrname, |
|
294 |
self.e_schema.default(attrname)) |
|
295 |
if callable(values): |
|
296 |
values = values() |
|
297 |
if values is None: |
|
298 |
values = () |
|
299 |
elif not isinstance(values, (list, tuple)): |
|
300 |
values = (values,) |
|
301 |
return values |
|
302 |
||
912 | 303 |
def use_fckeditor(self, attr): |
304 |
"""return True if fckeditor should be used to edit entity's attribute named |
|
305 |
`attr`, according to user preferences |
|
0 | 306 |
""" |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
307 |
if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'): |
912 | 308 |
if self.has_eid() or '%s_format' % attr in self: |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1182
diff
changeset
|
309 |
return self.attr_metadata(attr, 'format') == 'text/html' |
1013
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
1010
diff
changeset
|
310 |
return self.req.property_value('ui.default-text-format') == 'text/html' |
912 | 311 |
return False |
0 | 312 |
|
313 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
|
314 |
# configuration and we need the actual reference to avoid infinite loops |
|
315 |
# in mro |
|
316 |
ANYENTITY = AnyEntity |
|
317 |
||
318 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
319 |
if pclass is ANYENTITY: |
|
320 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
321 |
if pclass is not None: |
|
322 |
fetchattrs += pclass.fetch_attrs |
|
323 |
if mainattr is None: |
|
324 |
mainattr = fetchattrs[0] |
|
325 |
@classmethod |
|
326 |
def fetch_order(cls, attr, var): |
|
327 |
if attr == mainattr: |
|
328 |
return '%s %s' % (var, order) |
|
329 |
return None |
|
330 |
return fetchattrs, fetch_order |