From 8ed71906f9824b10913fa03f057e20f0fbcbccaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 11 Nov 2015 18:00:52 +0100 Subject: [PATCH] fields: fix label set after file type suggestion migration (#8946) --- tests/test_formdef.py | 21 +++++++++++++++++---- wcs/fields.py | 14 ++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/test_formdef.py b/tests/test_formdef.py index 53c22d6..05daa37 100644 --- a/tests/test_formdef.py +++ b/tests/test_formdef.py @@ -136,6 +136,16 @@ def test_substitution_variables_object(): assert substs.foobar def test_file_field_migration(): + pub.cfg['filetypes'] = {1: + {'mimetypes': [ + 'application/pdf', + 'application/vnd.oasis.opendocument.text', + 'application/msword', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.oasis.opendocument.spreadsheet', + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], + 'label': 'Documents'}} with patch('wcs.file_validation.get_document_types') as get_document_types: get_document_types.return_value = { 'justificatif-de-domicile': { @@ -147,13 +157,16 @@ def test_file_field_migration(): FormDef.wipe() formdef = FormDef() formdef.name = 'foo' - file_type = ['image/*', 'application/pdf,application/vnd.oasis.opendocument.text,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'] - formdef.fields = [FileField(type='file', id='1', label='file')] - formdef.fields[0].__dict__['file_type'] = file_type + formdef.fields = [ + FileField(type='file', id='1', label='images & docs'), + FileField(type='file', id='2', label='images')] + formdef.fields[0].__dict__['file_type'] = ['image/*', 'application/pdf,application/vnd.oasis.opendocument.text,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'] + formdef.fields[1].__dict__['file_type'] = ['image/*'] formdef.store() formdef = FormDef.get(1) assert 'file_type' not in formdef.fields[0].__dict__ assert formdef.fields[0].document_type assert formdef.fields[0].document_type['id'] == '_legacy' assert formdef.fields[0].document_type['mimetypes'] == ['image/*', 'application/pdf,application/vnd.oasis.opendocument.text,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'] - assert formdef.fields[0].document_type['label'] == ','.join(file_type) + assert formdef.fields[1].document_type['label'] == 'Image files' + assert formdef.fields[0].document_type['label'] == 'Image files, Documents' diff --git a/wcs/fields.py b/wcs/fields.py index 4103f8f..fd037bb 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -770,16 +770,26 @@ class FileField(WidgetField): if self.__dict__['file_type']: file_type = self.__dict__['file_type'] document_types = self.get_document_types() + parts = [] for key, value in document_types.iteritems(): - if self.file_type == value.get('mimetypes'): + if file_type == value.get('mimetypes'): self.document_type = value.copy() self.document_type['id'] = key + break + if not value.get('mimetypes'): + continue + if ','.join(value['mimetypes']) in file_type: + parts.append(value['label']) else: # self.file_type is a combination of file type, we create a # virtual one from them + if parts and len(parts) > 1: + label = ', '.join(parts) + else: + label = ','.join(file_type) self.document_type = { 'id': '_legacy', - 'label': ','.join(file_type), + 'label': label, 'mimetypes': file_type, } del self.__dict__['file_type'] -- 2.6.2