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