author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 18:26:31 +0200 | |
changeset 2496 | fbd1fd2ca312 |
parent 2459 | d088d0ff48a1 |
child 2613 | 5e19c2bb370e |
permissions | -rw-r--r-- |
0 | 1 |
"""base application's entities class implementation: `AnyEntity` |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1854
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:
1854
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from warnings import warn |
|
11 |
||
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
12 |
from logilab.common.deprecation import deprecated_function, obsolete |
0 | 13 |
from logilab.common.decorators import cached |
14 |
||
15 |
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
|
16 |
from cubicweb.entity import Entity |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
912
diff
changeset
|
17 |
from cubicweb.utils import dump_class |
0 | 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 |
""" |
|
1493 | 26 |
id = 'Any' |
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
27 |
__implements__ = (IBreadCrumbs, IFeed) |
1417 | 28 |
|
0 | 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 |
|
1493 | 41 |
|
0 | 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) |
|
1493 | 49 |
|
0 | 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 |
# meta data api ########################################################### |
|
61 |
||
62 |
def dc_title(self): |
|
63 |
"""return a suitable *unicode* title for this entity""" |
|
64 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
65 |
if rschema.meta: |
|
66 |
continue |
|
67 |
value = self.get_value(rschema.type) |
|
68 |
if value: |
|
69 |
# make the value printable (dates, floats, bytes, etc.) |
|
70 |
return self.printable_value(rschema.type, value, attrschema.type, |
|
71 |
format='text/plain') |
|
72 |
return u'%s #%s' % (self.dc_type(), self.eid) |
|
73 |
||
74 |
def dc_long_title(self): |
|
75 |
"""return a more detailled title for this entity""" |
|
76 |
return self.dc_title() |
|
1493 | 77 |
|
0 | 78 |
def dc_description(self, format='text/plain'): |
79 |
"""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
|
80 |
if self.e_schema.has_subject_relation('description'): |
0 | 81 |
return self.printable_value('description', format=format) |
82 |
return u'' |
|
83 |
||
84 |
def dc_authors(self): |
|
85 |
"""return a suitable description for the author(s) of the entity""" |
|
86 |
try: |
|
87 |
return ', '.join(u.name() for u in self.owned_by) |
|
88 |
except Unauthorized: |
|
89 |
return u'' |
|
90 |
||
91 |
def dc_creator(self): |
|
92 |
"""return a suitable description for the creator of the entity""" |
|
93 |
if self.creator: |
|
94 |
return self.creator.name() |
|
95 |
return u'' |
|
96 |
||
97 |
def dc_date(self, date_format=None):# XXX default to ISO 8601 ? |
|
98 |
"""return latest modification date of this entity""" |
|
99 |
return self.format_date(self.modification_date, date_format=date_format) |
|
100 |
||
101 |
def dc_type(self, form=''): |
|
102 |
"""return the display name for the type of this entity (translated)""" |
|
103 |
return self.e_schema.display_name(self.req, form) |
|
104 |
||
105 |
def dc_language(self): |
|
106 |
"""return language used by this entity (translated)""" |
|
107 |
# check if entities has internationalizable attributes |
|
108 |
# XXX one is enough or check if all String attributes are internationalizable? |
|
109 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
110 |
if rschema.rproperty(self.e_schema, attrschema, |
|
111 |
'internationalizable'): |
|
112 |
return self.req._(self.req.user.property_value('ui.language')) |
|
113 |
return self.req._(self.vreg.property_value('ui.language')) |
|
1493 | 114 |
|
0 | 115 |
@property |
116 |
def creator(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
117 |
"""return the CWUser entity which has created this entity, or None if |
0 | 118 |
unknown or if the curent user doesn't has access to this euser |
119 |
""" |
|
120 |
try: |
|
121 |
return self.created_by[0] |
|
122 |
except (Unauthorized, IndexError): |
|
123 |
return None |
|
124 |
||
125 |
def breadcrumbs(self, view=None, recurs=False): |
|
126 |
path = [self] |
|
127 |
if hasattr(self, 'parent'): |
|
128 |
parent = self.parent() |
|
129 |
if parent is not None: |
|
130 |
try: |
|
131 |
path = parent.breadcrumbs(view, True) + [self] |
|
132 |
except TypeError: |
|
133 |
warn("breadcrumbs method's now takes two arguments " |
|
134 |
"(view=None, recurs=False), please update", |
|
135 |
DeprecationWarning) |
|
136 |
path = parent.breadcrumbs(view) + [self] |
|
137 |
if not recurs: |
|
138 |
if view is None: |
|
139 |
if 'vtitle' in self.req.form: |
|
140 |
# embeding for instance |
|
141 |
path.append( self.req.form['vtitle'] ) |
|
142 |
elif view.id != 'primary' and hasattr(view, 'title'): |
|
143 |
path.append( self.req._(view.title) ) |
|
144 |
return path |
|
145 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
146 |
## IFeed interface ######################################################## |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
147 |
|
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
148 |
def rss_feed_url(self): |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
149 |
return self.absolute_url(vid='rss') |
1493 | 150 |
|
0 | 151 |
# abstractions making the whole things (well, some at least) working ###### |
1493 | 152 |
|
0 | 153 |
def sortvalue(self, rtype=None): |
154 |
"""return a value which can be used to sort this entity or given |
|
155 |
entity's attribute |
|
156 |
""" |
|
157 |
if rtype is None: |
|
158 |
return self.dc_title().lower() |
|
159 |
value = self.get_value(rtype) |
|
160 |
# do not restrict to `unicode` because Bytes will return a `str` value |
|
161 |
if isinstance(value, basestring): |
|
162 |
return self.printable_value(rtype, format='text/plain').lower() |
|
163 |
return value |
|
164 |
||
1493 | 165 |
# edition helper functions ################################################ |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
166 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
167 |
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
|
168 |
"""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
|
169 |
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
|
170 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
171 |
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
|
172 |
if `remove` is True (by default). |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
173 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
174 |
try: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
175 |
return self.__linkto[(rtype, target)] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
176 |
except AttributeError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
177 |
self.__linkto = {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
178 |
except KeyError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
179 |
pass |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
180 |
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
|
181 |
linkedto = [] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
182 |
for linkto in linktos[:]: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
183 |
ltrtype, eid, lttarget = linkto.split(':') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
184 |
if rtype == ltrtype and target == lttarget: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
185 |
# 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
|
186 |
# hidden input |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
187 |
if remove: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
188 |
linktos.remove(linkto) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
189 |
self.req.form['__linkto'] = linktos |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
190 |
linkedto.append(typed_eid(eid)) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
191 |
self.__linkto[(rtype, target)] = linkedto |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
192 |
return linkedto |
1493 | 193 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
194 |
# edit controller callbacks ############################################### |
1493 | 195 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
196 |
def after_deletion_path(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
197 |
"""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
|
198 |
information when this entity is being deleted |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
199 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
200 |
return str(self.e_schema).lower(), {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
201 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
202 |
def pre_web_edit(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
203 |
"""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
|
204 |
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
|
205 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
206 |
Do nothing by default. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
207 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
208 |
pass |
1493 | 209 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
210 |
# server side helpers ##################################################### |
1493 | 211 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
212 |
def notification_references(self, view): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
213 |
"""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
|
214 |
for this entity. `view` is the notification view. |
1493 | 215 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
216 |
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
|
217 |
of previously sent email |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
218 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
219 |
return () |
1493 | 220 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
221 |
# XXX deprecates, may be killed once old widgets system is gone ########### |
1493 | 222 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
223 |
@classmethod |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
224 |
def get_widget(cls, rschema, x='subject'): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
225 |
"""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
|
226 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
230 |
# 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
|
231 |
from cubicweb.web.widgets import widget |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
232 |
if isinstance(rschema, basestring): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
233 |
rschema = cls.schema.rschema(rschema) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
234 |
if x == 'subject': |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
235 |
tschema = rschema.objects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
236 |
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
|
237 |
else: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
238 |
tschema = rschema.subjects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
239 |
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
|
240 |
return wdg |
1154 | 241 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
242 |
@obsolete('use EntityFieldsForm.subject_relation_vocabulary') |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
243 |
def subject_relation_vocabulary(self, rtype, limit): |
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
|
244 |
form = self.vreg.select('forms', 'edition', self.req, entity=self) |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
245 |
return form.subject_relation_vocabulary(rtype, limit) |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
246 |
|
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
247 |
@obsolete('use EntityFieldsForm.object_relation_vocabulary') |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
248 |
def object_relation_vocabulary(self, rtype, limit): |
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
|
249 |
form = self.vreg.select('forms', 'edition', self.req, entity=self) |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
250 |
return form.object_relation_vocabulary(rtype, limit) |
1493 | 251 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
252 |
@obsolete('use AutomaticEntityForm.[e]relations_by_category') |
0 | 253 |
def relations_by_category(self, categories=None, permission=None): |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1493
diff
changeset
|
254 |
from cubicweb.web.views.autoform import AutomaticEntityForm |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
255 |
return AutomaticEntityForm.erelations_by_category(self, categories, permission) |
0 | 256 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
257 |
@obsolete('use AutomaticEntityForm.[e]srelations_by_category') |
0 | 258 |
def srelations_by_category(self, categories=None, permission=None): |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1493
diff
changeset
|
259 |
from cubicweb.web.views.autoform import AutomaticEntityForm |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
260 |
return AutomaticEntityForm.esrelations_by_category(self, categories, permission) |
1493 | 261 |
|
0 | 262 |
def attribute_values(self, attrname): |
263 |
if self.has_eid() or attrname in self: |
|
264 |
try: |
|
265 |
values = self[attrname] |
|
266 |
except KeyError: |
|
267 |
values = getattr(self, attrname) |
|
268 |
# actual relation return a list of entities |
|
269 |
if isinstance(values, list): |
|
270 |
return [v.eid for v in values] |
|
271 |
return (values,) |
|
272 |
# the entity is being created, try to find default value for |
|
273 |
# this attribute |
|
274 |
try: |
|
275 |
values = self.req.form[attrname] |
|
276 |
except KeyError: |
|
277 |
try: |
|
278 |
values = self[attrname] # copying |
|
279 |
except KeyError: |
|
280 |
values = getattr(self, 'default_%s' % attrname, |
|
281 |
self.e_schema.default(attrname)) |
|
282 |
if callable(values): |
|
283 |
values = values() |
|
284 |
if values is None: |
|
285 |
values = () |
|
286 |
elif not isinstance(values, (list, tuple)): |
|
287 |
values = (values,) |
|
288 |
return values |
|
289 |
||
912 | 290 |
def use_fckeditor(self, attr): |
291 |
"""return True if fckeditor should be used to edit entity's attribute named |
|
292 |
`attr`, according to user preferences |
|
0 | 293 |
""" |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
294 |
if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'): |
912 | 295 |
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
|
296 |
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
|
297 |
return self.req.property_value('ui.default-text-format') == 'text/html' |
912 | 298 |
return False |
0 | 299 |
|
300 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
|
301 |
# configuration and we need the actual reference to avoid infinite loops |
|
302 |
# in mro |
|
303 |
ANYENTITY = AnyEntity |
|
304 |
||
305 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
306 |
if pclass is ANYENTITY: |
|
307 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
308 |
if pclass is not None: |
|
309 |
fetchattrs += pclass.fetch_attrs |
|
310 |
if mainattr is None: |
|
311 |
mainattr = fetchattrs[0] |
|
312 |
@classmethod |
|
313 |
def fetch_order(cls, attr, var): |
|
314 |
if attr == mainattr: |
|
315 |
return '%s %s' % (var, order) |
|
316 |
return None |
|
317 |
return fetchattrs, fetch_order |