From 829ef03f1d4ecbd7be5c5f94cc26f6a6e56a1e53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Thu, 24 Nov 2022 20:35:29 +0100
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(-)

diff --git a/wcs/categories.py b/wcs/categories.py
index 2681bbc5b..1c92a9a4a 100644
--- a/wcs/categories.py
+++ b/wcs/categories.py
@@ -58,13 +58,6 @@ class Category(XmlStorableObject):
         StorableObject.__init__(self)
         self.name = name
 
-    def migrate(self):
-        changed = False
-        if not self.url_name:
-            changed = True  # trigger new slug in .store()
-        if changed:
-            self.store(comment=_('Automatic update'), snapshot_store_user=False)
-
     @classmethod
     def get_object_class(cls):
         from .formdef import FormDef
diff --git a/wcs/data_sources.py b/wcs/data_sources.py
index 327c96dc7..8a00b85aa 100644
--- a/wcs/data_sources.py
+++ b/wcs/data_sources.py
@@ -680,15 +680,6 @@ class NamedDataSource(XmlStorableObject):
             if datasource.data_source.get('value') == self.data_source.get('value'):
                 return datasource
 
-    def migrate(self):
-        changed = False
-
-        if not self.slug:
-            # .store() will take care of setting the slug
-            changed = True
-        if changed:
-            self.store(comment=_('Automatic update'), snapshot_store_user=False)
-
     def store(self, comment=None, snapshot_store_user=True, *args, **kwargs):
         assert not self.is_readonly()
         if self.slug is None:
diff --git a/wcs/fields.py b/wcs/fields.py
index a130884ad..fff1a25fd 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -602,7 +602,7 @@ class Field:
 
     def migrate(self):
         changed = False
-        if getattr(self, 'in_listing', None):
+        if getattr(self, 'in_listing', None):  # 2019-09-28
             self.display_locations = self.display_locations[:]
             self.display_locations.append('listings')
             changed = True
@@ -1333,7 +1333,7 @@ class StringField(WidgetField):
 
     def migrate(self):
         changed = super().migrate()
-        if isinstance(self.validation, str):
+        if isinstance(self.validation, str):  # 2019-08-10
             self.validation = {'type': 'regex', 'value': self.validation}
             changed = True
         return changed
@@ -1369,7 +1369,7 @@ class TextField(WidgetField):
 
     def migrate(self):
         changed = super().migrate()
-        if isinstance(getattr(self, 'pre', None), bool):
+        if isinstance(getattr(self, 'pre', None), bool):  # 2022-09-16
             if self.pre:
                 self.display_mode = 'pre'
             else:
@@ -1794,39 +1794,6 @@ class FileField(WidgetField):
             if value and hasattr(value, 'token'):
                 get_request().form[self.field_key + '$token'] = value.token
 
-    def migrate(self):
-        changed = super().migrate()
-        if 'file_type' in self.__dict__:
-            self.document_type = {}
-            if self.__dict__['file_type']:
-                file_type = self.__dict__['file_type']
-                document_types = get_document_types(self.document_type)
-                parts = []
-                for key, value in document_types.items():
-                    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([str(x) for x in parts])
-                    else:
-                        label = ','.join(file_type)
-                    self.document_type = {
-                        'id': '_legacy',
-                        'label': label,
-                        'mimetypes': file_type,
-                    }
-            del self.__dict__['file_type']
-            changed = True
-        return changed
-
     def export_to_xml(self, charset, include_id=False):
         # convert some sub-fields to strings as export_to_xml() only supports
         # dictionnaries with strings values
@@ -2219,7 +2186,7 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
 
     def migrate(self):
         changed = super().migrate()
-        if isinstance(getattr(self, 'show_as_radio', None), bool):
+        if isinstance(getattr(self, 'show_as_radio', None), bool):  # 2019-03-19
             if self.show_as_radio:
                 self.display_mode = 'radio'
             else:
@@ -3039,24 +3006,6 @@ class PageField(Field):
     def get_admin_attributes(self):
         return Field.get_admin_attributes(self) + ['post_conditions']
 
