add possibility to send versions by email (#4326)

This commit is contained in:
Frédéric Péters 2014-10-10 15:29:21 +02:00
parent 9ebef0ab14
commit 66471eccc9
3 changed files with 130 additions and 2 deletions

View File

@ -43,6 +43,13 @@
permission="zope2.View"
/>
<browser:page
name="send_by_email"
for="collective.dms.basecontent.dmsfile.IDmsFile"
class=".send_by_email.SendMailView"
permission="zope2.View"
/>
<!-- Custom comments viewlet for IBaseTask -->
<browser:viewlet
name="plone.comments"

View File

@ -0,0 +1,106 @@
# -*- encoding: utf-8 -*-
import logging
from datetime import datetime
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from zope.interface import Interface
from zope import schema
from zope.component import createObject, queryUtility
from z3c.form import form, button
from z3c.form.field import Fields
from z3c.form.interfaces import HIDDEN_MODE
from Acquisition import aq_inner, aq_parent
from plone.z3cform.layout import FormWrapper
from Products.Five.browser import BrowserView
from Products.CMFCore.utils import getToolByName
from plone import api
from .. import _
class IMail(Interface):
recipients = schema.Text(title=_(u"Recipients"), required=True,
description=_(u"Email addresses of the recipients, one per line"))
subject = schema.TextLine(title=_(u"Subject"), required=True)
comment = schema.Text(
title=_(u"Comment"),
description=_(u"You can enter a note."),
required=False)
class MailForm(form.AddForm):
fields = Fields(IMail)
next_url = None
def updateActions(self):
super(MailForm, self).updateActions()
self.actions["save"].addClass("context")
self.actions["cancel"].addClass("standalone")
def updateWidgets(self):
super(MailForm, self).updateWidgets()
@button.buttonAndHandler(_(u'Send'), name='save')
def handleAdd(self, action):
data, errors = self.extractData()
if errors:
self.status = self.formErrorsMessage
return
self._finishedAdd = True
subject = data['subject']
recipients = data['recipients'].splitlines()
comment = data.get('comment')
msg = MIMEMultipart()
msg['Subject'] = subject
msg['To'] = ', '.join(recipients)
msg['From'] = api.user.get_current().email or \
api.portal.get().getProperty('email_from_address') or \
'admin@localhost'
if comment:
msg.attach(MIMEText(comment, _charset='utf-8'))
ctype = self.context.file.contentType or 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(self.context.file.data)
encoders.encode_base64(attachment)
if self.context.file.filename:
attachment.add_header('Content-Disposition', 'attachment',
filename=self.context.file.filename)
msg.attach(attachment)
try:
self.context.MailHost.send(msg.as_string())
except Exception as e:
# do not abort transaction in case of email error
log = logging.getLogger('pfwbged.policy')
log.exception(e)
self.context.plone_utils.addPortalMessage(_('Error sending email'))
self.next_url = aq_parent(self.context).absolute_url()
@button.buttonAndHandler(_(u'Cancel'), name='cancel')
def handleCancel(self, action):
self._finishedAdd = True
self.next_url = aq_parent(self.context).absolute_url()
def nextURL(self):
return self.next_url
class SendMailView(FormWrapper, BrowserView):
form = MailForm
def __init__(self, context, request):
BrowserView.__init__(self, context, request)
FormWrapper.__init__(self, context, request)

View File

@ -48,6 +48,7 @@ dmsfile_wfactions_mapping = {'ask_opinion': _(u"Ask opinion about version ${vers
'refuse': _(u"Refuse version ${version}"),
'finish': _(u"Finish version ${version}"),
'finish_without_validation': _(u"Validate and finish version ${version}"),
'send_by_email': _(u"Send version ${version} by email"),
}
@ -135,6 +136,18 @@ class CustomMenu(menu.WorkflowMenu):
workflowActions.insert(idx, to_process_without_comment_action)
to_process_action['title'] = _(u'To process (with comment)')
if IDmsFile.providedBy(context):
workflowActions.append(
{'available': True,
'visible': True,
'allowed': True,
'link_target': None,
'id': 'send_by_email',
'category': 'workflow',
'title': 'Send by email',
'url': context.absolute_url() + '/@@send_by_email',
'icon': None})
for action in workflowActions:
if action['category'] != 'workflow':
continue
@ -152,7 +165,7 @@ class CustomMenu(menu.WorkflowMenu):
description = ''
if action['id'] in ('submit', 'ask_opinion', 'attribute',
'to_process', 'refuse'):
'to_process', 'refuse', 'send_by_email'):
cssClass += " overlay-form-reload"
transition = action.get('transition', None)
@ -349,7 +362,9 @@ class CustomMenu(menu.WorkflowMenu):
_editActions = ttool.listActionInfos(object=context,
category='object_buttons',
max=-1)
editActions = [action for action in _editActions if action['id'] != 'create_outgoing_mail' or (is_linked_to_an_incoming_mail(context) and not outgoingmail_created(context))]
editActions = [action for action in _editActions if
action['id'] not in ('create_outgoing_mail', 'send_by_email') or (
is_linked_to_an_incoming_mail(context) and not outgoingmail_created(context))]
else:
editActions = []