~homeworkprod/byceps

ref: 90357856fb25b54d9e60125d1b658be3514449ae byceps/scripts/delete_old_user_login_events.py -rwxr-xr-x 926 bytes
90357856 — Jochen Kupperschmidt Prepare login-based test with frozen time for Flask v2.x, Werkzeug v2.x 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python

"""Delete login user events older than a given number of days.

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from datetime import datetime, timedelta

import click

from byceps.services.user import event_service as user_event_service

from _util import call_with_app_context


@click.command()
@click.argument('minimum_age_in_days', type=int)
def execute(minimum_age_in_days):
    now = datetime.utcnow()
    occurred_before = now - timedelta(days=minimum_age_in_days)

    click.secho(
        f'Deleting all user login events older than {minimum_age_in_days} days '
        f'(i.e. before {occurred_before:%Y-%m-%d %H:%M:%S}) ...'
    )

    num_deleted = user_event_service.delete_user_login_events(occurred_before)

    click.secho(f'{num_deleted} user login events deleted.')


if __name__ == '__main__':
    call_with_app_context(execute)