-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_warning_quiet_users.py
100 lines (71 loc) · 4.04 KB
/
send_warning_quiet_users.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from datetime import datetime
from django.core.mail import send_mail
from json import dumps, loads
# import os
User = get_user_model()
now = datetime.now().timestamp()
gn_action = ""
#path, filename = os.path.split(os.path.abspath(__file__))
path = "/opt/geonode_custom/neec_geoportal/"
class Command(BaseCommand):
help = 'Send users a reminded to log once in a while'
def handle(self, *args, **options):
notified_user_emailbody = "\n\nCOURRIEL ENVOYÉ\:\n\n"
deactivated_user_emailbody = "\n\nACCÈS DÉSACTIVÉ\:\n\n"
notified_user_nb = 0
deactivated_user_nb = 0
#all_users = User.objects.filter(is_active=True)
all_users = User.objects.filter(username="bouliannev") # Test function
with open(path + 'saved_state.json', 'r') as fich:
chain = fich.read()
if not chain:
notified_user_list = {}
else:
notified_user_list = loads(chain)
for user in all_users:
if user.last_login != None:
timelapse = (now - user.last_login.timestamp())/86400
else:
timelapse = (now - user.date_joined.timestamp())/86400
if notification_timelapse != None:
notification_timelapse = (now - notified_user_list.get(user.username, None))/86400
else:
notification_timelapse = -1
if timelapse > 180.0:
if notification_timelapse > 30:
#----------- Deactivate user ----------
# TEST user.is_active = False
# TEST user.save
deactivated_user_emailbody += "\nL'accès de" + user.username + " a été désactivé.\n"
deactivated_user_nb += 1
notified_user_list.pop(user.username, None)
#gn_action = "Inactivated"
else:
if user.language == "fr":
with open(path + 'fr_email_quiet_users.txt','r', encoding='utf8') as f:
email_int = f.read()
email_subject = "Désactivation de votre compte géportail du CNUE dans 30 jours si vous ne vous connectez pas"
else:
with open(path + 'en_email_quiet_users.txt','r', encoding='utf8') as f:
email_int = f.read()
email_subject = "Deactivation of NECC geoportal account in 30 days if there is no login"
email_body = email_int.format(user.get_short_name())
#TEST user.email_user(email_subject, email_body, "[email protected]")
notified_user_emailbody += "\nUn courriel a été envoyé à " + user.username + " (" + user.email + ")\n"
notified_user_list[user.username] = now
notified_user_nb += 1
#gn_action = "Email sent"
else:
notified_user_list.pop(user.username, None) # Remove 180 notification flag
#gn_action = "None"
# Envoyer courriel à l'administrateur de geoportal
notified_user_emailbody += "\n\nTotal: " + notified_user_nb
deactivated_user_emailbody += "\n\nTotal:" + deactivated_user_nb
admin_email_body = "Actions entreprises par le script de vérification des usagers ne s'étant pas connectés depuis plus de 180 jours\n\n" + notified_user_emailbody + deactivated_user_emailbody
admin_email_subject = "Rapport d'action sur le statuts des usagers sans connection depuis 180 jours"
send_mail(admin_email_subject, admin_email_body, "[email protected]", ["[email protected]"])
# Inscrire dans fichier l'état des notifications.
with open(path + 'saved_state.json', mode='w') as fich:
fich.write(dumps(notified_user_list))