Projet

Général

Profil

0001-misc-prefix-CardData-FormData-class-name-to-avoid-co.patch

Lauréline Guérin, 17 septembre 2020 15:17

Télécharger (16,6 ko)

Voir les différences:

Subject: [PATCH] misc: prefix CardData & FormData class name to avoid conflict
 (#46588)

 .../concerned_roles-_submitter                | Bin 0 -> 15 bytes
 .../card-bar/.indexes/status/status-None      | Bin 0 -> 15 bytes
 .../card-bar/.indexes/user_id/user_id-None    | Bin 0 -> 15 bytes
 .../workflow_roles/workflow_roles-None        | Bin 0 -> 15 bytes
 tests/data/oldpickledata/card-bar/.max_id     |   1 +
 tests/data/oldpickledata/card-bar/1           | Bin 0 -> 76 bytes
 .../backoffice_submission_roles-None          | Bin 0 -> 15 bytes
 tests/data/oldpickledata/carddefs/1           | Bin 0 -> 315 bytes
 .../concerned_roles-_submitter                | Bin 0 -> 15 bytes
 .../form-foo/.indexes/status/status-None      | Bin 0 -> 15 bytes
 .../form-foo/.indexes/user_id/user_id-None    | Bin 0 -> 15 bytes
 .../workflow_roles/workflow_roles-None        | Bin 0 -> 15 bytes
 tests/data/oldpickledata/form-foo/.max_id     |   1 +
 tests/data/oldpickledata/form-foo/1           | Bin 0 -> 76 bytes
 .../backoffice_submission_roles-None          | Bin 0 -> 15 bytes
 tests/data/oldpickledata/formdefs/1           | Bin 0 -> 302 bytes
 tests/test_storage.py                         |  19 ++++++++++++++++--
 wcs/carddef.py                                |  12 +++++------
 wcs/ctl/management/commands/convert_to_sql.py |   2 +-
 wcs/formdata.py                               |   5 +++--
 wcs/formdef.py                                |  16 +++++++++------
 wcs/publisher.py                              |   4 ++++
 22 files changed, 43 insertions(+), 17 deletions(-)
 create mode 100644 tests/data/oldpickledata/card-bar/.indexes/concerned_roles/concerned_roles-_submitter
 create mode 100644 tests/data/oldpickledata/card-bar/.indexes/status/status-None
 create mode 100644 tests/data/oldpickledata/card-bar/.indexes/user_id/user_id-None
 create mode 100644 tests/data/oldpickledata/card-bar/.indexes/workflow_roles/workflow_roles-None
 create mode 100644 tests/data/oldpickledata/card-bar/.max_id
 create mode 100644 tests/data/oldpickledata/card-bar/1
 create mode 100644 tests/data/oldpickledata/carddefs/.indexes/backoffice_submission_roles/backoffice_submission_roles-None
 create mode 100644 tests/data/oldpickledata/carddefs/1
 create mode 100644 tests/data/oldpickledata/form-foo/.indexes/concerned_roles/concerned_roles-_submitter
 create mode 100644 tests/data/oldpickledata/form-foo/.indexes/status/status-None
 create mode 100644 tests/data/oldpickledata/form-foo/.indexes/user_id/user_id-None
 create mode 100644 tests/data/oldpickledata/form-foo/.indexes/workflow_roles/workflow_roles-None
 create mode 100644 tests/data/oldpickledata/form-foo/.max_id
 create mode 100644 tests/data/oldpickledata/form-foo/1
 create mode 100644 tests/data/oldpickledata/formdefs/.indexes/backoffice_submission_roles/backoffice_submission_roles-None
 create mode 100644 tests/data/oldpickledata/formdefs/1
tests/data/oldpickledata/card-bar/.max_id
1
1
tests/data/oldpickledata/form-foo/.max_id
1
1
tests/test_storage.py
1 1
import datetime
2 2
import os
3
import sys
4 3
import shutil
5 4
import time
6 5
import random
......
8 7
import pytest
9 8

  
10 9
from quixote import cleanup
11
from wcs import publisher
10
from wcs.carddef import CardDef
11
from wcs.formdef import FormDef
12 12
from wcs.qommon.storage import StorableObject, cache_umask
13 13
import wcs.qommon.storage as st
14 14

  
......
452 452
    cache_umask()
453 453
    test.store()
454 454
    assert (os.stat(test.get_object_filename()).st_mode % 0o1000) == 0o664
455

  
456

  
457
def test_load_old_pickle():
458
    # check picklized old data (name without 'WCS:' prefix)
459
    # formdata
460
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'data', 'oldpickledata', 'formdefs'), os.path.join(pub.app_dir, 'formdefs'))
461
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'data', 'oldpickledata', 'form-foo'), os.path.join(pub.app_dir, 'form-foo'))
462
    formdef = FormDef.select()[0]
463
    formdef.data_class().select()
464

  
465
    # carddata
466
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'data', 'oldpickledata', 'carddefs'), os.path.join(pub.app_dir, 'carddefs'))
467
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'data', 'oldpickledata', 'card-bar'), os.path.join(pub.app_dir, 'card-bar'))
468
    carddef = CardDef.select()[0]
469
    carddef.data_class().select()
wcs/carddef.py
40 40
    def data_class(self, mode=None):
41 41
        if not 'carddef' in sys.modules:
42 42
            sys.modules['carddef'] = sys.modules[__name__]
43
        if hasattr(sys.modules['carddef'], self.url_name.title()):
