forked from ISCPIF/python-gargantext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_accounts.py
122 lines (95 loc) · 3.96 KB
/
init_accounts.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import sys
import os
dirname = os.path.dirname(os.path.realpath(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gargantext.settings")
# initialize Django application
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from django.contrib.auth import password_validation
from django.contrib.auth.hashers import ( check_password
, is_password_usable
, make_password
)
from django.db import models
from django.utils.crypto import get_random_string, salted_hmac
# retrieve Django models
import django.apps
django_models = django.apps.apps.get_models()
django_models_names = set(model._meta.db_table for model in django_models)
from gargantext.util.db import session
from gargantext.models.users import User
from django.core.mail import send_mail
from gargantext.settings import BASE_URL
def make_random_password(length=10
, allowed_chars='abcdefghjkmnpqrstuvwxyz'
'ABCDEFGHJKLMNPQRSTUVWXYZ'
'0123456789'):
"""
Generate a random password with the given length and given
allowed_chars. The default value of allowed_chars does not have "I" or
"O" or letters and digits that look similar -- just to avoid confusion.
(source: django/contrib/auth)
"""
return get_random_string(length, allowed_chars)
def notify_user(username, email, password):
message = '''
Bonjour,
votre compte Gargantext vient d'être mis à jour.
Vous pouvez vous connecter à l'adresse:
http://%s
Avec les identifiants suivants:
Votre login est: %s
Votre mot de passe est : %s
La nouvelle version de Gargantext sort très prochainement.
Vous êtes actuellement sur la version de développement, vos retours
seront précieux pour stabiliser la plateforme: merci d'avance!
Nous restons à votre disposition pour tout complément d'information.
Cordialement
--
L'équipe de Gargantext (CNRS)
''' % (BASE_URL,username, password)
send_mail('[Gargantext] Votre accès à la plateforme', message, 'team@gargantext.org', [email], fail_silently=False )
# add option for mass sending email
def create_user(username, email, user=None, password=None, active=False, notify=True):
if user is None:
user = User()
user.username = username
user.email = email
user.is_active = True
if password is None or password == "":
password = make_random_password()
user.password = make_password(password)
session.add(user)
session.commit()
if notify == True:
notify_user(username, email, password)
return user
def delete_user(username):
session.query(User).filter(User.username == username).delete()
def active_user(username, active=True):
'''
To get inactive, active=False
'''
user = session.query(User).filter(User.username == username).first()
user.is_active = True
user.save()
def mass_account_creation(fichier=None,init=False):
if fichier is None:
fichier = "/tmp/comptes.csv"
accounts = open(fichier, "r")
for line in accounts.readlines():
username, email, password, fin = line.split(',')
try:
user = session.query(User).filter(User.username == username).first()
print("User %s does exist already" % (username))
if init == True:
create_user(username, email, user=user, password=password, active=True, notify=True)
print("User %s updated" % (username))
except:
print("User %s does not exist already" % (username))
create_user(username, email, password=password, active=True, notify=True)
#delete_user(username)
accounts.close()
if __name__ == "__main__":
mass_account_creation(fichier=sys.argv[1], init=True)