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