author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 21 Aug 2009 14:50:20 +0200 | |
branch | 3.5 |
changeset 2954 | 48507919b6e3 |
parent 2658 | 5535857eeaa5 |
child 2793 | bfb21f7a0d13 |
child 3348 | 97dca770c028 |
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 |
||
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""" |
|
253
57e88c0ba286
check we have a description *yams attribute*
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
142
diff
changeset
|
66 |
if self.e_schema.has_subject_relation('description'): |
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 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
153 |
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
|
154 |
"""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
|
155 |
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
|
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: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
161 |
return self.__linkto[(rtype, target)] |
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[:]: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
169 |
ltrtype, eid, lttarget = linkto.split(':') |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
170 |
if rtype == ltrtype and target == lttarget: |
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)) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
177 |
self.__linkto[(rtype, target)] = linkedto |
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 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
186 |
return str(self.e_schema).lower(), {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
187 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
188 |
def pre_web_edit(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
189 |
"""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
|
190 |
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
|
191 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
192 |
Do nothing by default. |
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 |
pass |
1493 | 195 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
196 |
# server side helpers ##################################################### |
1493 | 197 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
198 |
def notification_references(self, view): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
199 |
"""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
|
200 |
for this entity. `view` is the notification view. |
1493 | 201 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
202 |
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
|
203 |
of previously sent email |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
204 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
205 |
return () |
1493 | 206 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
207 |
# XXX deprecates, may be killed once old widgets system is gone ########### |
1493 | 208 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
209 |
@classmethod |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
210 |
def get_widget(cls, rschema, x='subject'): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
211 |
"""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
|
212 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
213 |
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
|
214 |
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
|
215 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
216 |
# 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
|
217 |
from cubicweb.web.widgets import widget |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
218 |
if isinstance(rschema, basestring): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
219 |
rschema = cls.schema.rschema(rschema) |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
220 |
if x == 'subject': |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
221 |
tschema = rschema.objects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
222 |
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
|
223 |
else: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
224 |
tschema = rschema.subjects(cls.e_schema)[0] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
225 |
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
|
226 |
return wdg |
1154 | 227 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2459
diff
changeset
|
228 |
@deprecated('use EntityFieldsForm.subject_relation_vocabulary') |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2459
diff
changeset
|
233 |
@deprecated('use EntityFieldsForm.object_relation_vocabulary') |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
return form.object_relation_vocabulary(rtype, limit) |
1493 | 237 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2459
diff
changeset
|
238 |
@deprecated('use AutomaticEntityForm.[e]relations_by_category') |
0 | 239 |
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
|
240 |
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
|
241 |
return AutomaticEntityForm.erelations_by_category(self, categories, permission) |
0 | 242 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2459
diff
changeset
|
243 |
@deprecated('use AutomaticEntityForm.[e]srelations_by_category') |
0 | 244 |
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
|
245 |
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
|
246 |
return AutomaticEntityForm.esrelations_by_category(self, categories, permission) |
1493 | 247 |
|
0 | 248 |
def attribute_values(self, attrname): |
249 |
if self.has_eid() or attrname in self: |
|
250 |
try: |
|
251 |
values = self[attrname] |
|
252 |
except KeyError: |
|
253 |
values = getattr(self, attrname) |
|
254 |
# actual relation return a list of entities |
|
255 |
if isinstance(values, list): |
|
256 |
return [v.eid for v in values] |
|
257 |
return (values,) |
|
258 |
# the entity is being created, try to find default value for |
|
259 |
# this attribute |
|
260 |
try: |
|
261 |
values = self.req.form[attrname] |
|
262 |
except KeyError: |
|
263 |
try: |
|
264 |
values = self[attrname] # copying |
|
265 |
except KeyError: |
|
266 |
values = getattr(self, 'default_%s' % attrname, |
|
267 |
self.e_schema.default(attrname)) |
|
268 |
if callable(values): |
|
269 |
values = values() |
|
270 |
if values is None: |
|
271 |
values = () |
|
272 |
elif not isinstance(values, (list, tuple)): |
|
273 |
values = (values,) |
|
274 |
return values |
|
275 |
||
912 | 276 |
def use_fckeditor(self, attr): |
277 |
"""return True if fckeditor should be used to edit entity's attribute named |
|
278 |
`attr`, according to user preferences |
|
0 | 279 |
""" |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
280 |
if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'): |
912 | 281 |
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
|
282 |
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
|
283 |
return self.req.property_value('ui.default-text-format') == 'text/html' |
912 | 284 |
return False |
0 | 285 |
|
286 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
|
287 |
# configuration and we need the actual reference to avoid infinite loops |
|
288 |
# in mro |
|
289 |
ANYENTITY = AnyEntity |
|
290 |
||
291 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
292 |
if pclass is ANYENTITY: |
|
293 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
294 |
if pclass is not None: |
|
295 |
fetchattrs += pclass.fetch_attrs |
|
296 |
if mainattr is None: |
|
297 |
mainattr = fetchattrs[0] |
|
298 |
@classmethod |
|
299 |
def fetch_order(cls, attr, var): |
|
300 |
if attr == mainattr: |
|
301 |
return '%s %s' % (var, order) |
|
302 |
return None |
|
303 |
return fetchattrs, fetch_order |