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