|
1 # copyright 2003-2010 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 """Specific views for email addresses entities""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 |
|
22 from logilab.mtconverter import xml_escape |
|
23 |
|
24 from cubicweb.schema import display_name |
|
25 from cubicweb.predicates import is_instance |
|
26 from cubicweb import Unauthorized |
|
27 from cubicweb.web.views import uicfg, baseviews, primary, ibreadcrumbs |
|
28 |
|
29 _pvs = uicfg.primaryview_section |
|
30 _pvs.tag_subject_of(('*', 'use_email', '*'), 'attributes') |
|
31 _pvs.tag_subject_of(('*', 'primary_email', '*'), 'hidden') |
|
32 |
|
33 class EmailAddressPrimaryView(primary.PrimaryView): |
|
34 __select__ = is_instance('EmailAddress') |
|
35 |
|
36 def cell_call(self, row, col, skipeids=None): |
|
37 self.skipeids = skipeids |
|
38 super(EmailAddressPrimaryView, self).cell_call(row, col) |
|
39 |
|
40 def render_entity_attributes(self, entity): |
|
41 self.w(u'<h3>') |
|
42 entity.view('oneline', w=self.w) |
|
43 if entity.prefered: |
|
44 self.w(u' (<i>%s</i>)' % entity.prefered.view('oneline')) |
|
45 self.w(u'</h3>') |
|
46 try: |
|
47 persons = entity.reverse_primary_email |
|
48 except Unauthorized: |
|
49 persons = [] |
|
50 if persons: |
|
51 emailof = persons[0] |
|
52 self.field(display_name(self._cw, 'primary_email', 'object'), emailof.view('oneline')) |
|
53 pemaileid = emailof.eid |
|
54 else: |
|
55 pemaileid = None |
|
56 try: |
|
57 emailof = 'use_email' in self._cw.vreg.schema and entity.reverse_use_email or () |
|
58 emailof = [e for e in emailof if not e.eid == pemaileid] |
|
59 except Unauthorized: |
|
60 emailof = [] |
|
61 if emailof: |
|
62 emailofstr = ', '.join(e.view('oneline') for e in emailof) |
|
63 self.field(display_name(self._cw, 'use_email', 'object'), emailofstr) |
|
64 |
|
65 def render_entity_relations(self, entity): |
|
66 for i, email in enumerate(entity.related_emails(self.skipeids)): |
|
67 self.w(u'<div class="%s">' % (i%2 and 'even' or 'odd')) |
|
68 email.view('oneline', w=self.w, contexteid=entity.eid) |
|
69 self.w(u'</div>') |
|
70 |
|
71 |
|
72 class EmailAddressShortPrimaryView(EmailAddressPrimaryView): |
|
73 __select__ = is_instance('EmailAddress') |
|
74 __regid__ = 'shortprimary' |
|
75 title = None # hidden view |
|
76 |
|
77 def render_entity_attributes(self, entity): |
|
78 self.w(u'<h5>') |
|
79 entity.view('oneline', w=self.w) |
|
80 self.w(u'</h5>') |
|
81 |
|
82 |
|
83 class EmailAddressOneLineView(baseviews.OneLineView): |
|
84 __select__ = is_instance('EmailAddress') |
|
85 |
|
86 def entity_call(self, entity, **kwargs): |
|
87 if entity.reverse_primary_email: |
|
88 self.w(u'<b>') |
|
89 if entity.alias: |
|
90 self.w(u'%s <' % xml_escape(entity.alias)) |
|
91 self.w('<a href="%s">%s</a>' % (xml_escape(entity.absolute_url()), |
|
92 xml_escape(entity.display_address()))) |
|
93 if entity.alias: |
|
94 self.w(u'>\n') |
|
95 if entity.reverse_primary_email: |
|
96 self.w(u'</b>') |
|
97 |
|
98 |
|
99 class EmailAddressMailToView(baseviews.OneLineView): |
|
100 """A one line view that builds a user clickable URL for an email with |
|
101 'mailto:'""" |
|
102 |
|
103 __regid__ = 'mailto' |
|
104 __select__ = is_instance('EmailAddress') |
|
105 |
|
106 def entity_call(self, entity, **kwargs): |
|
107 if entity.reverse_primary_email: |
|
108 self.w(u'<b>') |
|
109 if entity.alias: |
|
110 alias = entity.alias |
|
111 elif entity.reverse_use_email: |
|
112 alias = entity.reverse_use_email[0].dc_title() |
|
113 else: |
|
114 alias = None |
|
115 if alias: |
|
116 mailto = "mailto:%s <%s>" % (alias, entity.display_address()) |
|
117 else: |
|
118 mailto = "mailto:%s" % entity.display_address() |
|
119 self.w(u'<a href="%s">%s</a>' % (xml_escape(mailto), |
|
120 xml_escape(entity.display_address()))) |
|
121 if entity.reverse_primary_email: |
|
122 self.w(u'</b>') |
|
123 |
|
124 |
|
125 class EmailAddressInContextView(baseviews.InContextView): |
|
126 __select__ = is_instance('EmailAddress') |
|
127 |
|
128 def cell_call(self, row, col, **kwargs): |
|
129 if self._cw.vreg.config['mangle-emails']: |
|
130 self.wview('oneline', self.cw_rset, row=row, col=col, **kwargs) |
|
131 else: |
|
132 self.wview('mailto', self.cw_rset, row=row, col=col, **kwargs) |
|
133 |
|
134 |
|
135 class EmailAddressTextView(baseviews.TextView): |
|
136 __select__ = is_instance('EmailAddress') |
|
137 |
|
138 def cell_call(self, row, col, **kwargs): |
|
139 self.w(self.cw_rset.get_entity(row, col).display_address()) |
|
140 |
|
141 |
|
142 class EmailAddressIBreadCrumbsAdapter(ibreadcrumbs.IBreadCrumbsAdapter): |
|
143 __select__ = is_instance('EmailAddress') |
|
144 |
|
145 def parent_entity(self): |
|
146 return self.entity.email_of |