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