email.py 5.66 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
"""Specific views for email related entities

:organization: Logilab
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
4
:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
5
6
7
8
9
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""

from logilab.mtconverter import html_escape

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
10
from cubicweb.selectors import implements
Nicolas Chauvat's avatar
Nicolas Chauvat committed
11
12
from cubicweb.common.uilib import soup2xhtml
from cubicweb.common.mixins import TreeViewMixIn
13
from cubicweb.web import uicfg, formwidgets
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
14
from cubicweb.web.views import baseviews, primary
Nicolas Chauvat's avatar
Nicolas Chauvat committed
15

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
16
for rtype in ('sender', 'recipients', 'cc', 'parts'):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
17
    uicfg.primaryview_section.tag_subject_of(('Email', rtype, '*'), 'hidden')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
18

19
20
uicfg.autoform_field_kwargs.tag_attribute(('Email', 'subject'),
                                          {'widget': formwidgets.TextInput})
21

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
22
23
24
25
26
27
uicfg.actionbox_appearsin_addmenu.tag_subject_of(('Email', 'attachment', '*'),
                                                 True)

uicfg.actionbox_appearsin_addmenu.tag_object_of(('EmailThread', 'forked_from', 'EmailThread'),
                                                True)

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
28
29
class EmailPrimaryView(primary.PrimaryView):
    __select__ = implements('Email')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
30

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
31
    def render_entity_attributes(self, entity):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
32
33
34
35
        self.field('from', entity.sender[0].view('oneline'))
        self.field('to', ', '.join(ea.view('oneline') for ea in entity.recipients))
        if entity.cc:
            self.field('cc', ', '.join(ea.view('oneline') for ea in entity.cc))
36
        self.field('subject', html_escape(entity.subject))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
37
38
39
40
41
42
43
44
45
46
47
48
49
        self.field('date', self.format_date(entity.date, time=True))
        for part in entity.parts_in_order():
            content, mime = part.content, part.content_format
            if mime == 'text/html':
                content = soup2xhtml(content, self.req.encoding)
            elif mime != 'text/xhtml':
                content = html_escape(content)
                if mime == 'text/plain':
                    content = content.replace('\n', '<br/>').replace(' ', '&nbsp;')
            # XXX some headers to skip if html ?
            self.w(content)
            self.w(u'<br class="partseparator"/>')

50
51
52
    def render_entity_title(self, entity):
        self.w(u'<h1><span class="etype">%s</span> %s</h1>'
               % (entity.dc_type().capitalize(), html_escape(entity.dc_title())))
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
53
54


Nicolas Chauvat's avatar
Nicolas Chauvat committed
55
56
57
class EmailHeadersView(baseviews.EntityView):
    """display email's headers"""
    id = 'headers'
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
58
    __select__ = implements('Email')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
59
60
61
    title = _('headers')
    templatable = False
    content_type = 'text/plain'
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
62

Nicolas Chauvat's avatar
Nicolas Chauvat committed
63
64
65
66
    def cell_call(self, row, col):
        entity = self.entity(row, col)
        self.w(entity.headers)

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
67
68

class EmailOneLineView(baseviews.OneLineView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
69
70
71
    """short view usable in the context of the email sender/recipient (in which
    case the caller should specify its context eid) or outside any context
    """
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
72
    __select__ = implements('Email')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
73
    title = _('oneline')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
74

Nicolas Chauvat's avatar
Nicolas Chauvat committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    def cell_call(self, row, col, contexteid=None):
        entity = self.entity(row, col)
        sender = entity.sender[0]
        self.w(u'<div class="email">')
        self.w(u'<i>%s&nbsp;%s</i> '
               % (self.req._('email_date'), self.format_date(entity.date, time=True)))
        if contexteid != sender.eid:
            self.w(u'<b>%s</b>&nbsp;%s '
                   % (self.req._('email_from'), sender.view('oneline')))
        if contexteid not in (r.eid for r in entity.recipients):
            recipients = ', '.join(r.view('oneline') for r in entity.recipients)
            self.w(u'<b>%s</b>&nbsp;%s'
                   % (self.req._('email_to'), recipients))
        self.w(u'<br/>\n<a href="%s">%s</a>'
               % (html_escape(entity.absolute_url()), html_escape(entity.subject)))
        self.w(u'</div>')

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
92

Nicolas Chauvat's avatar
Nicolas Chauvat committed
93
94
95
class EmailOutOfContextView(EmailOneLineView):
    """short view outside the context of the email"""
    id = 'outofcontext'
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
96
    title = _('out of context')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
97
98
99
100
101
102
103
104

class EmailInContextView(EmailOneLineView):
    """short view inside the context of the email"""
    id = 'incontext'


class EmailPartOutOfContextView(baseviews.OutOfContextView):
    """out of context an email part is redirecting to related email view"""
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
105
    __select__ = implements('EmailPart')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
106
107
108
    def cell_call(self, row, col):
        entity = self.entity(row, col)
        entity.reverse_parts[0].view('outofcontext', w=self.w)
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
109
110


Nicolas Chauvat's avatar
Nicolas Chauvat committed
111
112
class EmailThreadView(TreeViewMixIn, baseviews.ListView):
    """display email's headers"""
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
113
    __select__ = implements('Email')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
114
115
116
    title = _('thread view')
    item_vid = 'outofcontext'

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
117

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
118
class EmailThreadPrimaryView(primary.PrimaryView):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
119
120
    __select__ = implements('EmailThread')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    def cell_call(self, row, col):
        entity = self.complete_entity(row, col)
        self.w(u'<h1>%s</h1>' % html_escape(entity.title))
        # get top level emails in this thread (ie message which are not a reply
        # of a message in this thread)
        #
        # Warn: adding Y in_thread E changes the meaning of the query since it joins
        # with messages which are not a direct reply (eg if A reply_to B, B reply_to C
        # B is also retreived since it's not a reply of C
        #
        # XXX  union with
        #   DISTINCT Any X,D ORDERBY D WHERE X date D, X in_thread E, X reply_to Y,
        #   NOT Y in_thread E, E eid %(x)s'
        # to get message which are a reply of a message in another thread ?
        # we may get duplicates in this case
        rset =  self.req.execute('DISTINCT Any X,D ORDERBY D '
                                 'WHERE X date D, X in_thread E, '
                                 'NOT X reply_to Y, E eid %(x)s',
                                 {'x': entity.eid}, 'x')
        if rset:
            self.w(u'<ul>')
            self.w(u'\n'.join(email.view('tree') for email in rset.entities()))
            self.w(u'</ul>')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
144
145