author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 19 Oct 2009 18:15:45 +0200 | |
changeset 3735 | 80fb4083c29b |
parent 3720 | 5376aaadd16b |
child 4147 | 52fe79a98b21 |
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 |
""" |
|
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(): |
|
96 |
if rschema.rproperty(self.e_schema, attrschema, |
|
97 |
'internationalizable'): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
98 |
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
|
99 |
return self._cw._(self._cw.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: |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
125 |
if 'vtitle' in self._cw.form: |
0 | 126 |
# 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
|
127 |
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
|
128 |
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
|
129 |
path.append( self._cw._(view.title) ) |
0 | 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 |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
166 |
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
|
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) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
175 |
self._cw.form['__linkto'] = linktos |
1086
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 |
|
0 | 209 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
210 |
# configuration and we need the actual reference to avoid infinite loops |
|
211 |
# in mro |
|
212 |
ANYENTITY = AnyEntity |
|
213 |
||
214 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
215 |
if pclass is ANYENTITY: |
|
216 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
217 |
if pclass is not None: |
|
218 |
fetchattrs += pclass.fetch_attrs |
|
219 |
if mainattr is None: |
|
220 |
mainattr = fetchattrs[0] |
|
221 |
@classmethod |
|
222 |
def fetch_order(cls, attr, var): |
|
223 |
if attr == mainattr: |
|
224 |
return '%s %s' % (var, order) |
|
225 |
return None |
|
226 |
return fetchattrs, fetch_order |