author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 13 Aug 2009 13:07:21 +0200 | |
changeset 2829 | 054a8805da52 |
parent 2819 | b864288fd316 |
child 3369 | 7b88d12b4ee2 |
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 |
|
0 | 207 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
208 |
# configuration and we need the actual reference to avoid infinite loops |
|
209 |
# in mro |
|
210 |
ANYENTITY = AnyEntity |
|
211 |
||
212 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
213 |
if pclass is ANYENTITY: |
|
214 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
215 |
if pclass is not None: |
|
216 |
fetchattrs += pclass.fetch_attrs |
|
217 |
if mainattr is None: |
|
218 |
mainattr = fetchattrs[0] |
|
219 |
@classmethod |
|
220 |
def fetch_order(cls, attr, var): |
|
221 |
if attr == mainattr: |
|
222 |
return '%s %s' % (var, order) |
|
223 |
return None |
|
224 |
return fetchattrs, fetch_order |