-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.py
55 lines (53 loc) · 1.8 KB
/
user.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
# Creates new auth.User object in the database.
#
# Any parameters which MAY have spaces or restricted characters should
# be passed using single quotes-i.e. '$password'
# Usage:
# python user.py user_id username password [first_name last_name email
#
# Arguments:
# user_id: Row id of the user record or -1 if new
# username The string the user will log in with
# password The unencrypted password
# Optional arguments(Shown in brackets above)
# first_name The user given name
# last_name The user family name
# email The user email address
# Returns: The row id of the new auth.User as a long value if successful or
# -1 if the creation was unsuccessful
import sys, os
sys.path.append('/opt/sana/sana.mds')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mds.settings'
from django.conf import settings
from django.contrib.auth.models import User
result = -1
messages = []
try:
user_id = long(sys.argv[1])
username = sys.argv[2]
password = sys.argv[3]
user = None
if user_id > 0:
user = User.objects.get(id=1)
# TODO Update fields so this can act as an edit script
else:
user = User.objects.create_user(username, password=password)
# set [optional] fields here
try:
user.first_name = sys.argv[3]
except Exception as e:
messages.append("'%s' -> '%s'" % ("first_name", e))
try:
user.last_name = sys.argv[4]
except Exception as e:
messages.append("'%s' -> '%s'" % ("last_name", e))
try:
user.email = sys.argv[5]
except Exception as e:
messages.append("'%s' -> '%s'" % ("email", e))
user.save()
result = User.objects.get(username=username).id
except Exception as e:
messages.append("'%s' -> '%s'" % ("email", e))
message = messages.join(":")
print('{}'.format(result).rstrip('L'))