author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 25 Oct 2010 15:31:29 +0200 | |
changeset 6622 | 27402fe6a94a |
parent 6039 | 6e84db1b3e44 |
child 7143 | 0517e3bf0b84 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""base application's entities class implementation: `AnyEntity`""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
from warnings import warn |
|
23 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2459
diff
changeset
|
24 |
from logilab.common.deprecation import deprecated |
0 | 25 |
from logilab.common.decorators import cached |
26 |
||
27 |
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
|
28 |
from cubicweb.entity import Entity |
0 | 29 |
|
30 |
||
31 |
class AnyEntity(Entity): |
|
32 |
"""an entity instance has e_schema automagically set on the class and |
|
33 |
instances have access to their issuing cursor |
|
34 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
35 |
__regid__ = 'Any' |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
36 |
__implements__ = () |
0 | 37 |
|
6039
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
38 |
@classmethod |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
39 |
def cw_create_url(cls, req, **kwargs): |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
40 |
""" return the url of the entity creation form for this entity type""" |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
41 |
return req.build_url('add/%s' % cls.__regid__, **kwargs) |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
42 |
|
0 | 43 |
# meta data api ########################################################### |
44 |
||
45 |
def dc_title(self): |
|
46 |
"""return a suitable *unicode* title for this entity""" |
|
47 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
48 |
if rschema.meta: |
|
49 |
continue |
|
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
50 |
value = self.cw_attr_value(rschema.type) |
0 | 51 |
if value: |
52 |
# make the value printable (dates, floats, bytes, etc.) |
|
53 |
return self.printable_value(rschema.type, value, attrschema.type, |
|
54 |
format='text/plain') |
|
55 |
return u'%s #%s' % (self.dc_type(), self.eid) |
|
56 |
||
57 |
def dc_long_title(self): |
|
58 |
"""return a more detailled title for this entity""" |
|
59 |
return self.dc_title() |
|
1493 | 60 |
|
0 | 61 |
def dc_description(self, format='text/plain'): |
62 |
"""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
|
63 |
if 'description' in self.e_schema.subjrels: |
0 | 64 |
return self.printable_value('description', format=format) |
65 |
return u'' |
|
66 |
||
67 |
def dc_authors(self): |
|
68 |
"""return a suitable description for the author(s) of the entity""" |
|
69 |
try: |
|
70 |
return ', '.join(u.name() for u in self.owned_by) |
|
71 |
except Unauthorized: |
|
72 |
return u'' |
|
73 |
||
74 |
def dc_creator(self): |
|
75 |
"""return a suitable description for the creator of the entity""" |
|
76 |
if self.creator: |
|
77 |
return self.creator.name() |
|
78 |
return u'' |
|
79 |
||
80 |
def dc_date(self, date_format=None):# XXX default to ISO 8601 ? |
|
81 |
"""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
|
82 |
return self._cw.format_date(self.modification_date, date_format=date_format) |
0 | 83 |
|
84 |
def dc_type(self, form=''): |
|
85 |
"""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
|
86 |
return self.e_schema.display_name(self._cw, form) |
0 | 87 |
|
88 |
def dc_language(self): |
|
89 |
"""return language used by this entity (translated)""" |
|
90 |
# check if entities has internationalizable attributes |
|
91 |
# XXX one is enough or check if all String attributes are internationalizable? |
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
return self._cw._(self._cw.vreg.property_value('ui.language')) |
1493 | 96 |
|
0 | 97 |
@property |
98 |
def creator(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
99 |
"""return the CWUser entity which has created this entity, or None if |
0 | 100 |
unknown or if the curent user doesn't has access to this euser |
101 |
""" |
|
102 |
try: |
|
103 |
return self.created_by[0] |
|
104 |
except (Unauthorized, IndexError): |
|
105 |
return None |
|
106 |
||
107 |
# abstractions making the whole things (well, some at least) working ###### |
|
1493 | 108 |
|
0 | 109 |
def sortvalue(self, rtype=None): |
110 |
"""return a value which can be used to sort this entity or given |
|
111 |
entity's attribute |
|
112 |
""" |
|
113 |
if rtype is None: |
|
114 |
return self.dc_title().lower() |
|
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
115 |
value = self.cw_attr_value(rtype) |
0 | 116 |
# do not restrict to `unicode` because Bytes will return a `str` value |
117 |
if isinstance(value, basestring): |
|
118 |
return self.printable_value(rtype, format='text/plain').lower() |
|
119 |
return value |
|
120 |
||
1493 | 121 |
# edition helper functions ################################################ |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
122 |
|
3348
97dca770c028
this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
123 |
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
|
124 |
"""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
|
125 |
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
|
126 |
|
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
127 |
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
|
128 |
if `remove` is True (by default). |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
129 |
""" |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
130 |
try: |
3348
97dca770c028
this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
131 |
return self.__linkto[(rtype, role)] |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
132 |
except AttributeError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
133 |
self.__linkto = {} |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
134 |
except KeyError: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
135 |
pass |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
136 |
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
|
137 |
linkedto = [] |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
if rtype == ltrtype and role == ltrole: |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
141 |
# 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
|
142 |
# hidden input |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
143 |
if remove: |
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
144 |
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
|
145 |
self._cw.form['__linkto'] = linktos |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
146 |
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
|
147 |
self.__linkto[(rtype, role)] = linkedto |
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
148 |
return linkedto |
1493 | 149 |
|
1086
c8ff5c7fd0ae
move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
150 |
# server side helpers ##################################################### |
1493 | 151 |
|
0 | 152 |
# XXX: store a reference to the AnyEntity class since it is hijacked in goa |
153 |
# configuration and we need the actual reference to avoid infinite loops |
|
154 |
# in mro |
|
155 |
ANYENTITY = AnyEntity |
|
156 |
||
157 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
|
158 |
if pclass is ANYENTITY: |
|
159 |
pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
|
160 |
if pclass is not None: |
|
161 |
fetchattrs += pclass.fetch_attrs |
|
162 |
if mainattr is None: |
|
163 |
mainattr = fetchattrs[0] |
|
164 |
@classmethod |
|
165 |
def fetch_order(cls, attr, var): |
|
166 |
if attr == mainattr: |
|
167 |
return '%s %s' % (var, order) |
|
168 |
return None |
|
169 |
return fetchattrs, fetch_order |