author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 04 May 2010 11:09:18 +0200 | |
branch | stable |
changeset 5459 | 6e561796804c |
parent 5426 | 0d4853a6e5ee |
child 5556 | 9ab2b4c74baf |
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/>. |
0 | 18 |
"""entity classes for optional library entities |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
from urlparse import urlsplit, urlunsplit |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
24 |
from datetime import datetime |
0 | 25 |
|
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
26 |
from logilab.common.deprecation import deprecated |
0 | 27 |
|
1302
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
28 |
from cubicweb import UnknownProperty |
713
5adb6d8e5fa7
update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
583
diff
changeset
|
29 |
from cubicweb.entity import _marker |
0 | 30 |
from cubicweb.entities import AnyEntity, fetch_config |
31 |
||
32 |
def mangle_email(address): |
|
33 |
try: |
|
34 |
name, host = address.split('@', 1) |
|
35 |
except ValueError: |
|
36 |
return address |
|
37 |
return '%s at %s' % (name, host.replace('.', ' dot ')) |
|
38 |
||
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
39 |
|
0 | 40 |
class EmailAddress(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
41 |
__regid__ = 'EmailAddress' |
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
42 |
fetch_attrs, fetch_order = fetch_config(['address', 'alias']) |
0 | 43 |
|
44 |
def dc_title(self): |
|
45 |
if self.alias: |
|
46 |
return '%s <%s>' % (self.alias, self.display_address()) |
|
47 |
return self.display_address() |
|
1477 | 48 |
|
0 | 49 |
@property |
50 |
def email_of(self): |
|
51 |
return self.reverse_use_email and self.reverse_use_email[0] |
|
1477 | 52 |
|
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
53 |
@property |
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
54 |
def prefered(self): |
3207
2516324401dd
return self, not None, when no prefered form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3204
diff
changeset
|
55 |
return self.prefered_form and self.prefered_form[0] or self |
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
56 |
|
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
57 |
@deprecated('use .prefered') |
0 | 58 |
def canonical_form(self): |
3204
0b766b8a13e1
#370578: change EmailAddress identical_to/canonical to prefered_form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
59 |
return self.prefered_form and self.prefered_form[0] or self |
0 | 60 |
|
61 |
def related_emails(self, skipeids=None): |
|
62 |
# XXX move to eemail |
|
63 |
# check email relations are in the schema first |
|
64 |
subjrels = self.e_schema.object_relations() |
|
65 |
if not ('sender' in subjrels and 'recipients' in subjrels): |
|
66 |
return |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
67 |
rset = self._cw.execute('DISTINCT Any X, S, D ORDERBY D DESC ' |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
68 |
'WHERE X sender Y or X recipients Y, ' |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
69 |
'X subject S, X date D, Y eid %(y)s', |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
70 |
{'y': self.eid}) |
0 | 71 |
if skipeids is None: |
72 |
skipeids = set() |
|
73 |
for i in xrange(len(rset)): |
|
74 |
eid = rset[i][0] |
|
75 |
if eid in skipeids: |
|
76 |
continue |
|
77 |
skipeids.add(eid) |
|
78 |
yield rset.get_entity(i, 0) |
|
79 |
||
80 |
def display_address(self): |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
81 |
if self._cw.vreg.config['mangle-emails']: |
0 | 82 |
return mangle_email(self.address) |
83 |
return self.address |
|
84 |
||
85 |
def printable_value(self, attr, value=_marker, attrtype=None, |
|
86 |
format='text/html'): |
|
87 |
"""overriden to return displayable address when necessary""" |
|
88 |
if attr == 'address': |
|
89 |
return self.display_address() |
|
90 |
return super(EmailAddress, self).printable_value(attr, value, attrtype, format) |
|
91 |
||
92 |
def after_deletion_path(self): |
|
93 |
"""return (path, parameters) which should be used as redirect |
|
94 |
information when this entity is being deleted |
|
95 |
""" |
|
96 |
if self.email_of: |
|
97 |
return self.email_of.rest_path(), {} |
|
98 |
return super(EmailAddress, self).after_deletion_path() |
|
99 |
||
100 |
||
2828
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
101 |
class Bookmark(AnyEntity): |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
102 |
"""customized class for Bookmark entities""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
103 |
__regid__ = 'Bookmark' |
2828
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
104 |
fetch_attrs, fetch_order = fetch_config(['title', 'path']) |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
105 |
|
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
106 |
def actual_url(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
107 |
url = self._cw.build_url(self.path) |
2828
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
108 |
if self.title: |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
109 |
urlparts = list(urlsplit(url)) |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
110 |
if urlparts[3]: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
111 |
urlparts[3] += '&vtitle=%s' % self._cw.url_quote(self.title) |
2828
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
112 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
113 |
urlparts[3] = 'vtitle=%s' % self._cw.url_quote(self.title) |
2828
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
114 |
url = urlunsplit(urlparts) |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
115 |
return url |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
116 |
|
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
117 |
def action_url(self): |
306fe72bfd07
remove old bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2098
diff
changeset
|
118 |
return self.absolute_url() + '/follow' |
0 | 119 |
|
120 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
121 |
class CWProperty(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
122 |
__regid__ = 'CWProperty' |
0 | 123 |
|
124 |
fetch_attrs, fetch_order = fetch_config(['pkey', 'value']) |
|
125 |
rest_attr = 'pkey' |
|
126 |
||
127 |
def typed_value(self): |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
128 |
return self._cw.vreg.typed_value(self.pkey, self.value) |
1477 | 129 |
|
1780
7549509ce0e6
dc_description should take a format argument
sylvain.thenault@logilab.fr
parents:
1477
diff
changeset
|
130 |
def dc_description(self, format='text/plain'): |
1302
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
131 |
try: |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
132 |
return self._cw._(self._cw.vreg.property_info(self.pkey)['help']) |
1302
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
133 |
except UnknownProperty: |
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
134 |
return u'' |
0 | 135 |
|
136 |
def after_deletion_path(self): |
|
137 |
"""return (path, parameters) which should be used as redirect |
|
138 |
information when this entity is being deleted |
|
139 |
""" |
|
140 |
return 'view', {} |
|
1477 | 141 |
|
0 | 142 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
143 |
class CWCache(AnyEntity): |
0 | 144 |
"""Cache""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
145 |
__regid__ = 'CWCache' |
0 | 146 |
fetch_attrs, fetch_order = fetch_config(['name']) |
147 |
||
148 |
def touch(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
149 |
self._cw.execute('SET X timestamp %(t)s WHERE X eid %(x)s', |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
150 |
{'t': datetime.now(), 'x': self.eid}) |
0 | 151 |
|
152 |
def valid(self, date): |
|
2060
10d9fbdbed35
[entities][CWCache] handle classic datetime vs None comparison bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
153 |
if date: |
2098
13aab3775af7
[CWCache] fix CWCache validity test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2060
diff
changeset
|
154 |
return date > self.timestamp |
2060
10d9fbdbed35
[entities][CWCache] handle classic datetime vs None comparison bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
155 |
return False |
10d9fbdbed35
[entities][CWCache] handle classic datetime vs None comparison bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
156 |