Project

General

Profile

0002-users-add-cronjob-to-delete-users-24430.patch

Benjamin Dauvergne, 14 Oct 2018 07:15 PM

Download (4.15 KB)

View differences:

Subject: [PATCH 2/4] users: add cronjob to delete users (#24430)

 tests/test_users.py | 39 +++++++++++++++++++++++++++++++++++++
 wcs/users.py        | 47 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
tests/test_users.py
77 77

  
78 78
    with pytest.raises(AttributeError):
79 79
        user.xxx
80

  
81

  
82
def test_clean_deleted_users():
83
    from wcs.formdef import FormDef
84
    from wcs.qommon import storage as st
85

  
86
    User = pub.user_class
87

  
88
    User.wipe()
89
    FormDef.wipe()
90

  
91
    formdef = FormDef()
92
    formdef.name = 'foobar'
93
    formdef.url_name = 'foobar'
94
    formdef.fields = []
95
    formdef.store()
96
    data_class = formdef.data_class()
97

  
98
    user1 = User()
99
    user1.name = 'Pierre'
100
    user1.deleted = True
101
    user1.store()
102

  
103
    user2 = User()
104
    user2.name = 'Jean'
105
    user2.deleted = True
106
    user2.store()
107

  
108
    formdata1 = data_class()
109
    formdata1.user_id = user1.id
110
    formdata1.store()
111

  
112
    assert User.count() == 2
113

  
114
    User.clean_deleted_users()
115

  
116
    assert User.count() == 1
117
    assert len(User.select([st.Equal('name', 'Pierre')])) == 1
118
    assert len(User.select([st.Equal('name', 'Jean')])) == 0
wcs/users.py
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from quixote import get_publisher
18

  
17 19
from qommon import _
18 20
from qommon.misc import simplify
19 21
from qommon.storage import StorableObject
20 22
from qommon import get_cfg
23
from qommon.cron import CronJob
21 24
import wcs.qommon.storage as st
22 25

  
23 26
from qommon.substitution import Substitutions, invalidate_substitution_cache
27
from qommon.publisher import get_publisher_class
28

  
24 29

  
25 30
class User(StorableObject):
26 31
    _names = 'users'
......
221 226
    def get_full_name(self):
222 227
        return self.display_name
223 228

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

  
237
    @classmethod
238
    def clean_deleted_users_sql(cls):
239
        from wcs.sql import AnyFormData
240
        from wcs.formdef import FormDef
241

  
242
        deleted = cls.select([st.Equal('deleted', True)])
243
        deleted_ids = set(str(u.id) for u in deleted)
244
        active_ids = set()
245
        if FormDef.count():
246
            # without any FormDef global views are not created.
247
            active_ids = set(f.user_id for f in AnyFormData.select(
248
                [st.Contains('user_id', deleted_ids)], iterator=True))
249
        to_delete_ids = deleted_ids - active_ids
250
        for user_id in to_delete_ids:
251
            cls.remove_object(user_id)
252

  
253
    @classmethod
254
    def clean_deleted_users_pickle(cls):
255
        from wcs.formdef import FormDef
256

  
257
        deleted = cls.select([st.Equal('deleted', True)])
258
        deleted_ids = set(str(u.id) for u in deleted)
259
        active_ids = set()
260
        for formdef in FormDef.select():
261
            for formdata in formdef.data_class().select([st.Contains('user_id', deleted_ids)], iterator=True):
262
                active_ids.add(str(formdata.user_id))
263
        to_delete_ids = deleted_ids - active_ids
264
        for user_id in to_delete_ids:
265
            cls.remove_object(user_id)
266

  
224 267

  
225 268
Substitutions.register('session_user_display_name', category=N_('User'), comment=N_('Session User Display Name'))
226 269
Substitutions.register('session_user_email', category=N_('User'), comment=N_('Session User Email'))
227 270
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]))
228
-