Projet

Général

Profil

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

Frédéric Péters, 24 novembre 2022 20:36

Télécharger (14 ko)

Voir les différences:

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

 wcs/categories.py         |  7 -----
 wcs/data_sources.py       |  9 ------
 wcs/fields.py             | 59 +++----------------------------------
 wcs/formdata.py           | 12 ++------
 wcs/formdef.py            | 61 +--------------------------------------
 wcs/roles.py              |  8 -----
 wcs/users.py              | 21 --------------
 wcs/wf/create_formdata.py |  2 +-
 wcs/wf/jump.py            | 10 -------
 wcs/workflows.py          |  7 +----
 10 files changed, 9 insertions(+), 187 deletions(-)
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
602 602

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

  
1334 1334
    def migrate(self):
1335 1335
        changed = super().migrate()
1336
        if isinstance(self.validation, str):
1336
        if isinstance(self.validation, str):  # 2019-08-10
1337 1337
            self.validation = {'type': 'regex', 'value': self.validation}
1338 1338
            changed = True
1339 1339
        return changed
......
1369 1369

  
1370 1370
    def migrate(self):
1371 1371
        changed = super().migrate()
1372
        if isinstance(getattr(self, 'pre', None), bool):
1372
        if isinstance(getattr(self, 'pre', None), bool):  # 2022-09-16
1373 1373
            if self.pre:
1374 1374
                self.display_mode = 'pre'
1375 1375
            else:
......
1794 1794
            if value and hasattr(value, 'token'):
1795 1795
                get_request().form[self.field_key + '$token'] = value.token
1796 1796

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

  
1830 1797
    def export_to_xml(self, charset, include_id=False):
1831 1798
        # convert some sub-fields to strings as export_to_xml() only supports
1832 1799
        # dictionnaries with strings values
......
2219 2186

  
2220 2187
    def migrate(self):
2221 2188
        changed = super().migrate()
2222
        if isinstance(getattr(self, 'show_as_radio', None), bool):
2189
        if isinstance(getattr(self, 'show_as_radio', None), bool):  # 2019-03-19
2223 2190
            if self.show_as_radio:
2224 2191
                self.display_mode = 'radio'
2225 2192
            else:
......
3039 3006
    def get_admin_attributes(self):
3040 3007
        return Field.get_admin_attributes(self) + ['post_conditions']
3041 3008

  
3042
    def migrate(self):
3043
        changed = super().migrate()
3044
        if isinstance(self.condition, str):
3045
            if self.condition:
3046
                self.condition = {'type': 'python', 'value': self.condition}
3047
            else:
3048
                self.condition = {}
3049
            changed = True
3050
        for post_condition in self.post_conditions or []:
3051
            condition = post_condition.get('condition')
3052
            if isinstance(condition, str):
3053
                if condition:
3054
                    post_condition['condition'] = {'type': 'python', 'value': condition}
3055
                else:
3056
                    post_condition['condition'] = {}
3057
                changed = True
3058
        return changed
3059

  
3060 3009
    def add_to_view_form(self, *args, **kwargs):
3061 3010
        pass
3062 3011

  
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
50 50
    def __eq__(self, other):
51 51
        return bool(self.__class__ is other.__class__ and self.id == other.id)
52 52

  
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()
60

  
61 53
    def store(self):
62 54
        if self.slug is None:
63 55
            # set slug if it's not yet there
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
2483 2483

  
2484 2484
    def migrate(self):
2485 2485
        changed = False
2486
        for roles_attribute in ('to', 'by'):
2487
            attribute_value = getattr(self, roles_attribute, []) or []
2488
            if any(x for x in attribute_value if isinstance(x, int)):
2489
                setattr(self, roles_attribute, [str(x) for x in attribute_value])
2490
                changed = True
2491
        if getattr(self, 'attachments', None):
2486
        if getattr(self, 'attachments', None):  # 2022-06-19
2492 2487
            if any(x for x in self.attachments if not x.startswith('{{')):
2493 2488
                # convert old attachment python expression to templates
2494 2489
                for field in self.parent.parent.get_backoffice_fields():
2495
-