|
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """entity classes for optional library entities""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 from warnings import warn |
|
22 from datetime import datetime |
|
23 |
|
24 from six.moves import range |
|
25 from six.moves.urllib.parse import urlsplit, urlunsplit |
|
26 |
|
27 from logilab.mtconverter import xml_escape |
|
28 |
|
29 from cubicweb import UnknownProperty |
|
30 from cubicweb.entity import _marker |
|
31 from cubicweb.entities import AnyEntity, fetch_config |
|
32 |
|
33 def mangle_email(address): |
|
34 try: |
|
35 name, host = address.split('@', 1) |
|
36 except ValueError: |
|
37 return address |
|
38 return '%s at %s' % (name, host.replace('.', ' dot ')) |
|
39 |
|
40 |
|
41 class EmailAddress(AnyEntity): |
|
42 __regid__ = 'EmailAddress' |
|
43 fetch_attrs, cw_fetch_order = fetch_config(['address', 'alias']) |
|
44 rest_attr = 'eid' |
|
45 |
|
46 def dc_title(self): |
|
47 if self.alias: |
|
48 return '%s <%s>' % (self.alias, self.display_address()) |
|
49 return self.display_address() |
|
50 |
|
51 @property |
|
52 def email_of(self): |
|
53 return self.reverse_use_email and self.reverse_use_email[0] or None |
|
54 |
|
55 @property |
|
56 def prefered(self): |
|
57 return self.prefered_form and self.prefered_form[0] or self |
|
58 |
|
59 def related_emails(self, skipeids=None): |
|
60 # XXX move to eemail |
|
61 # check email relations are in the schema first |
|
62 subjrels = self.e_schema.object_relations() |
|
63 if not ('sender' in subjrels and 'recipients' in subjrels): |
|
64 return |
|
65 rset = self._cw.execute('DISTINCT Any X, S, D ORDERBY D DESC ' |
|
66 'WHERE X sender Y or X recipients Y, ' |
|
67 'X subject S, X date D, Y eid %(y)s', |
|
68 {'y': self.eid}) |
|
69 if skipeids is None: |
|
70 skipeids = set() |
|
71 for i in range(len(rset)): |
|
72 eid = rset[i][0] |
|
73 if eid in skipeids: |
|
74 continue |
|
75 skipeids.add(eid) |
|
76 yield rset.get_entity(i, 0) |
|
77 |
|
78 def display_address(self): |
|
79 if self._cw.vreg.config['mangle-emails']: |
|
80 return mangle_email(self.address) |
|
81 return self.address |
|
82 |
|
83 def printable_value(self, attr, value=_marker, attrtype=None, |
|
84 format='text/html'): |
|
85 """overriden to return displayable address when necessary""" |
|
86 if attr == 'address': |
|
87 address = self.display_address() |
|
88 if format == 'text/html': |
|
89 address = xml_escape(address) |
|
90 return address |
|
91 return super(EmailAddress, self).printable_value(attr, value, attrtype, format) |
|
92 |
|
93 |
|
94 class Bookmark(AnyEntity): |
|
95 """customized class for Bookmark entities""" |
|
96 __regid__ = 'Bookmark' |
|
97 fetch_attrs, cw_fetch_order = fetch_config(['title', 'path']) |
|
98 |
|
99 def actual_url(self): |
|
100 url = self._cw.build_url(self.path) |
|
101 if self.title: |
|
102 urlparts = list(urlsplit(url)) |
|
103 if urlparts[3]: |
|
104 urlparts[3] += '&vtitle=%s' % self._cw.url_quote(self.title) |
|
105 else: |
|
106 urlparts[3] = 'vtitle=%s' % self._cw.url_quote(self.title) |
|
107 url = urlunsplit(urlparts) |
|
108 return url |
|
109 |
|
110 def action_url(self): |
|
111 return self.absolute_url() + '/follow' |
|
112 |
|
113 |
|
114 class CWProperty(AnyEntity): |
|
115 __regid__ = 'CWProperty' |
|
116 |
|
117 fetch_attrs, cw_fetch_order = fetch_config(['pkey', 'value']) |
|
118 rest_attr = 'pkey' |
|
119 |
|
120 def typed_value(self): |
|
121 return self._cw.vreg.typed_value(self.pkey, self.value) |
|
122 |
|
123 def dc_description(self, format='text/plain'): |
|
124 try: |
|
125 return self._cw._(self._cw.vreg.property_info(self.pkey)['help']) |
|
126 except UnknownProperty: |
|
127 return u'' |
|
128 |
|
129 |
|
130 class CWCache(AnyEntity): |
|
131 """Cache""" |
|
132 __regid__ = 'CWCache' |
|
133 fetch_attrs, cw_fetch_order = fetch_config(['name']) |
|
134 |
|
135 def __init__(self, *args, **kwargs): |
|
136 warn('[3.19] CWCache entity type is going away soon. ' |
|
137 'Other caching mechanisms can be used more reliably ' |
|
138 'to the same effect.', |
|
139 DeprecationWarning) |
|
140 super(CWCache, self).__init__(*args, **kwargs) |
|
141 |
|
142 def touch(self): |
|
143 self._cw.execute('SET X timestamp %(t)s WHERE X eid %(x)s', |
|
144 {'t': datetime.now(), 'x': self.eid}) |
|
145 |
|
146 def valid(self, date): |
|
147 if date: |
|
148 return date > self.timestamp |
|
149 return False |