-    def migrate(self):
-        changed = super().migrate()
-        if isinstance(self.condition, str):
-            if self.condition:
-                self.condition = {'type': 'python', 'value': self.condition}
-            else:
-                self.condition = {}
-            changed = True
-        for post_condition in self.post_conditions or []:
-            condition = post_condition.get('condition')
-            if isinstance(condition, str):
-                if condition:
-                    post_condition['condition'] = {'type': 'python', 'value': condition}
-                else:
-                    post_condition['condition'] = {}
-                changed = True
-        return changed
-
     def add_to_view_form(self, *args, **kwargs):
         pass
 
diff --git a/wcs/formdata.py b/wcs/formdata.py
index 97fac0961..09348fdc9 100644
--- a/wcs/formdata.py
+++ b/wcs/formdata.py
@@ -318,24 +318,16 @@ class FormData(StorableObject):
 
     def migrate(self):
         changed = False
-        if self.status and not self.status.startswith('wf-') and self.status != 'draft':
-            self.status = 'wf-%s' % self.status
-            changed = True
-        if self.evolution:
-            for evo in self.evolution:
-                evo._formdata = self  # link from evolution to formdata
-                if evo.status and not evo.status.startswith('wf-'):
-                    evo.status = 'wf-%s' % evo.status
-                    changed = True
         if (
             not self.submission_agent_id
             and self.submission_context
             and self.submission_context.get('agent_id')
         ):
+            # 2020-07-13
             self.submission_agent_id = str(self.submission_context.get('agent_id'))
             changed = True
 
-        if 'digest' in self.__dict__:
+        if 'digest' in self.__dict__:  # 2021-06-24
             # migration from a simple digest to digests
             if not self.digests:
                 self.digests = {}
diff --git a/wcs/formdef.py b/wcs/formdef.py
index e2bdeb0e8..20a7974f4 100644
--- a/wcs/formdef.py
+++ b/wcs/formdef.py
@@ -224,73 +224,14 @@ class FormDef(StorableObject):
             # don't run migration on lightweight objects
             return
 
-        if 'receiver' in self.__dict__:
-            self.receiver_id = self.__dict__['receiver']
-            del self.__dict__['receiver']
-            changed = True
-
-        if 'category' in self.__dict__:
-            self.category_id = self.__dict__['category']
-            del self.__dict__['category']
-            changed = True
-
-        if not self.url_name:
-            try:
-                int(self.id)
-            except ValueError:
-                self.url_name = self.id
-            else:
-                self.url_name = self.get_new_url_name()
-            changed = True
-
-        if self.fields and isinstance(self.fields[0], dict):
-            for f in self.fields:
-                if 'name' in f:
-                    f['label'] = f['name']
-                    del f['name']
-            self.fields = [FormField(**x) for x in self.fields]
-            for i, f in enumerate(self.fields):
-                f.id = str(i)
-            for formdata in self.data_class().select():
-                for f in self.fields:
-                    if f.label not in formdata.data:
-                        continue
-                    formdata.data[f.id] = formdata.data[f.label]
-                    del formdata.data[f.label]
-                formdata.store()
-            changed = True
-
-        if self.fields and isinstance(self.fields[0], FormField):
-            # migration from generic FormField to specific Field classes
-            # (200603)
-            self.fields = [x.real_field for x in self.fields]
-
-        if 'public' in self.__dict__:
-            del self.__dict__['public']
-            changed = True
-
-        if 'receiver_id' in self.__dict__:
-            # migration from a simple receiver role to workflow roles
-            if not self.workflow_roles:
-                self.workflow_roles = {}
-            self.workflow_roles['_receiver'] = self.__dict__['receiver_id']
-            del self.__dict__['receiver_id']
-            changed = True
-
         if 'digest_template' in self.__dict__:
