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