author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 29 Jan 2010 18:02:50 +0100 | |
branch | stable |
changeset 4410 | fdf0ffbab313 |
parent 4212 | ab6573088b4a |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
0 | 1 |
"""entity classes for optional library entities |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3217
diff
changeset
|
4 |
:copyright: 2001-2010 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): |
|
27 |
id = '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' |
|
54 |
rset = self.req.execute(rql, {'y': self.eid}, 'y') |
|
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): |
|
65 |
if self.vreg.config['mangle-emails']: |
|
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 |
||
85 |
from logilab.common.deprecation import class_renamed |
|
86 |
Emailaddress = class_renamed('Emailaddress', EmailAddress) |
|
87 |
Emailaddress.id = 'Emailaddress' |
|
88 |
||
89 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
90 |
class CWProperty(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
91 |
id = 'CWProperty' |
0 | 92 |
|
93 |
fetch_attrs, fetch_order = fetch_config(['pkey', 'value']) |
|
94 |
rest_attr = 'pkey' |
|
95 |
||
96 |
def typed_value(self): |
|
97 |
return self.vreg.typed_value(self.pkey, self.value) |
|
1477 | 98 |
|
1780
7549509ce0e6
dc_description should take a format argument
sylvain.thenault@logilab.fr
parents:
1477
diff
changeset
|
99 |
def dc_description(self, format='text/plain'): |
1302
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
100 |
try: |
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
101 |
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
|
102 |
except UnknownProperty: |
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
103 |
return u'' |
0 | 104 |
|
105 |
def after_deletion_path(self): |
|
106 |
"""return (path, parameters) which should be used as redirect |
|
107 |
information when this entity is being deleted |
|
108 |
""" |
|
109 |
return 'view', {} |
|
1477 | 110 |
|
0 | 111 |
|
112 |
class Bookmark(AnyEntity): |
|
113 |
"""customized class for Bookmark entities""" |
|
114 |
id = 'Bookmark' |
|
115 |
fetch_attrs, fetch_order = fetch_config(['title', 'path']) |
|
116 |
||
117 |
def actual_url(self): |
|
118 |
url = self.req.build_url(self.path) |
|
119 |
if self.title: |
|
120 |
urlparts = list(urlsplit(url)) |
|
121 |
if urlparts[3]: |
|
122 |
urlparts[3] += '&vtitle=%s' % self.req.url_quote(self.title) |
|
123 |
else: |
|
124 |
urlparts[3] = 'vtitle=%s' % self.req.url_quote(self.title) |
|
125 |
url = urlunsplit(urlparts) |
|
126 |
return url |
|
127 |
||
128 |
def action_url(self): |
|
129 |
return self.absolute_url() + '/follow' |
|
130 |
||
131 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
132 |
class CWCache(AnyEntity): |
0 | 133 |
"""Cache""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1302
diff
changeset
|
134 |
id = 'CWCache' |
0 | 135 |
fetch_attrs, fetch_order = fetch_config(['name']) |
136 |
||
137 |
def touch(self): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
138 |
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
|
139 |
{'t': datetime.now(), 'x': self.eid}, 'x') |
0 | 140 |
|
141 |
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
|
142 |
if date: |
2098
13aab3775af7
[CWCache] fix CWCache validity test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2060
diff
changeset
|
143 |
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
|
144 |
return False |
10d9fbdbed35
[entities][CWCache] handle classic datetime vs None comparison bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
145 |