1
|
import random
|
2
|
|
3
|
from django.contrib.auth.hashers import make_password
|
4
|
|
5
|
from .models import LocalAccount
|
6
|
|
7
|
|
8
|
def create_password():
|
9
|
return ''.join([random.choice('23456789ABCDEFGHJLMNPQRSTUVWXZabcdefghjkmnpqrstuvwxyz')
|
10
|
for x in range(random.randint(6,9))])
|
11
|
|
12
|
|
13
|
def create_user(data):
|
14
|
if not data['password']:
|
15
|
data['password'] = create_password()
|
16
|
try:
|
17
|
user = LocalAccount.objects.create(**data)
|
18
|
return user
|
19
|
except:
|
20
|
return False
|
21
|
|
22
|
def create_or_update_users(data):
|
23
|
created = 0
|
24
|
updated = 0
|
25
|
for user in data:
|
26
|
try:
|
27
|
account = LocalAccount.objects.get(username=user['username'])
|
28
|
if not user['password']:
|
29
|
del user['password']
|
30
|
account.__dict__.update(user)
|
31
|
account.save()
|
32
|
updated += 1
|
33
|
except LocalAccount.DoesNotExist:
|
34
|
if create_user(user):
|
35
|
created += 1
|
36
|
return created, updated
|