Projet

Général

Profil

0001-misc-clean-old-migrate-methods-71693.patch

Frédéric Péters, 25 novembre 2022 07:48

Télécharger (20,6 ko)

Voir les différences:

Subject: [PATCH] misc: clean old migrate methods (#71693)

 tests/test_formdef.py      | 77 +-------------------------------------
 tests/test_role.py         | 15 --------
 tests/workflow/test_all.py | 29 --------------
 wcs/categories.py          |  7 ----
 wcs/data_sources.py        |  9 -----
 wcs/fields.py              | 60 ++---------------------------
 wcs/formdata.py            | 12 +-----
 wcs/formdef.py             | 61 +-----------------------------
 wcs/roles.py               |  7 +---
 wcs/users.py               | 21 -----------
 wcs/wf/create_formdata.py  |  2 +-
 wcs/wf/jump.py             | 10 -----
 wcs/workflows.py           |  7 +---
 13 files changed, 11 insertions(+), 306 deletions(-)
tests/test_formdef.py
13 13
from wcs.admin.settings import UserFieldsFormDef
14 14
from wcs.blocks import BlockDef
15 15
from wcs.carddef import CardDef
16
from wcs.fields import DateField, FileField, ItemField, PageField, StringField
16
from wcs.fields import DateField, ItemField, StringField
17 17
from wcs.formdef import FormDef, get_formdefs_of_all_kinds
18 18
from wcs.qommon.http_request import HTTPRequest
19 19
from wcs.qommon.upload_storage import PicklableUpload
......
212 212
    assert substs.formdef is formdef
213 213

  
214 214

  
215
def test_file_field_migration(pub):
216
    pub.cfg['filetypes'] = {
217
        1: {
218
            'mimetypes': [
219
                'application/pdf',
220
                'application/vnd.oasis.opendocument.text',
221
                'application/msword',
222
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
223
                'application/vnd.oasis.opendocument.spreadsheet',
224
                'application/vnd.ms-excel',
225
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
226
            ],
227
            'label': 'Documents',
228
        }
229
    }
230

  
231
    FormDef.wipe()
232
    formdef = FormDef()
233
    formdef.name = 'foo'
234
    formdef.fields = [
235
        FileField(type='file', id='1', label='images & docs'),
236
        FileField(type='file', id='2', label='images'),
237
    ]
238
    formdef.fields[0].__dict__['file_type'] = [
239
        'image/*',
240
        (
241
            'application/pdf,application/vnd.oasis.opendocument.text,application/msword,'
242
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document,'
243
            'application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,'
244
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
245
        ),
246
    ]
247
    formdef.fields[1].__dict__['file_type'] = ['image/*']
248
    formdef.store()
249
    formdef = FormDef.get(1)
250
    assert 'file_type' not in formdef.fields[0].__dict__
251
    assert formdef.fields[0].document_type
252
    assert formdef.fields[0].document_type['id'] == '_legacy'
253
    assert formdef.fields[0].document_type['mimetypes'] == [
254
        'image/*',
255
        (
256
            'application/pdf,application/vnd.oasis.opendocument.text,application/msword,'
257
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document,'
258
            'application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,'
259
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
260
        ),
261
    ]
262
    assert formdef.fields[1].document_type['label'] == 'Image files'
263
    assert formdef.fields[0].document_type['label'] == 'Image files, Documents'
264

  
265

  
266 215
def test_internal_identifier_migration(pub):
267 216
    FormDef.wipe()
268 217
    formdef = FormDef()
......
283 232
    assert formdef.internal_identifier == 'foo'
284 233

  
285 234

  
286
def test_page_field_migration(pub):
287
    # disable snapshots as XML serialization of old post_conditions structure
288
    # is not possible.
289
    pub.snapshot_class = None
290
    FormDef.wipe()
291
    formdef = FormDef()
292
    formdef.name = 'foo'
293
    formdef.fields = [
294
        PageField(
295
            id='1',
296
            type='page',
297
            condition='foo',
298
            # old post_conditions structure
299
            post_conditions=[{'condition': 'blah', 'error_message': 'blah'}],
300
        ),
301
    ]
302
    formdef.store()
303
    formdef = FormDef.get(formdef.id)
304
    assert formdef.fields[0].condition == {'type': 'python', 'value': 'foo'}
305
    assert formdef.fields[0].post_conditions == [
306
        {'condition': {'type': 'python', 'value': 'blah'}, 'error_message': 'blah'}
307
    ]
308

  
309

  
310 235
def test_unused_file_removal_job(pub):
311 236
    from wcs.formdef import clean_unused_files
312 237

  
tests/test_role.py
1 1
from quixote import get_publisher
2 2

  
3
from wcs import sql
4 3
from wcs.roles import get_user_roles
5 4

  
6 5
from .utilities import clean_temporary_pub, create_temporary_pub
......
33 32
    assert role.slug == 'hello-world-1'
34 33

  
35 34

  
36
def test_migrate():
37
    get_publisher().role_class.wipe()
38
    role = get_publisher().role_class(name='Hello world')
39
    role.store()
40

  
41
    conn, cur = sql.get_connection_and_cursor()
42
    sql_statement = 'UPDATE roles SET slug = NULL'
43
    cur.execute(sql_statement)
44
    conn.commit()
45
    cur.close()
46

  
47
    assert get_publisher().role_class.get(role.id).slug == 'hello-world'
48

  
49

  
50 35
def test_get_user_roles():
51 36
    get_publisher().role_class.wipe()
52 37
    get_publisher().role_class(name='f1').store()
tests/workflow/test_all.py
3112 3112
    assert '>[if-any test]a[else]b[end]</button>' in str(form.render())
3113 3113

  
3114 3114

  
3115
def test_workflow_role_type_migration(pub):
3116
    workflow = Workflow(name='role migration')
3117
    st1 = workflow.add_status('Status1', 'st1')
3118

  
3119
    jump = st1.add_action('jump', id='_jump')
3120
    jump.by = [1, 2]
3121

  
3122
    workflow.store()
3123

  
3124
    reloaded_workflow = Workflow.get(workflow.id)
3125
    assert reloaded_workflow.possible_status[0].items[0].by == ['1', '2']
3126

  
3127

  
3128 3115
def test_workflow_display_message(pub):
3129 3116
    pub.substitutions.feed(MockSubstitutionVariables())
3130 3117

  
......
4553 4540
    assert item.perform(formdata) is None
4554 4541

  
4555 4542

  
4556
def test_workflow_jump_condition_migration(pub):
4557
    workflow = Workflow(name='jump condition migration')
4558
    st1 = workflow.add_status('Status1', 'st1')
4559

  
4560
    jump = st1.add_action('jump', id='_jump')
4561
    workflow.store()
4562

  
4563
    reloaded_workflow = Workflow.get(workflow.id)
4564
    assert reloaded_workflow.possible_status[0].items[0].condition is None
4565

  
4566
    jump.condition = 'foobar'
4567
    workflow.store()
4568
    reloaded_workflow = Workflow.get(workflow.id)
4569
    assert reloaded_workflow.possible_status[0].items[0].condition == {'type': 'python', 'value': 'foobar'}
4570

  
4571

  
4572 4543
def test_workflow_action_condition(pub):
4573 4544
    pub._set_request(None)  # to avoid after jobs
4574 4545
    workflow = Workflow(name='jump condition migration')
wcs/categories.py
58 58
        StorableObject.__init__(self)
59 59
        self.name = name
60 60

  
61
    def migrate(self):
62
        changed = False
63
        if not self.url_name:
64
            changed = True  # trigger new slug in .store()
65
        if changed:
66
            self.store(comment=_('Automatic update'), snapshot_store_user=False)
67

  
68 61
    @classmethod
69 62
    def get_object_class(cls):
70 63
        from .formdef import FormDef
wcs/data_sources.py
680 680
            if datasource.data_source.get('value') == self.data_source.get('value'):
681 681
                return datasource
682 682

  
683
    def migrate(self):
684
        changed = False
685

  
686
        if not self.slug:
687
            # .store() will take care of setting the slug
688
            changed = True
689
        if changed:
690
            self.store(comment=_('Automatic update'), snapshot_store_user=False)
691

  
692 683
    def store(self, comment=None, snapshot_store_user=True, *args, **kwargs):
693 684
        assert not self.is_readonly()
694 685
        if self.slug is None:
wcs/fields.py
82 82
    ellipsize,
83 83
    get_as_datetime,
84 84
    get_document_type_value_options,
85
    get_document_types,
86 85
    strftime,
87 86
    strip_some_tags,
88 87
    xml_node_text,
......
602 601

  
603 602
    def migrate(self):
604 603
        changed = False
605
        if getattr(self, 'in_listing', None):
604
        if getattr(self, 'in_listing', None):  # 2019-09-28
606 605
            self.display_locations = self.display_locations[:]
607 606
            self.display_locations.append('listings')
608 607
            changed = True
......
1343 1342

  
1344 1343
    def migrate(self):
1345 1344
        changed = super().migrate()
1346
        if isinstance(self.validation, str):
1345
        if isinstance(self.validation, str):  # 2019-08-10
1347 1346
            self.validation = {'type': 'regex', 'value': self.validation}
1348 1347
            changed = True
1349 1348
        return changed
......
1379 1378

  
1380 1379
    def migrate(self):
1381 1380
        changed = super().migrate()
1382
        if isinstance(getattr(self, 'pre', None), bool):
1381
        if isinstance(getattr(self, 'pre', None), bool):  # 2022-09-16
1383 1382
            if self.pre:
1384 1383
                self.display_mode = 'pre'
1385 1384
            else:
......
1804 1803
            if value and hasattr(value, 'token'):
1805 1804
                get_request().form[self.field_key + '$token'] = value.token
1806 1805

  
1807
    def migrate(self):
1808
        changed = super().migrate()
1809
        if 'file_type' in self.__dict__:
1810
            self.document_type = {}
1811
            if self.__dict__['file_type']:
1812
                file_type = self.__dict__['file_type']
1813
                document_types = get_document_types(self.document_type)
1814
                parts = []
1815
                for key, value in document_types.items():
1816
                    if file_type == value.get('mimetypes'):
1817
                        self.document_type = value.copy()
1818
                        self.document_type['id'] = key
1819
                        break
1820
                    if not value.get('mimetypes'):
1821
                        continue
1822
                    if ','.join(value['mimetypes']) in file_type:
1823
                        parts.append(value['label'])
1824
                else:
1825
                    # self.file_type is a combination of file type, we create a
1826
                    # virtual one from them
1827
                    if parts and len(parts) > 1:
1828
                        label = ', '.join([str(x) for x in parts])
1829
                    else:
1830
                        label = ','.join(file_type)
1831
                    self.document_type = {
1832
                        'id': '_legacy',
1833
                        'label': label,
1834
                        'mimetypes': file_type,
1835
                    }
1836
            del self.__dict__['file_type']
1837
            changed = True
1838
        return changed
1839

  
1840 1806
    def export_to_xml(self, charset, include_id=False):
1841 1807
        # convert some sub-fields to strings as export_to_xml() only supports
1842 1808
        # dictionnaries with strings values
......
2229 2195

  
2230 2196
    def migrate(self):
2231 2197
        changed = super().migrate()
2232
        if isinstance(getattr(self, 'show_as_radio', None), bool):
2198
        if isinstance(getattr(self, 'show_as_radio', None), bool):  # 2019-03-19
2233 2199
            if self.show_as_radio:
2234 2200
                self.display_mode = 'radio'
2235 2201
            else:
......
3049 3015
    def get_admin_attributes(self):
3050 3016
        return Field.get_admin_attributes(self) + ['post_conditions']
3051 3017

  
3052
    def migrate(self):
3053
        changed = super().migrate()
3054
        if isinstance(self.condition, str):
3055
            if self.condition:
3056
                self.condition = {'type': 'python', 'value': self.condition}
3057
            else:
3058
                self.condition = {}
3059
            changed = True
3060
        for post_condition in self.post_conditions or []:
3061
            condition = post_condition.get('condition')
3062
            if isinstance(condition, str):
3063
                if condition:
3064
                    post_condition['condition'] = {'type': 'python', 'value': condition}
3065
                else:
3066
                    post_condition['condition'] = {}
3067
                changed = True
3068
        return changed
3069

  
3070 3018
    def add_to_view_form(self, *args, **kwargs):
3071 3019
        pass
3072 3020

  
wcs/formdata.py
318 318

  
319 319
    def migrate(self):
320 320
        changed = False
321
        if self.status and not self.status.startswith('wf-') and self.status != 'draft':
322
            self.status = 'wf-%s' % self.status
323
            changed = True
324
        if self.evolution:
325
            for evo in self.evolution:
326
                evo._formdata = self  # link from evolution to formdata
327
                if evo.status and not evo.status.startswith('wf-'):
328
                    evo.status = 'wf-%s' % evo.status
329
                    changed = True
330 321
        if (
331 322
            not self.submission_agent_id
332 323
            and self.submission_context
333 324
            and self.submission_context.get('agent_id')
334 325
        ):
326
            # 2020-07-13
335 327
            self.submission_agent_id = str(self.submission_context.get('agent_id'))
336 328
            changed = True
337 329

  
338
        if 'digest' in self.__dict__:
330
        if 'digest' in self.__dict__:  # 2021-06-24
339 331
            # migration from a simple digest to digests
340 332
            if not self.digests:
341 333
                self.digests = {}
wcs/formdef.py
224 224
            # don't run migration on lightweight objects
225 225
            return
226 226

  
227
        if 'receiver' in self.__dict__:
228
            self.receiver_id = self.__dict__['receiver']
229
            del self.__dict__['receiver']
230
            changed = True
231

  
232
        if 'category' in self.__dict__:
233
            self.category_id = self.__dict__['category']
234
            del self.__dict__['category']
235
            changed = True
236

  
237
        if not self.url_name:
238
            try:
239
                int(self.id)
240
            except ValueError:
241
                self.url_name = self.id
242
            else:
243
                self.url_name = self.get_new_url_name()
244
            changed = True
245

  
246
        if self.fields and isinstance(self.fields[0], dict):
247
            for f in self.fields:
248
                if 'name' in f:
249
                    f['label'] = f['name']
250
                    del f['name']
251
            self.fields = [FormField(**x) for x in self.fields]
252
            for i, f in enumerate(self.fields):
253
                f.id = str(i)
254
            for formdata in self.data_class().select():
255
                for f in self.fields:
256
                    if f.label not in formdata.data:
257
                        continue
258
                    formdata.data[f.id] = formdata.data[f.label]
259
                    del formdata.data[f.label]
260
                formdata.store()
261
            changed = True
262

  
263
        if self.fields and isinstance(self.fields[0], FormField):
264
            # migration from generic FormField to specific Field classes
265
            # (200603)
266
            self.fields = [x.real_field for x in self.fields]
267

  
268
        if 'public' in self.__dict__:
269
            del self.__dict__['public']
270
            changed = True
271

  
272
        if 'receiver_id' in self.__dict__:
273
            # migration from a simple receiver role to workflow roles
274
            if not self.workflow_roles:
275
                self.workflow_roles = {}
276
            self.workflow_roles['_receiver'] = self.__dict__['receiver_id']
277
            del self.__dict__['receiver_id']
278
            changed = True
279

  
280 227
        if 'digest_template' in self.__dict__:
281
            # migration from a simple template to templates
228
            # 2021-06-22 - migration from a simple template to templates
282 229
            if not self.digest_templates:
283 230
                self.digest_templates = {}
284 231
            self.digest_templates['default'] = self.__dict__['digest_template']
285 232
            del self.__dict__['digest_template']
286 233
            changed = True
287 234

  
288
        if not self.table_name and get_publisher().has_site_option('postgresql'):
289
            from . import sql
290

  
291
            self.table_name = sql.get_formdef_table_name(self)
292
            changed = True
293

  
294 235
        if self.max_field_id is None and self.fields:
295 236
            self.max_field_id = max(lax_int(x.id) for x in self.fields)
296 237
            changed = True
wcs/roles.py
51 51
        return bool(self.__class__ is other.__class__ and self.id == other.id)
52 52

  
53 53
    def migrate(self):
54
        changed = False
55
        if not self.slug:
56
            # .store() will take care of setting the slug
57
            changed = True
58
        if changed:
59
            self.store()
54
        pass
60 55

  
61 56
    def store(self):
62 57
        if self.slug is None:
wcs/users.py
64 64
        self.verified_fields = []
65 65
        self.roles = []
66 66

  
67
    def migrate(self):
68
        changed = False
69

  
70
        if self.roles and 'site-admin' in self.roles:
71
            self.is_admin = True
72
            self.roles = [x for x in self.roles if x != 'site-admin']
73
            changed = True
74

  
75
        if self.roles:
76
            for role in self.roles:
77
                if isinstance(role, int):
78
                    self.roles = [str(x) for x in self.roles]
79
                    changed = True
80
                    break
81

  
82
        if not self.verified_fields:
83
            self.verified_fields = []
84

  
85
        if changed:
86
            self.store()
87

  
88 67
    @invalidate_substitution_cache
89 68
    def store(self, *args, **kwargs):
90 69
        return super().store(*args, **kwargs)
wcs/wf/create_formdata.py
355 355

  
356 356
    def migrate(self):
357 357
        changed = super().migrate()
358
        if getattr(self, 'keep_user', False) is True:
358
        if getattr(self, 'keep_user', False) is True:  # 2021-03-15
359 359
            self.user_association_mode = 'keep-user'
360 360
            delattr(self, 'keep_user')
361 361
            changed = True
wcs/wf/jump.py
141 141
            else:
142 142
                self.timeout = int(timeout)
143 143

  
144
    def migrate(self):
145
        changed = super().migrate()
146
        if isinstance(self.condition, str):
147
            if self.condition:
148
                self.condition = {'type': 'python', 'value': self.condition}
149
            else:
150
                self.condition = {}
151
            changed = True
152
        return changed
153

  
154 144
    @property
155 145
    def waitpoint(self):
156 146
        if self.timeout or self.trigger:
wcs/workflows.py
2491 2491

  
2492 2492
    def migrate(self):
2493 2493
        changed = False
2494
        for roles_attribute in ('to', 'by'):
2495
            attribute_value = getattr(self, roles_attribute, []) or []
2496
            if any(x for x in attribute_value if isinstance(x, int)):
2497
                setattr(self, roles_attribute, [str(x) for x in attribute_value])
2498
                changed = True
2499
        if getattr(self, 'attachments', None):
2494
        if getattr(self, 'attachments', None):  # 2022-06-19
2500 2495
            if any(x for x in self.attachments if not x.startswith('{{')):
2501 2496
                # convert old attachment python expression to templates
2502 2497
                for field in self.parent.parent.get_backoffice_fields():
2503
-