Passed
Push — main ( 7da116...6ee49c )
by Jochen
04:48
created

delete_old_verification_tokens()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.2441

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 0
dl 0
loc 16
ccs 3
cts 8
cp 0.375
crap 1.2441
rs 9.75
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.maintenance.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from datetime import datetime, timedelta
10
11 1
from flask_babel import gettext
12
13 1
from ....services.user import event_service as user_event_service
14 1
from ....services.verification_token import (
15
    service as verification_token_service,
16
)
17 1
from ....util.framework.blueprint import create_blueprint
18 1
from ....util.framework.flash import flash_success
19 1
from ....util.framework.templating import templated
20 1
from ....util.views import permission_required, respond_no_content
21
22 1
from ..core.authorization import AdminPermission
23
24
25 1
blueprint = create_blueprint('maintenance_admin', __name__)
26
27
28 1
@blueprint.get('')
29 1
@permission_required(AdminPermission.access)
30 1
@templated
31
def index():
32
    """Show maintenance overview."""
33
    verification_token_counts_by_purpose = (
34
        verification_token_service.count_tokens_by_purpose()
35
    )
36
37
    verification_token_counts_by_purpose_name = {
38
        purpose.name: count
39
        for purpose, count in verification_token_counts_by_purpose.items()
40
    }
41
42
    verification_token_total = sum(
43
        verification_token_counts_by_purpose.values()
44
    )
45
46
    return {
47
        'verification_token_counts_by_purpose_name': verification_token_counts_by_purpose_name,
48
        'verification_token_total': verification_token_total,
49
    }
50
51
52 1
@blueprint.post('/delete_old_login_events')
53 1
@permission_required(AdminPermission.access)
54 1
@respond_no_content
55
def delete_old_login_events():
56
    """Delete login events older than a given number of days."""
57
    now = datetime.utcnow()
58
    minimum_age_in_days = 90
59
    occurred_before = now - timedelta(days=minimum_age_in_days)
60
61
    num_deleted = user_event_service.delete_user_login_events(occurred_before)
62
63
    flash_success(
64
        gettext(
65
            'Deleted %(num_deleted)s login events older than %(minimum_age_in_days)s days.',
66
            num_deleted=num_deleted,
67
            minimum_age_in_days=minimum_age_in_days,
68
        )
69
    )
70
71
72 1
@blueprint.post('/delete_old_verification_tokens')
73 1
@permission_required(AdminPermission.access)
74 1
@respond_no_content
75
def delete_old_verification_tokens():
76
    """Delete verification tokens older than a given number of days."""
77
    now = datetime.utcnow()
78
    minimum_age_in_days = 7
79
    created_before = now - timedelta(days=minimum_age_in_days)
80
81
    num_deleted = verification_token_service.delete_old_tokens(created_before)
82
83
    flash_success(
84
        gettext(
85
            'Deleted %(num_deleted)s verification tokens older than %(minimum_age_in_days)s days.',
86
            num_deleted=num_deleted,
87
            minimum_age_in_days=minimum_age_in_days,
88
        )
89
    )
90