|
1 """Specific views for email addresses entities |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 """ |
|
7 __docformat__ = "restructuredtext en" |
|
8 |
|
9 from logilab.mtconverter import html_escape |
|
10 |
|
11 from cubicweb.common import Unauthorized |
|
12 from cubicweb.web.views import baseviews |
|
13 |
|
14 class EmailAddressPrimaryView(baseviews.PrimaryView): |
|
15 accepts = ('EmailAddress',) |
|
16 |
|
17 def cell_call(self, row, col, skipeids=None): |
|
18 self.skipeids = skipeids |
|
19 super(EmailAddressPrimaryView, self).cell_call(row, col) |
|
20 |
|
21 def render_entity_attributes(self, entity, siderelations): |
|
22 self.w(u'<h3>') |
|
23 entity.view('oneline', w=self.w) |
|
24 if not entity.canonical: |
|
25 canonemailaddr = entity.canonical_form() |
|
26 if canonemailaddr: |
|
27 self.w(u' (<i>%s</i>)' % canonemailaddr.view('oneline')) |
|
28 self.w(u'</h3>') |
|
29 elif entity.identical_to: |
|
30 self.w(u'</h3>') |
|
31 identicaladdr = [e.view('oneline') for e in entity.identical_to] |
|
32 self.field('identical_to', ', '.join(identicaladdr)) |
|
33 else: |
|
34 self.w(u'</h3>') |
|
35 try: |
|
36 persons = entity.reverse_primary_email |
|
37 except Unauthorized: |
|
38 persons = [] |
|
39 if persons: |
|
40 emailof = persons[0] |
|
41 self.field(display_name(self.req, 'primary_email', 'object'), emailof.view('oneline')) |
|
42 pemaileid = emailof.eid |
|
43 else: |
|
44 pemaileid = None |
|
45 try: |
|
46 emailof = 'use_email' in self.schema and entity.reverse_use_email or () |
|
47 emailof = [e for e in emailof if not e.eid == pemaileid] |
|
48 except Unauthorized: |
|
49 emailof = [] |
|
50 if emailof: |
|
51 emailofstr = ', '.join(e.view('oneline') for e in emailof) |
|
52 self.field(display_name(self.req, 'use_email', 'object'), emailofstr) |
|
53 |
|
54 def render_entity_relations(self, entity, siderelations): |
|
55 for i, email in enumerate(entity.related_emails(self.skipeids)): |
|
56 self.w(u'<div class="%s">' % (i%2 and 'even' or 'odd')) |
|
57 email.view('oneline', w=self.w, contexteid=entity.eid) |
|
58 self.w(u'</div>') |
|
59 |
|
60 |
|
61 class EmailAddressShortPrimaryView(EmailAddressPrimaryView): |
|
62 accepts = ('EmailAddress',) |
|
63 id = 'shortprimary' |
|
64 title = None # hidden view |
|
65 def render_entity_attributes(self, entity, siderelations): |
|
66 self.w(u'<h5>') |
|
67 entity.view('oneline', w=self.w) |
|
68 self.w(u'</h5>') |
|
69 |
|
70 |
|
71 class EmailAddressOneLineView(baseviews.OneLineView): |
|
72 accepts = ('EmailAddress',) |
|
73 |
|
74 def cell_call(self, row, col, **kwargs): |
|
75 entity = self.entity(row, col) |
|
76 if entity.reverse_primary_email: |
|
77 self.w(u'<b>') |
|
78 if entity.alias: |
|
79 self.w(u'%s <' % html_escape(entity.alias)) |
|
80 self.w('<a href="%s">%s</a>' % (html_escape(entity.absolute_url()), |
|
81 html_escape(entity.display_address()))) |
|
82 if entity.alias: |
|
83 self.w(u'>\n') |
|
84 if entity.reverse_primary_email: |
|
85 self.w(u'</b>') |
|
86 |
|
87 class EmailAddressMailToView(baseviews.OneLineView): |
|
88 """A one line view that builds a user clickable URL for an email with |
|
89 'mailto:'""" |
|
90 |
|
91 id = 'mailto' |
|
92 accepts = ('EmailAddress',) |
|
93 |
|
94 def cell_call(self, row, col, **kwargs): |
|
95 entity = self.entity(row, col) |
|
96 if entity.reverse_primary_email: |
|
97 self.w(u'<b>') |
|
98 if entity.alias: |
|
99 mailto = u'%s <%s>' % (entity.alias, entity.display_address()) |
|
100 elif entity.reverse_use_email: |
|
101 mailto = "mailto:%s <%s>" % \ |
|
102 (entity.reverse_use_email[0].dc_title(), |
|
103 entity.display_address()) |
|
104 else: |
|
105 mailto = "mailto:%s" % entity.display_address() |
|
106 self.w(u'<a href="%s">%s</a>' % (html_escape(mailto), |
|
107 html_escape(entity.display_address()))) |
|
108 |
|
109 if entity.alias: |
|
110 self.w(u'>\n') |
|
111 if entity.reverse_primary_email: |
|
112 self.w(u'</b>') |
|
113 |
|
114 |
|
115 class EmailAddressTextView(baseviews.TextView): |
|
116 accepts = ('EmailAddress',) |
|
117 |
|
118 def cell_call(self, row, col, **kwargs): |
|
119 self.w(self.entity(row, col).display_address()) |