Projet

Général

Profil

0001-ctl-add-role-support-to-convert-to-sql-command-53849.patch

Frédéric Péters, 12 juillet 2021 21:57

Télécharger (4,7 ko)

Voir les différences:

Subject: [PATCH] ctl: add role support to convert-to-sql command (#53849)

 tests/test_convert_to_sql.py                  | 19 ++++++++++-
 wcs/ctl/management/commands/convert_to_sql.py | 34 ++++++++++++-------
 2 files changed, 39 insertions(+), 14 deletions(-)
tests/test_convert_to_sql.py
9 9

  
10 10
from wcs.fields import BoolField
11 11
from wcs.formdef import FormDef
12
from wcs.roles import Role
12 13
from wcs.sql import cleanup_connection
13 14

  
14 15
from .utilities import clean_temporary_pub, create_temporary_pub, force_connections_close
......
129 130
    call_command('convert_to_sql', '-d', 'example.net', '--database', database)
130 131
    pub.load_site_options()
131 132
    assert pub.site_options.has_option('options', 'postgresql')
132
    assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1
133
    pub.is_using_postgresql = lambda: True
134
    pub.set_config()
133 135
    formdefs = FormDef.select()
134 136
    assert len(formdefs) == 1
135 137
    data_class = formdefs[0].data_class(mode='sql')
136 138
    assert len(data_class.keys()) == 4
139

  
140

  
141
def test_users_and_roles(pub, database, local_user):
142
    role = Role(name='Test Role')
143
    role.store()
144

  
145
    pub.load_site_options()
146
    assert not pub.site_options.has_option('options', 'postgresql')
147
    call_command('convert_to_sql', '-d', 'example.net', '--database', database)
148
    pub.load_site_options()
149
    assert pub.site_options.has_option('options', 'postgresql')
150
    pub.is_using_postgresql = lambda: True
151
    pub.set_config()
152
    assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1
153
    assert pub.role_class.count() == 1
wcs/ctl/management/commands/convert_to_sql.py
28 28
from wcs.qommon.misc import localstrftime
29 29
from wcs.qommon.publisher import UnknownTenantError, get_publisher_class
30 30
from wcs.qommon.storage import atomic_write
31
from wcs.roles import Role
31 32
from wcs.users import User
32 33

  
33 34

  
......
50 51

  
51 52
        self.setup_connection(**options)
52 53
        sql.get_connection(new=True)
54
        self.store_roles()
53 55
        self.store_users()
54 56
        self.store_forms()
55 57
        self.publisher.write_cfg()
......
84 86
            atomic_write(options_file, force_bytes(stringio.getvalue()))
85 87

  
86 88
    def store_users(self):
89
        self.convert_objects('user', User, sql.SqlUser)
90
        sql.SqlUser.fix_sequences()
91

  
92
    def store_roles(self):
93
        self.convert_objects('role', Role, sql.Role)
94

  
95
    def convert_objects(self, object_name, object_class, object_sql_class):
87 96
        errors = []
88
        print('converting users')
89
        sql.do_user_table()
90
        count = User.count()
91
        for i, user_id in enumerate(User.keys()):
92
            user = User.get(user_id)
93
            user.__class__ = sql.SqlUser
97
        print('converting %ss' % object_name)
98
        getattr(sql, 'do_%s_table' % object_name)()
99
        count = object_class.count()
100
        for i, obj_id in enumerate(object_class.keys()):
101
            obj = object_class.get(obj_id)
102
            obj.__class__ = object_sql_class
94 103
            try:
95
                user.store()
104
                obj.store()
96 105
            except AssertionError:
97
                errors.append((user, traceback.format_exc()))
106
                errors.append((obj, traceback.format_exc()))
98 107
            self.update_progress(100 * i / count)
99
        sql.SqlUser.fix_sequences()
100 108

  
101 109
        if errors:
102
            with open('error_user.log', 'w') as error_log:
103
                for user, trace in errors:
104
                    error_log.write('user_id %s\n' % user.id)
110
            with open('error_%s.log' % object_name, 'w') as error_log:
111
                for obj, trace in errors:
112
                    error_log.write('obj_id %s\n' % obj.id)
105 113
                    error_log.write(trace)
106 114
                    error_log.write('-' * 80)
107 115
                    error_log.write('\n\n')
108
            print('There were some errors, see error_user.log for details.')
116
            print('There were some errors, see error_%s.log for details.' % object_name)
109 117

  
110 118
    def store_forms(self):
111 119
        errors = []
112
-