author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 10:10:04 +0200 | |
branch | tls-sprint |
changeset 1491 | 742aff97dbf7 |
parent 1477 | b056a49c16dc |
child 1780 | 7549509ce0e6 |
permissions | -rw-r--r-- |
0 | 1 |
"""entity classes for optional library entities |
2 |
||
3 |
:organization: Logilab |
|
583
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from urlparse import urlsplit, urlunsplit |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
10 |
from datetime import datetime |
0 | 11 |
|
12 |
from logilab.common.decorators import cached |
|
13 |
||
1302
dd984d682ab0
don't fail on unregistered properties
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
14 |
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
|
15 |
from cubicweb.entity import _marker |
0 | 16 |
from cubicweb.entities import AnyEntity, fetch_config |
17 |
||
18 |
def mangle_email(address): |
|
19 |
try: |
|
20 |
name, host = address.split('@', 1) |
|
21 |
except ValueError: |
|
22 |
return address |
|
23 |
return '%s at %s' % (name, host.replace('.', ' dot ')) |
|
24 |
||
25 |
class EmailAddress(AnyEntity): |
|
26 |
id = 'EmailAddress' |
|
27 |
fetch_attrs, fetch_order = fetch_config(['address', 'alias', 'canonical']) |
|
28 |
||
29 |
def dc_title(self): |
|
30 |
if self.alias: |
|
31 |
return '%s <%s>' % (self.alias, self.display_address()) |
|
32 |
return self.display_address() |
|
1477 | 33 |
|
0 | 34 |
@property |
35 |
def email_of(self): |
|
36 |
return self.reverse_use_email and self.reverse_use_email[0] |
|
1477 | 37 |
|
0 | 38 |
@cached |
39 |
def canonical_form(self): |
|
40 |
if self.canonical: |
|
41 |
return self |
|
42 |
rql = 'EmailAddress X WHERE X identical_to Y, X canonical TRUE, Y eid %(y)s' |
|
43 |
cnrset = self.req.execute(rql, {'y': self.eid}, 'y') |
|
44 |
if cnrset: |
|
45 |
return cnrset.get_entity(0, 0) |
|
46 |
return None |
|
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 |
|
0 | 100 |
def dc_description(self): |
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): |
|
143 |
return date < self.timestamp |