-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarningquiet.py.txt
106 lines (73 loc) · 4.59 KB
/
warningquiet.py.txt
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
101
102
103
104
105
106
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/scripts/quiet_users/"
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)
# following read saved_state.json where a pair username/timestamp has been stored.
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: # account for the possibility that someone never logged in
timelapse = (now - user.last_login.timestamp())/86400
else:
timelapse = (now - user.date_joined.timestamp())/86400
if notified_user_list.get(user.username, None) != None:
notification_timelapse = (now - notified_user_list.get(user.username, None))/86400 # username/datetimestamp was found in save_state.json
else:
notification_timelapse = -1 # username/datetimestamp was NOT found in save_state.json; will trigger sending of email
if timelapse > 180.0:
if notification_timelapse > 30:
#============== Deactivate user ================
user.is_active = False
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)
elif notification_timelapse == -1:
#============ Send notification ===========
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"
email_body = email_int.format(user.get_short_name())
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())
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
else:
# Do nothing. The clock is ticking on the 30 days notice
pass
else:
notified_user_list.pop(user.username, None) # Remove notification flag in saved_state.json
# ================== Envoyer courriel à l'administrateur de geoportal ===================
notified_user_emailbody += "\n\nTotal: " + str(notified_user_nb)
deactivated_user_emailbody += "\n\nTotal: " + str(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))