From cf06d77932f23e878783fd9b18d5e1423dfaaf47 Mon Sep 17 00:00:00 2001 From: Dominic Hollis Date: Mon, 27 Jan 2025 18:35:52 +0100 Subject: [PATCH 1/5] Convert all files with CloudConvert * Use generic filenames when sending files to CC * Add a configurable notice setting to the upload files dialog (shown to end users) --- conversion/indico_conversion/conversion.py | 3 ++- conversion/indico_conversion/plugin.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/conversion/indico_conversion/conversion.py b/conversion/indico_conversion/conversion.py index 9ee0c83a..f6da4de6 100644 --- a/conversion/indico_conversion/conversion.py +++ b/conversion/indico_conversion/conversion.py @@ -207,13 +207,14 @@ def submit_attachment_cloudconvert(task, attachment): } try: + file_ext = os.path.splitext(attachment.file.filename)[1] job = client.Job.create(payload=job_definition) upload_task = job['tasks'][0] export_task = job['tasks'][-1] assert upload_task['operation'] == 'import/upload' assert export_task['operation'] == 'export/url' with attachment.file.open() as fd: - client.Task.upload(upload_task, attachment.file.filename, fd, attachment.file.content_type) + client.Task.upload(upload_task, f'attachment{file_ext}', fd, attachment.file.content_type) # add polling in case we miss a webhook export_task_id = export_task['id'] cloudconvert_task_cache.set(export_task_id, 'pending') diff --git a/conversion/indico_conversion/plugin.py b/conversion/indico_conversion/plugin.py index c7125cc5..a4cba888 100644 --- a/conversion/indico_conversion/plugin.py +++ b/conversion/indico_conversion/plugin.py @@ -11,7 +11,7 @@ from flask import flash, g from flask_pluginengine import render_plugin_template, uses -from wtforms.fields import BooleanField, EmailField, IntegerField, URLField +from wtforms.fields import BooleanField, EmailField, IntegerField, URLField, TextAreaField from wtforms.validators import DataRequired, NumberRange, Optional from indico.core import signals @@ -57,6 +57,9 @@ class SettingsForm(IndicoForm): cloudconvert_notify_email = EmailField(_('Notification email'), [HiddenUnless('use_cloudconvert', preserve_data=True)], description=_('Email to send the notifications to')) + cloudconvert_conversion_notice = TextAreaField(_('PDF conversion notice'), + description=_('A notice that will be shown to end users when ' + 'converting PDF files in the upload files dialog.')) valid_extensions = TextListField(_('Extensions'), filters=[lambda exts: sorted({ext.lower().lstrip('.').strip() for ext in exts})], description=_('File extensions for which PDF conversion is supported. ' @@ -81,6 +84,7 @@ class ConversionPlugin(IndicoPlugin): 'cloudconvert_sandbox': False, 'cloudconvert_notify_threshold': None, 'cloudconvert_notify_email': '', + 'cloudconvert_conversion_notice': '', 'valid_extensions': ['ppt', 'doc', 'pptx', 'docx', 'odp', 'sxi']} def init(self): @@ -105,10 +109,14 @@ def get_vars_js(self): def _add_file_form_fields(self, form_cls, **kwargs): exts = ', '.join(self.settings.get('valid_extensions')) + description = _('If enabled, your files will be converted to PDF if possible. ' + 'The following file types can be converted: {exts}').format(exts=exts) + if self.settings.get('cloudconvert_conversion_notice'): + description = '{}\n\n{}'.format(self.settings.get('cloudconvert_conversion_notice'), + _('The following file types can be converted: {exts}').format(exts=exts)) return 'convert_to_pdf', \ BooleanField(_('Convert to PDF'), widget=SwitchWidget(), - description=_('If enabled, your files will be be converted to PDF if possible. ' - 'The following file types can be converted: {exts}').format(exts=exts), + description=description, default=True) def _add_url_form_fields(self, form_cls, **kwargs): @@ -152,7 +160,7 @@ def _attachment_created(self, attachment, **kwargs): # Prepare for submission (after commit) if 'convert_attachments' not in g: g.convert_attachments = set() - g.convert_attachments.add((attachment, attachment.is_protected)) + g.convert_attachments.add(attachment) # Set cache entry to show the pending attachment pdf_state_cache.set(str(attachment.id), 'pending', timeout=info_ttl) if not g.get('attachment_conversion_msg_displayed'): @@ -165,9 +173,9 @@ def _attachment_created(self, attachment, **kwargs): 'automatically once the conversion is finished.')) def _after_commit(self, sender, **kwargs): - for attachment, is_protected in g.get('convert_attachments', ()): + for attachment in g.get('convert_attachments', ()): if attachment.type == AttachmentType.file: - if self.settings.get('use_cloudconvert') and not is_protected: + if self.settings.get('use_cloudconvert'): submit_attachment_cloudconvert.delay(attachment) else: submit_attachment_doconverter.delay(attachment) From fbe3d1c22990ffddbc0df12a5c7fc369c6635e5e Mon Sep 17 00:00:00 2001 From: Dominic Hollis Date: Mon, 27 Jan 2025 21:33:31 +0100 Subject: [PATCH 2/5] Add support for basic HTML formatting --- conversion/indico_conversion/plugin.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/conversion/indico_conversion/plugin.py b/conversion/indico_conversion/plugin.py index a4cba888..a1c32636 100644 --- a/conversion/indico_conversion/plugin.py +++ b/conversion/indico_conversion/plugin.py @@ -11,7 +11,8 @@ from flask import flash, g from flask_pluginengine import render_plugin_template, uses -from wtforms.fields import BooleanField, EmailField, IntegerField, URLField, TextAreaField +from markupsafe import Markup +from wtforms.fields import BooleanField, EmailField, IntegerField, TextAreaField, URLField from wtforms.validators import DataRequired, NumberRange, Optional from indico.core import signals @@ -59,7 +60,8 @@ class SettingsForm(IndicoForm): description=_('Email to send the notifications to')) cloudconvert_conversion_notice = TextAreaField(_('PDF conversion notice'), description=_('A notice that will be shown to end users when ' - 'converting PDF files in the upload files dialog.')) + 'converting PDF files in the upload files dialog. ' + 'You may use basic HTML elements for formatting.')) valid_extensions = TextListField(_('Extensions'), filters=[lambda exts: sorted({ext.lower().lstrip('.').strip() for ext in exts})], description=_('File extensions for which PDF conversion is supported. ' @@ -112,11 +114,11 @@ def _add_file_form_fields(self, form_cls, **kwargs): description = _('If enabled, your files will be converted to PDF if possible. ' 'The following file types can be converted: {exts}').format(exts=exts) if self.settings.get('cloudconvert_conversion_notice'): - description = '{}\n\n{}'.format(self.settings.get('cloudconvert_conversion_notice'), + description = '{}
{}'.format(self.settings.get('cloudconvert_conversion_notice'), _('The following file types can be converted: {exts}').format(exts=exts)) return 'convert_to_pdf', \ BooleanField(_('Convert to PDF'), widget=SwitchWidget(), - description=description, + description=Markup(description), default=True) def _add_url_form_fields(self, form_cls, **kwargs): From 8a695f0a72d07cd17de46b1ab7aca9908216621d Mon Sep 17 00:00:00 2001 From: Dominic Hollis Date: Tue, 28 Jan 2025 10:04:04 +0100 Subject: [PATCH 3/5] Add support for Markdown formatting too As well as clean up some ruff related warnings... --- conversion/indico_conversion/plugin.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/conversion/indico_conversion/plugin.py b/conversion/indico_conversion/plugin.py index a1c32636..7c8194c1 100644 --- a/conversion/indico_conversion/plugin.py +++ b/conversion/indico_conversion/plugin.py @@ -21,6 +21,7 @@ from indico.modules.attachments.models.attachments import AttachmentType from indico.modules.events.views import WPSimpleEventDisplay from indico.util.date_time import now_utc +from indico.util.string import render_markdown from indico.web.forms.base import IndicoForm from indico.web.forms.fields import IndicoPasswordField, TextListField from indico.web.forms.validators import HiddenUnless @@ -61,7 +62,8 @@ class SettingsForm(IndicoForm): cloudconvert_conversion_notice = TextAreaField(_('PDF conversion notice'), description=_('A notice that will be shown to end users when ' 'converting PDF files in the upload files dialog. ' - 'You may use basic HTML elements for formatting.')) + 'You may use Markdown and basic HTML elements for ' + 'formatting.')) valid_extensions = TextListField(_('Extensions'), filters=[lambda exts: sorted({ext.lower().lstrip('.').strip() for ext in exts})], description=_('File extensions for which PDF conversion is supported. ' @@ -114,11 +116,14 @@ def _add_file_form_fields(self, form_cls, **kwargs): description = _('If enabled, your files will be converted to PDF if possible. ' 'The following file types can be converted: {exts}').format(exts=exts) if self.settings.get('cloudconvert_conversion_notice'): - description = '{}
{}'.format(self.settings.get('cloudconvert_conversion_notice'), - _('The following file types can be converted: {exts}').format(exts=exts)) + notice = Markup(render_markdown(self.settings.get('cloudconvert_conversion_notice'))) + description = Markup('{}
{}').format( + notice, + _('The following file types can be converted: {exts}').format(exts=exts) + ) return 'convert_to_pdf', \ BooleanField(_('Convert to PDF'), widget=SwitchWidget(), - description=Markup(description), + description=description, default=True) def _add_url_form_fields(self, form_cls, **kwargs): From e941e74adc0cbaa363ae954a52d312481bcfd7ef Mon Sep 17 00:00:00 2001 From: Dominic Hollis Date: Thu, 30 Jan 2025 14:32:36 +0100 Subject: [PATCH 4/5] Tweak order of custom message + description --- conversion/indico_conversion/plugin.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conversion/indico_conversion/plugin.py b/conversion/indico_conversion/plugin.py index 7c8194c1..188ff682 100644 --- a/conversion/indico_conversion/plugin.py +++ b/conversion/indico_conversion/plugin.py @@ -117,9 +117,10 @@ def _add_file_form_fields(self, form_cls, **kwargs): 'The following file types can be converted: {exts}').format(exts=exts) if self.settings.get('cloudconvert_conversion_notice'): notice = Markup(render_markdown(self.settings.get('cloudconvert_conversion_notice'))) - description = Markup('{}
{}').format( - notice, - _('The following file types can be converted: {exts}').format(exts=exts) + description = Markup('{}