-            # migration from a simple template to templates
+            # 2021-06-22 - migration from a simple template to templates
             if not self.digest_templates:
                 self.digest_templates = {}
             self.digest_templates['default'] = self.__dict__['digest_template']
             del self.__dict__['digest_template']
             changed = True
 
-        if not self.table_name and get_publisher().has_site_option('postgresql'):
-            from . import sql
-
-            self.table_name = sql.get_formdef_table_name(self)
-            changed = True
-
         if self.max_field_id is None and self.fields:
             self.max_field_id = max(lax_int(x.id) for x in self.fields)
             changed = True
diff --git a/wcs/roles.py b/wcs/roles.py
index 5ea555041..06723b9ae 100644
--- a/wcs/roles.py
+++ b/wcs/roles.py
@@ -50,14 +50,6 @@ class Role(StorableObject):
     def __eq__(self, other):
         return bool(self.__class__ is other.__class__ and self.id == other.id)
 
-    def migrate(self):
-        changed = False
-        if not self.slug:
-            # .store() will take care of setting the slug
-            changed = True
-        if changed:
-            self.store()
-
     def store(self):
         if self.slug is None:
             # set slug if it's not yet there
diff --git a/wcs/users.py b/wcs/users.py
index 3df9a5e95..72208fba6 100644
--- a/wcs/users.py
+++ b/wcs/users.py
@@ -64,27 +64,6 @@ class User(StorableObject):
         self.verified_fields = []
         self.roles = []
 
-    def migrate(self):
-        changed = False
-
-        if self.roles and 'site-admin' in self.roles:
-            self.is_admin = True
-            self.roles = [x for x in self.roles if x != 'site-admin']
-            changed = True
-
-        if self.roles:
-            for role in self.roles:
-                if isinstance(role, int):
-                    self.roles = [str(x) for x in self.roles]
-                    changed = True
-                    break
-
-        if not self.verified_fields:
-            self.verified_fields = []
-
-        if changed:
-            self.store()
-
     @invalidate_substitution_cache
     def store(self, *args, **kwargs):
         return super().store(*args, **kwargs)
diff --git a/wcs/wf/create_formdata.py b/wcs/wf/create_formdata.py
index 5dee7946d..f8b47cac7 100644
--- a/wcs/wf/create_formdata.py
+++ b/wcs/wf/create_formdata.py
@@ -355,7 +355,7 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
 
     def migrate(self):
         changed = super().migrate()
-        if getattr(self, 'keep_user', False) is True:
+        if getattr(self, 'keep_user', False) is True:  # 2021-03-15
             self.user_association_mode = 'keep-user'
             delattr(self, 'keep_user')
             changed = True
diff --git a/wcs/wf/jump.py b/wcs/wf/jump.py
index 74ea2f187..6292f46a7 100644
--- a/wcs/wf/jump.py
+++ b/wcs/wf/jump.py
@@ -141,16 +141,6 @@ class JumpWorkflowStatusItem(WorkflowStatusJumpItem):
             else:
                 self.timeout = int(timeout)
 
-    def migrate(self):
-        changed = super().migrate()
-        if isinstance(self.condition, str):
-            if self.condition:
-                self.condition = {'type': 'python', 'value': self.condition}
-            else:
-                self.condition = {}
-            changed = True
-        return changed
-
     @property
     def waitpoint(self):
         if self.timeout or self.trigger:
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 83b3749dd..b8bb073d9 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -2483,12 +2483,7 @@ class WorkflowStatusItem(XmlSerialisable):
 
     def migrate(self):
         changed = False
-        for roles_attribute in ('to', 'by'):
-            attribute_value = getattr(self, roles_attribute, []) or []
-            if any(x for x in attribute_value if isinstance(x, int)):
-                setattr(self, roles_attribute, [str(x) for x in attribute_value])
-                changed = True
-        if getattr(self, 'attachments', None):
+        if getattr(self, 'attachments', None):  # 2022-06-19
             if any(x for x in self.attachments if not x.startswith('{{')):
                 # convert old attachment python expression to templates
                 for field in self.parent.parent.get_backoffice_fields():
-- 
2.38.1

