194 lines
7.1 KiB
Python
194 lines
7.1 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2013 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from quixote import get_publisher
|
|
from quixote.html import htmltext
|
|
|
|
from wcs.comment_templates import CommentTemplate
|
|
from wcs.workflows import (
|
|
AttachmentEvolutionPart,
|
|
EvolutionPart,
|
|
WorkflowStatusItem,
|
|
register_item_class,
|
|
template_on_formdata,
|
|
)
|
|
|
|
from ..qommon import _, ezt
|
|
from ..qommon.form import SingleSelectWidget, TextWidget, WidgetListOfRoles
|
|
from ..qommon.template import TemplateError
|
|
|
|
|
|
class JournalEvolutionPart(EvolutionPart):
|
|
content = None
|
|
to = None
|
|
|
|
def __init__(self, formdata, message, to):
|
|
if not message:
|
|
return
|
|
self.to = to
|
|
if '{{' in message or '{%' in message:
|
|
# django template
|
|
content = template_on_formdata(formdata, message)
|
|
if content and not content.startswith('<'):
|
|
# add <div> to mark the string as processed as HTML
|
|
content = '<div>%s</div>' % content
|
|
self.content = content
|
|
return
|
|
|
|
if message.startswith('<'):
|
|
# treat it as html, escape strings from ezt variables
|
|
self.content = template_on_formdata(formdata, message, ezt_format=ezt.FORMAT_HTML)
|
|
return
|
|
|
|
# treat is as text/plain
|
|
self.content = template_on_formdata(formdata, message)
|
|
|
|
def view(self):
|
|
if not self.content:
|
|
return ''
|
|
if self.content.startswith('<'):
|
|
return htmltext(self.content)
|
|
else:
|
|
# use empty lines to mark paragraphs
|
|
return (
|
|
htmltext('<p>')
|
|
+ htmltext('\n').join([(x or htmltext('</p><p>')) for x in self.content.splitlines()])
|
|
+ htmltext('</p>')
|
|
)
|
|
|
|
def get_json_export_dict(self, anonymise=False, include_files=True):
|
|
d = {
|
|
'type': 'workflow-comment',
|
|
'to': self.to,
|
|
}
|
|
if not anonymise:
|
|
d['content'] = self.content
|
|
return d
|
|
|
|
|
|
class RegisterCommenterWorkflowStatusItem(WorkflowStatusItem):
|
|
description = _('History Message')
|
|
key = 'register-comment'
|
|
category = 'interaction'
|
|
|
|
comment = None
|
|
comment_template = None
|
|
to = None
|
|
attachments = None
|
|
|
|
def add_parameters_widgets(self, form, parameters, prefix='', formdef=None, **kwargs):
|
|
super().add_parameters_widgets(form, parameters, prefix=prefix, formdef=formdef, **kwargs)
|
|
subject_body_attrs = {}
|
|
if 'comment' in parameters:
|
|
if CommentTemplate.count():
|
|
subject_body_attrs = {
|
|
'data-dynamic-display-value': '',
|
|
'data-dynamic-display-child-of': '%scomment_template' % prefix,
|
|
}
|
|
if 'comment' in parameters:
|
|
form.add(
|
|
TextWidget,
|
|
'%scomment' % prefix,
|
|
title=_('Message'),
|
|
value=self.comment,
|
|
cols=80,
|
|
rows=10,
|
|
attrs=subject_body_attrs,
|
|
)
|
|
if 'comment_template' in parameters and CommentTemplate.count():
|
|
form.add(
|
|
SingleSelectWidget,
|
|
'%scomment_template' % prefix,
|
|
title=_('Comment Template'),
|
|
value=self.comment_template,
|
|
options=[(None, '', '')] + CommentTemplate.get_as_options_list(),
|
|
attrs={'data-dynamic-display-parent': 'true'},
|
|
)
|
|
if 'to' in parameters:
|
|
form.add(
|
|
WidgetListOfRoles,
|
|
'%sto' % prefix,
|
|
title=_('To'),
|
|
value=self.to or [],
|
|
add_element_label=self.get_add_role_label(),
|
|
first_element_empty_label=_('Everybody'),
|
|
roles=self.get_list_of_roles(include_logged_in_users=False),
|
|
)
|
|
|
|
def get_parameters(self):
|
|
return ('comment_template', 'comment', 'to', 'attachments', 'condition')
|
|
|
|
def attach_uploads_to_formdata(self, formdata, uploads, to):
|
|
if not formdata.evolution[-1].parts:
|
|
formdata.evolution[-1].parts = []
|
|
for upload in uploads:
|
|
try:
|
|
# useless but required to restore upload.fp from serialized state,
|
|
# needed by AttachmentEvolutionPart.from_upload()
|
|
upload.get_file_pointer()
|
|
formdata.evolution[-1].add_part(AttachmentEvolutionPart.from_upload(upload, to=to))
|
|
except Exception as e:
|
|
get_publisher().record_error(exception=e, context='[comment/attachments]', notify=True)
|
|
continue
|
|
|
|
def perform(self, formdata):
|
|
if not formdata.evolution:
|
|
return
|
|
|
|
if self.comment_template:
|
|
comment_template = CommentTemplate.get_by_slug(self.comment_template)
|
|
if comment_template:
|
|
comment = comment_template.comment
|
|
extra_attachments = comment_template.attachments
|
|
else:
|
|
message = _(
|
|
'reference to invalid comment template %(comment_template)s in status %(status)s'
|
|
) % {
|
|
'status': self.parent.name,
|
|
'comment_template': self.comment_template,
|
|
}
|
|
get_publisher().record_error(message, formdata=formdata, status_item=self)
|
|
return
|
|
else:
|
|
comment = self.comment
|
|
extra_attachments = None
|
|
|
|
# process attachments first, they might be used in the comment
|
|
# (with substitution vars)
|
|
if self.attachments or extra_attachments:
|
|
uploads = self.convert_attachments_to_uploads(extra_attachments)
|
|
self.attach_uploads_to_formdata(formdata, uploads, self.to)
|
|
formdata.store() # store and invalidate cache, so references can be used in the comment message.
|
|
|
|
# the comment can use attachments done above
|
|
if comment:
|
|
try:
|
|
formdata.evolution[-1].add_part(
|
|
JournalEvolutionPart(formdata, get_publisher().translate(comment), self.to)
|
|
)
|
|
formdata.store()
|
|
except TemplateError as e:
|
|
get_publisher().record_error(
|
|
_('Error in template, comment could not be generated'), formdata=formdata, exception=e
|
|
)
|
|
|
|
def i18n_scan(self, base_location):
|
|
location = '%sitems/%s/' % (base_location, self.id)
|
|
yield location, None, self.comment
|
|
|
|
|
|
register_item_class(RegisterCommenterWorkflowStatusItem)
|