Skip to content

Commit 774f831

Browse files
authored
Merge pull request #966 from MTES-MCT/feat-inactive-admins
Ajout d'un cron pour alerter des comptes administrateurs inactifs
2 parents c1b7c9d + 18d9c47 commit 774f831

File tree

4 files changed

+44
-55
lines changed

4 files changed

+44
-55
lines changed

config/celery.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535

3636
app.conf.beat_schedule = {
37-
# Executes every Monday morning at 7:30 a.m.
3837
"alive-every-minute": {
3938
"task": "config.celery.log",
4039
"schedule": crontab(minute="*/15"),
@@ -57,6 +56,11 @@
5756
"queue": "long",
5857
},
5958
},
59+
"check-inactive-admins": {
60+
"task": "users.tasks.check_inactive_admins",
61+
# schedule to every monday at 8:00
62+
"schedule": crontab(minute="0", hour="8"),
63+
},
6064
}
6165

6266

users/tasks.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import logging
2+
from datetime import datetime, timedelta
3+
4+
from celery import shared_task
5+
6+
from utils.mattermost import DevMattermost
7+
8+
from .models import User
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
@shared_task
14+
def check_inactive_admins() -> int:
15+
users = User.objects.filter(
16+
is_superuser=True,
17+
last_login__lte=datetime.now().date() - timedelta(days=60),
18+
)
19+
20+
if users.count() > 10:
21+
markdown_message = f":warning: Nombre d'administratifs inactifs : {users.count()}\n"
22+
markdown_message += "Le nombre est trop elevé pour être affiché ici.\n"
23+
else:
24+
markdown_message = ":warning: Les administrateurs suivants n'ont pas été actifs depuis plus de 2 mois :\n"
25+
for user in users:
26+
markdown_message += (
27+
f"- {user.email} - {user.first_name} - {user.last_name} | Last login : {user.last_login}\n"
28+
)
29+
30+
mattermost = DevMattermost(msg=markdown_message)
31+
mattermost.send()
32+
33+
return users.count()

utils/mattermost.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ def send(self):
4343
return True
4444

4545

46-
class StartupSparte(Mattermost):
47-
"""Send message in startup-sparte, channel general"""
46+
class DevMattermost(Mattermost):
47+
"""Send message in dev channel"""
4848

4949
def __init__(self, *args, **kwargs):
50-
super().__init__(channel="startup-sparte", *args, **kwargs) # noqa: B026
50+
super().__init__(channel=settings.MATTER_DEV_CHANNEL, *args, **kwargs) # noqa: B026
5151

5252

53-
class SwannPrivate(Mattermost):
54-
"""Send a message to Swann direct channel"""
53+
class StartupSparte(Mattermost):
54+
"""Send message in startup-sparte, channel general"""
5555

5656
def __init__(self, *args, **kwargs):
57-
super().__init__(channel="@Swann", *args, **kwargs) # noqa: B026
57+
super().__init__(channel="startup-sparte", *args, **kwargs) # noqa: B026
5858

5959

6060
class Crisp(Mattermost):

utils/test_mattermost.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)