44
            data_class = getattr(sys.modules['carddef'], self.url_name.title())
43
        if hasattr(sys.modules['carddef'], self.data_class_name):
44
            data_class = getattr(sys.modules['carddef'], self.data_class_name)
45 45
            # only use existing data class if it has a reference to this actual
46 46
            # carddef
47 47
            if data_class._formdef is self:
......
49 49
        if (get_publisher().is_using_postgresql() and not mode == 'files') or mode == 'sql':
50 50
            from . import sql
51 51
            table_name = sql.get_formdef_table_name(self)
52
            cls = types.ClassType(self.url_name.title(), (sql.SqlCardData,),
52
            cls = types.ClassType(self.data_class_name, (sql.SqlCardData,),
53 53
                        {'_formdef': self,
54 54
                         '_table_name': table_name})
55 55
            actions = sql.do_formdef_tables(self)
56 56
        else:
57
            cls = types.ClassType(self.url_name.title(), (CardData,),
57
            cls = types.ClassType(self.data_class_name, (CardData,),
58 58
                        {'_names': 'card-%s' % self.internal_identifier,
59 59
                         '_formdef': self})
60 60
            actions = []
61
        setattr(sys.modules['carddef'], self.url_name.title(), cls)
62
        setattr(sys.modules['wcs.carddef'], self.url_name.title(), cls)
61
        setattr(sys.modules['carddef'], self.data_class_name, cls)
62
        setattr(sys.modules['wcs.carddef'], self.data_class_name, cls)
63 63

  
64 64
        if actions:
65 65
            for action in actions:
wcs/ctl/management/commands/convert_to_sql.py
124 124
            # run and the eventual changes properly saved.
125 125
            for id in data_class.keys():
126 126
                formdata = data_class.get(id)
127
            delattr(sys.modules['formdef'], formdef.url_name.title())
127
            delattr(sys.modules['formdef'], formdef.data_class_name)
128 128

  
129 129
            # once this is done, reload and store everything in postgresql
130 130
            sql_data_class = formdef.data_class(mode='sql')
wcs/formdata.py
289 289
        # particular object, as it is required by pickle (or it will raise
290 290
        # "Can't pickle %r: it's not the same object as %s.%s" if the class
291 291
        # object has been changed in the course of the request).
292
        setattr(sys.modules[self._formdef.pickle_module_name], self._formdef.url_name.title(), self.__class__)
293
        setattr(sys.modules['wcs.%s' % self._formdef.pickle_module_name], self._formdef.url_name.title(), self.__class__)
292
        print('store', self._formdef, self._formdef.pickle_module_name, self._formdef.data_class_name, self.__class__)
293
        setattr(sys.modules[self._formdef.pickle_module_name], self._formdef.data_class_name, self.__class__)
294
        setattr(sys.modules['wcs.%s' % self._formdef.pickle_module_name], self._formdef.data_class_name, self.__class__)
294 295
        has_id = (self.id is not None)
295 296
        if has_id:
296 297
            self.set_auto_fields()
wcs/formdef.py
260 260
            conn.commit()
261 261
            cur.close()
262 262

  
263
    @property
264
    def data_class_name(self):
265
        return 'WCS:%s' % self.url_name.title()
266

  
263 267
    def data_class(self, mode=None):
264 268
        if not 'formdef' in sys.modules:
265 269
            sys.modules['formdef'] = sys.modules[__name__]
266
        if hasattr(sys.modules['formdef'], self.url_name.title()):
267
            data_class = getattr(sys.modules['formdef'], self.url_name.title())
270
        if hasattr(sys.modules['formdef'], self.data_class_name):
271
            data_class = getattr(sys.modules['formdef'], self.data_class_name)
268 272
            # only use existing data class if it has a reference to this actual
269 273
            # formdef
270 274
            if data_class._formdef is self:
......
272 276
        if (get_publisher().is_using_postgresql() and not mode == 'files') or mode == 'sql':
273 277
            from . import sql
274 278
            table_name = sql.get_formdef_table_name(self)
275
            cls = types.ClassType(self.url_name.title(), (sql.SqlFormData,),
279
            cls = types.ClassType(self.data_class_name, (sql.SqlFormData,),
276 280
                        {'_formdef': self,
277 281
                         '_table_name': table_name})
278 282
            actions = sql.do_formdef_tables(self)
279 283
        else:
280
            cls = types.ClassType(self.url_name.title(), (FormData,),
284
            cls = types.ClassType(self.data_class_name, (FormData,),
281 285
                        {'_names': 'form-%s' % self.internal_identifier,
282 286
                         '_formdef': self})
283 287
            actions = []
284
        setattr(sys.modules['formdef'], self.url_name.title(), cls)
285
        setattr(sys.modules['wcs.formdef'], self.url_name.title(), cls)
288
        setattr(sys.modules['formdef'], self.data_class_name, cls)
289
        setattr(sys.modules['wcs.formdef'], self.data_class_name, cls)
286 290

  
287 291
        if actions:
288 292
            for action in actions:
wcs/publisher.py
65 65
            module = 'wcs.%s' % module
66 66
        __import__(module)
67 67
        mod = sys.modules[module]
68
        if module == 'wcs.formdef' and name != 'FormDef' and not name.startswith('WCS:'):
69
            name = 'WCS:%s' % name
70
        elif module == 'wcs.carddef' and name != 'CardDef' and not name.startswith('WCS:'):
71
            name = 'WCS:%s' % name
68 72
        klass = getattr(mod, name)
69 73
        return klass
70 74

  
71
-