author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 15 Feb 2010 15:11:02 +0100 | |
branch | stable |
changeset 4571 | 794ac358dc36 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
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 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
25 |
__regid__ = '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""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
85 |
return self._cw.format_date(self.modification_date, date_format=date_format) |
0 | 86 |
|
87 |
def dc_type(self, form=''): |
|
88 |
"""return the display name for the type of this entity (translated)""" |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
89 |
return self.e_schema.display_name(self._cw, form) |
0 | 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(): |
|
4147
52fe79a98b21
3.6 remove deprecation warning
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
96 |
if rschema.rdef(self.e_schema, attrschema).internationalizable: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
97 |
return self._cw._(self._cw.user.property_value('ui.language')) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
98 |
return self._cw._(self._cw.vreg.property_value('ui.language')) |
1493 | 99 |
|
0 | 100 |
@property |
101 |
def creator(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
102 |
"""return the CWUser entity which has created this entity, or None if |
0 | 103 |
unknown or if the curent user doesn't has access to this euser |
104 |
""" |
|
105 |
try: |
|
106 |
return self.created_by[0] |
|
107 |
except (Unauthorized, IndexError): |
|
108 |
return None |
|
109 |
||
110 |
def breadcrumbs(self, view=None, recurs=False): |
|
111 |
path = [self] |
|
112 |
if hasattr(self, 'parent'): |
|
113 |
parent = self.parent() |
|
114 |
if parent is not None: |
|
115 |
try: |
|
116 |
path = parent.breadcrumbs(view, True) + [self] |
|
117 |
except TypeError: |
|
118 |
warn("breadcrumbs method's now takes two arguments " |
|
119 |
"(view=None, recurs=False), please update", |
|
120 |
DeprecationWarning) |
|
121 |
path = parent.breadcrumbs(view) + [self] |
|
122 |
if not recurs: |
|
123 |
if view is None: |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
124 |
if 'vtitle' in self._cw.form: |
0 | 125 |
# embeding for instance |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
126 |
path.append( self._cw.form['vtitle'] ) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
127 |
elif view.__regid__ != 'primary' and hasattr(view, 'title'): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
128 |
path.append( self._cw._(view.title) ) |
0 | 129 |
return path |
130 |
||
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
131 |
## IFeed interface ######################################################## |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
132 |
|
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
133 |
def rss_feed_url(self): |
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
134 |
return self.absolute_url(vid='rss') |
1493 | 135 |
|
0 | 136 |
# abstractions making the whole things (well, some at least) working ###### |
1493 | 137 |
|
0 | 138 |
def sortvalue(self, rtype=None): |
139 |
"""return a value which can be used to sort this entity or given |
|
140 |
entity's attribute |
|
141 |
""" |
|
142 |
if rtype is None: |
|
143 |
return self.dc_title().lower() |
|
144 |
value = self.get_value(rtype) |
|
145 |
# do not restrict to `unicode` because Bytes will return a `str` value |
|
146 |
if isinstance(value, basestring): |
|
147 |
return self.printable_value(rtype, format='text/plain').lower() |
|
148 |
return value |
|
149 |
||
1493 | 150 |
# edition helper functions ################################################ |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
151 |
|
3348
97dca770c028
this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
152 |
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
|
153 |
"""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
|
154 |
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
|
155 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
156 |
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
|
157 |
if `remove` is True (by default). |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
158 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
159 |
try: |
3348
97dca770c028
this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
160 |
return self.__linkto[(rtype, role)] |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
161 |
except AttributeError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
162 |
self.__linkto = {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
163 |
except KeyError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
164 |
pass |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
165 |
linktos = list(self._cw.list_form_param('__linkto')) |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
166 |
linkedto = [] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
if rtype == ltrtype and role == ltrole: |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
170 |
# 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
|
171 |
# hidden input |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
172 |
if remove: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
173 |
linktos.remove(linkto) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
174 |
self._cw.form['__linkto'] = linktos |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
175 |
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
|
176 |
self.__linkto[(rtype, role)] = linkedto |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
177 |
return linkedto |
1493 | 178 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
179 |
# edit controller callbacks ############################################### |
1493 | 180 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
181 |
def after_deletion_path(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
182 |
"""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
|
183 |
information when this entity is being deleted |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
184 |
""" |
3578
7f026d15cc82
doesn't necessarily have parent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3566
diff
changeset
|
185 |
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
|
186 |
return self.parent().rest_path(), {} |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
187 |
return str(self.e_schema).lower(), {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
188 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
189 |
def pre_web_edit(self): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
190 |
"""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
|
191 |
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
|
192 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
193 |
Do nothing by default. |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
194 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
195 |
pass |
1493 | 196 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
197 |
# server side helpers ##################################################### |
1493 | 198 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
199 |
def notification_references(self, view): |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
200 |
"""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
|
201 |
for this entity. `view` is the notification view. |
1493 | 202 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
203 |
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
|
204 |
of previously sent email |
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 |
return () |
1493 | 207 |
|
0 | 208 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
209 |
# configuration and we need the actual reference to avoid infinite loops |
|
210 |
# in mro |
|
211 |
ANYENTITY = AnyEntity |
|
212 |
||
213 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
214 |
if pclass is ANYENTITY: |
|
215 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
216 |
if pclass is not None: |
|
217 |
fetchattrs += pclass.fetch_attrs |
|
218 |
if mainattr is None: |
|
219 |
mainattr = fetchattrs[0] |
|
220 |
@classmethod |
|
221 |
def fetch_order(cls, attr, var): |
|
222 |
if attr == mainattr: |
|
223 |
return '%s %s' % (var, order) |
|
224 |
return None |
|
225 |
return fetchattrs, fetch_order |