Project

General

Profile

0003-d-placement-impl-mentation-clean_deleted_users-fixup.patch

Benjamin Dauvergne, 17 Dec 2018 01:37 PM

Download (4.45 KB)

View differences:

Subject: [PATCH 3/6] =?UTF-8?q?d=C3=A9placement=20impl=C3=A9mentation=20cl?=
 =?UTF-8?q?ean=5Fdeleted=5Fusers=20(=C3=A0=20fixuper)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 tests/test_users.py |  4 ++--
 wcs/publisher.py    |  6 ++++++
 wcs/sql.py          | 12 ++++++++++++
 wcs/users.py        | 24 ++++++++++++++----------
 4 files changed, 34 insertions(+), 12 deletions(-)
tests/test_users.py
7 7

  
8 8
import pytest
9 9

  
10
from quixote import cleanup
10
from quixote import cleanup, get_publisher
11 11

  
12 12
from wcs import publisher
13 13
from wcs import fields
......
111 111

  
112 112
    assert User.count() == 2
113 113

  
114
    User.clean_deleted_users()
114
    get_publisher().clean_deleted_users()
115 115

  
116 116
    assert User.count() == 1
117 117
    assert len(User.select([st.Equal('name', 'Pierre')])) == 1
wcs/publisher.py
115 115
        super(WcsPublisher, cls).register_cronjobs()
116 116
        # every hour: check for global action timeouts
117 117
        cls.register_cronjob(CronJob(cls.apply_global_action_timeouts, hourly=True))
118
        cls.register_cronjob(CronJob(cls.clean_deleted_users, hourly=True))
118 119

  
119 120
    def is_using_postgresql(self):
120 121
        return bool(self.has_site_option('postgresql') and self.cfg.get('postgresql', {}))
......
309 310
            import sql
310 311
            sql.cleanup_connection()
311 312

  
313
    def clean_deleted_users(self):
314
        for user_id in self.user_class.get_to_delete_ids():
315
            self.user_class.remove_object(user_id)
316

  
317

  
312 318
set_publisher_class(WcsPublisher)
313 319
WcsPublisher.register_extra_dir(os.path.join(os.path.dirname(__file__), 'extra'))
314 320

  
wcs/sql.py
1794 1794

  
1795 1795
        return objects
1796 1796

  
1797
    @classmethod
1798
    def get_to_delete_ids(cls):
1799
        from .formdef import FormDef
1800

  
1801
        deleted_ids = set(str(user.id) for user in cls.select([Equal('deleted', True)]))
1802
        for formdef in FormDef.select():
1803
            for formdata in formdef.data_class().select([Contains('user_id', deleted_ids)], iterator=True):
1804
                user_id = str(formdata.user_id)
1805
                if user_id in deleted_ids:
1806
                    deleted_ids.remove(str(formdata.user_id))
1807
        return deleted_ids
1808

  
1797 1809

  
1798 1810
class Session(SqlMixin, wcs.sessions.BasicSession):
1799 1811
    _table_name = 'sessions'
wcs/users.py
227 227
        return self.display_name
228 228

  
229 229
    @classmethod
230
    def clean_deleted_users(cls):
231
        # really delete users without any form
232
        if get_publisher().is_using_postgresql():
233
            cls.clean_deleted_users_sql()
234
        else:
235
            cls.clean_deleted_users_pickle()
230
    def get_to_delete_ids(cls):
231
        from .formdef import FormDef
232

  
233
        deleted_ids = set(str(user.id) for user in cls.select([st.Equal('deleted', True)]))
234
        for formdef in FormDef.select():
235
            data_class = formdef.data_class()
236
            formdatas = data_class.select(
237
                [st.Contains('user_id', deleted_ids)],
238
                iterator=True)
239
            for formdata in formdatas:
240
                user_id = str(formdata.user_id)
241
                if user_id in deleted_ids:
242
                    deleted_ids.remove(str(formdata.user_id))
243
        return deleted_ids
236 244

  
237 245
    @classmethod
238 246
    def clean_deleted_users_sql(cls):
......
268 276
Substitutions.register('session_user_display_name', category=N_('User'), comment=N_('Session User Display Name'))
269 277
Substitutions.register('session_user_email', category=N_('User'), comment=N_('Session User Email'))
270 278
Substitutions.register_dynamic_source(User)
271

  
272
if get_publisher_class():
273
    # clean deleted users hourly
274
    get_publisher_class().register_cronjob(CronJob(User.clean_deleted_users, minutes=[20], hours=[6]))
275
-