Projet

Général

Profil

0001-fields-fix-label-set-after-file-type-suggestion-migr.patch

Frédéric Péters, 11 novembre 2015 18:01

Télécharger (4,82 ko)

Voir les différences:

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(-)
tests/test_formdef.py
136 136
        assert substs.foobar
137 137

  
138 138
def test_file_field_migration():
139
    pub.cfg['filetypes'] = {1:
140
            {'mimetypes': [
141
                'application/pdf',
142
                'application/vnd.oasis.opendocument.text',
143
                'application/msword',
144
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
145
                'application/vnd.oasis.opendocument.spreadsheet',
146
                'application/vnd.ms-excel',
147
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
148
             'label': 'Documents'}}
139 149
    with patch('wcs.file_validation.get_document_types') as get_document_types:
140 150
        get_document_types.return_value = {
141 151
                'justificatif-de-domicile': {
......
147 157
        FormDef.wipe()
148 158
        formdef = FormDef()
149 159
        formdef.name = 'foo'
150
        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']
151
        formdef.fields = [FileField(type='file', id='1', label='file')]
152
        formdef.fields[0].__dict__['file_type'] = file_type
160
        formdef.fields = [
161
                FileField(type='file', id='1', label='images & docs'),
162
                FileField(type='file', id='2', label='images')]
163
        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']
164
        formdef.fields[1].__dict__['file_type'] = ['image/*']
153 165
        formdef.store()
154 166
        formdef = FormDef.get(1)
155 167
        assert 'file_type' not in formdef.fields[0].__dict__
156 168
        assert formdef.fields[0].document_type
157 169
        assert formdef.fields[0].document_type['id'] == '_legacy'
158 170
        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']
159
        assert formdef.fields[0].document_type['label'] == ','.join(file_type)
171
        assert formdef.fields[1].document_type['label'] == 'Image files'
172
        assert formdef.fields[0].document_type['label'] == 'Image files, Documents'
wcs/fields.py
770 770
            if self.__dict__['file_type']:
771 771
                file_type = self.__dict__['file_type']
772 772
                document_types = self.get_document_types()
773
                parts = []
773 774
                for key, value in document_types.iteritems():
774
                    if self.file_type == value.get('mimetypes'):
775
                    if file_type == value.get('mimetypes'):
775 776
                        self.document_type = value.copy()
776 777
                        self.document_type['id'] = key
778
                        break
779
                    if not value.get('mimetypes'):
780
                        continue
781
                    if ','.join(value['mimetypes']) in file_type:
782
                        parts.append(value['label'])
777 783
                else:
778 784
                    # self.file_type is a combination of file type, we create a
779 785
                    # virtual one from them
786
                    if parts and len(parts) > 1:
787
                        label = ', '.join(parts)
788
                    else:
789
                        label = ','.join(file_type)
780 790
                    self.document_type = {
781 791
                        'id': '_legacy',
782
                        'label': ','.join(file_type),
792
                        'label': label,
783 793
                        'mimetypes': file_type,
784 794
                    }
785 795
            del self.__dict__['file_type']
786
-