misc: revamp handling of document type options (#47183)

This commit is contained in:
Frédéric Péters 2020-09-30 13:47:15 +02:00
parent d018fe2a7d
commit bc676969a4
4 changed files with 66 additions and 21 deletions

View File

@ -798,6 +798,59 @@ def test_workflows_edit_sms_action(pub):
assert Workflow.get(workflow.id).possible_status[0].items[0].to == ['12345']
def test_workflows_edit_attachment_action(pub):
create_superuser(pub)
create_role()
Workflow.wipe()
workflow = Workflow(name='foo')
workflow.add_status(name='baz')
workflow.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/1/')
resp = resp.click('baz')
resp.form['action-interaction'] = 'Attachment'
resp = resp.form.submit().follow()
resp = resp.click('Attachment')
assert not resp.form['document_type'].value
resp.form['document_type'] = '_audio'
resp = resp.form.submit('submit').follow().follow()
assert Workflow.get(workflow.id).possible_status[0].items[0].document_type == {
'label': 'Sound files', 'mimetypes': ['audio/*'], 'id': '_audio'}
resp = resp.click('Attachment')
assert resp.form['document_type'].value == '_audio'
resp = resp.form.submit('submit').follow().follow()
assert Workflow.get(workflow.id).possible_status[0].items[0].document_type == {
'label': 'Sound files', 'mimetypes': ['audio/*'], 'id': '_audio'}
# configure global filetypes
pub.cfg['filetypes'] = {
1: {'mimetypes': ['application/pdf', 'application/msword'], 'label': 'Text files'}
}
pub.write_cfg()
resp = resp.click('Attachment')
resp.form['document_type'] = '1'
resp = resp.form.submit('submit').follow().follow()
assert Workflow.get(workflow.id).possible_status[0].items[0].document_type == {
'label': 'Text files', 'mimetypes': ['application/pdf', 'application/msword'], 'id': 1}
# remove global filetype
pub.cfg['filetypes'] = {}
pub.write_cfg()
# check its value is still selected
resp = resp.click('Attachment')
assert 'Text files' in [x[2] for x in resp.form['document_type'].options]
assert resp.form['document_type'].value == '1'
resp = resp.form.submit('submit').follow().follow()
assert Workflow.get(workflow.id).possible_status[0].items[0].document_type == {
'label': 'Text files', 'mimetypes': ['application/pdf', 'application/msword'], 'id': 1}
def test_workflows_edit_display_form_action(pub):
create_superuser(pub)
create_role()

View File

@ -1081,10 +1081,10 @@ class FileField(WidgetField):
def fill_admin_form(self, form):
WidgetField.fill_admin_form(self, form)
value, options = get_document_type_value_options(self.document_type)
options = get_document_type_value_options(self.document_type)
form.add(SingleSelectWidget, 'document_type', title=_('File type suggestion'),
value=value, options=options,
advanced=not(value))
value=self.document_type, options=options,
advanced=not(self.document_type))
form.add(FileSizeWidget, 'max_file_size', title=_('Max file size'),
value=self.max_file_size,
advanced=not(self.max_file_size))

View File

@ -839,7 +839,7 @@ class QLookupRedirect:
return redirect(self.url)
def get_document_types(document_type):
def get_document_types(current_document_type):
document_types = {
'_audio': {
'label': _('Sound files'),
@ -858,23 +858,15 @@ def get_document_types(document_type):
document_types.update(get_cfg('filetypes', {}))
for key, document_type in document_types.items():
document_type['id'] = key
# add current file type if it does not exist anymore in the settings
cur_dt = document_type or {}
# add current file type if it does not exist anymore in the settings
cur_dt = current_document_type
if cur_dt and cur_dt['id'] not in document_types:
document_types[cur_dt['id']] = cur_dt
return document_types
def get_document_type_value_options(document_type):
document_types = get_document_types(document_type)
cur_dt = document_type or {}
# SingleSelectWidget compare the value and not the keys, so if we want
# the current value not to be hidden, we must reset it with the corresponding
# value from settings based on the 'id'
document_type_id = cur_dt.get('id')
if document_type_id in document_types \
and cur_dt != document_types[document_type_id]:
cur_dt = document_types[document_type_id]
options = [(None, '---', {})]
def get_document_type_value_options(current_document_type):
document_types = get_document_types(current_document_type)
options = [({}, '---', '')]
options += [(doc_type, doc_type['label'], key) for key, doc_type in document_types.items()]
return cur_dt, options
return options

View File

@ -204,10 +204,10 @@ class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
title=_('Push to portfolio'),
value=self.push_to_portfolio)
if 'document_type' in parameters:
value, options = get_document_type_value_options(self.document_type)
options = get_document_type_value_options(self.document_type)
form.add(SingleSelectWidget, 'document_type', title=_('File type suggestion'),
value=value, options=options,
advanced=not(value))
value=self.document_type, options=options,
advanced=not(self.document_type))
if 'max_file_size' in parameters:
form.add(FileSizeWidget, 'max_file_size', title=_('Max file size'),
value=self.max_file_size,