{}').format( + _('If enabled, your files will be converted to PDF if possible. ' + 'The following file types can be converted: {exts}').format(exts=exts), + notice, ) return 'convert_to_pdf', \ BooleanField(_('Convert to PDF'), widget=SwitchWidget(), From e47c437ea18383c043c6987d0e47e267fac97491 Mon Sep 17 00:00:00 2001 From: Dominic Hollis Date: Fri, 7 Feb 2025 16:42:59 +0100 Subject: [PATCH 5/5] Remove mention of "public materials" from setting Also fixup missing punctuation in settings --- conversion/indico_conversion/plugin.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversion/indico_conversion/plugin.py b/conversion/indico_conversion/plugin.py index 188ff682..a5f1fc8e 100644 --- a/conversion/indico_conversion/plugin.py +++ b/conversion/indico_conversion/plugin.py @@ -42,7 +42,7 @@ class SettingsForm(IndicoForm): description=_('Temporarily disable submitting files. The tasks will be kept and once ' 'this setting is disabled the files will be submitted.')) use_cloudconvert = BooleanField(_('Use CloudConvert'), widget=SwitchWidget(), - description=_('Use Cloudconvert instead of Doconverter for public materials')) + description=_('Use Cloudconvert instead of Doconverter for PDF conversion.')) server_url = URLField(_('Doconverter server URL'), [DataRequired()], description=_("The URL to the conversion server's uploadFile.py script.")) cloudconvert_api_key = IndicoPasswordField(_('CloudConvert API key'), @@ -51,14 +51,14 @@ class SettingsForm(IndicoForm): cloudconvert_sandbox = BooleanField(_('Sandbox'), [HiddenUnless('use_cloudconvert', preserve_data=True)], widget=SwitchWidget(), - description=_('Use CloudConvert sandbox')) + description=_('Use CloudConvert sandbox.')) cloudconvert_notify_threshold = IntegerField(_('CloudConvert credit threshold'), [Optional(), NumberRange(min=0), HiddenUnless('use_cloudconvert', preserve_data=True)], - description=_('Send an email when credits drop below this threshold')) + description=_('Send an email when credits drop below this threshold.')) cloudconvert_notify_email = EmailField(_('Notification email'), [HiddenUnless('use_cloudconvert', preserve_data=True)], - description=_('Email to send the notifications to')) + description=_('Email to send the notifications to.')) cloudconvert_conversion_notice = TextAreaField(_('PDF conversion notice'), description=_('A notice that will be shown to end users when ' 'converting PDF files in the upload files dialog. '