Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
85af1d88
Commit
85af1d88
authored
12 years ago
by
ichuang
Browse files
Options
Downloads
Patches
Plain Diff
utility scripts to create new users, and staff_* groups for courses
parent
b8ae026c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
utility-scripts/create_groups.py
+32
-0
32 additions, 0 deletions
utility-scripts/create_groups.py
utility-scripts/create_user.py
+137
-0
137 additions, 0 deletions
utility-scripts/create_user.py
with
169 additions
and
0 deletions
utility-scripts/create_groups.py
0 → 100644
+
32
−
0
View file @
85af1d88
#!/usr/bin/python
#
# File: create_groups.py
# Date: 04-Aug-12
# Author: I. Chuang <ichuang@mit.edu>
#
# Create all staff_* groups for classes in data directory.
import
os
,
sys
,
string
,
re
sys
.
path
.
append
(
os
.
path
.
abspath
(
'
.
'
))
os
.
environ
[
'
DJANGO_SETTINGS_MODULE
'
]
=
'
lms.envs.dev
'
from
lms.envs.dev
import
*
from
django.conf
import
settings
from
django.contrib.auth.models
import
User
,
Group
from
path
import
path
data_dir
=
settings
.
DATA_DIR
print
"
data_dir = %s
"
%
data_dir
for
course_dir
in
os
.
listdir
(
data_dir
):
# print course_dir
if
not
os
.
path
.
isdir
(
path
(
data_dir
)
/
course_dir
):
continue
gname
=
'
staff_%s
'
%
course_dir
if
Group
.
objects
.
filter
(
name
=
gname
):
print
"
group exists for %s
"
%
gname
continue
g
=
Group
(
name
=
gname
)
g
.
save
()
print
"
created group %s
"
%
gname
This diff is collapsed.
Click to expand it.
utility-scripts/create_user.py
0 → 100644
+
137
−
0
View file @
85af1d88
#!/usr/bin/python
#
# File: create_user.py
# Date: 04-Aug-12
# Author: I. Chuang <ichuang@mit.edu>
#
# Create user. Prompt for groups and ExternalAuthMap
import
os
,
sys
,
string
,
re
import
datetime
from
getpass
import
getpass
import
json
import
readline
sys
.
path
.
append
(
os
.
path
.
abspath
(
'
.
'
))
os
.
environ
[
'
DJANGO_SETTINGS_MODULE
'
]
=
'
lms.envs.dev
'
from
lms.envs.dev
import
*
from
student.models
import
UserProfile
,
Registration
from
external_auth.models
import
ExternalAuthMap
from
django.contrib.auth.models
import
User
,
Group
from
random
import
choice
class
MyCompleter
(
object
):
# Custom completer
def
__init__
(
self
,
options
):
self
.
options
=
sorted
(
options
)
def
complete
(
self
,
text
,
state
):
if
state
==
0
:
# on first trigger, build possible matches
if
text
:
# cache matches (entries that start with entered text)
self
.
matches
=
[
s
for
s
in
self
.
options
if
s
and
s
.
startswith
(
text
)]
else
:
# no text entered, all matches possible
self
.
matches
=
self
.
options
[:]
# return match indexed by state
try
:
return
self
.
matches
[
state
]
except
IndexError
:
return
None
def
GenPasswd
(
length
=
8
,
chars
=
string
.
letters
+
string
.
digits
):
return
''
.
join
([
choice
(
chars
)
for
i
in
range
(
length
)])
#-----------------------------------------------------------------------------
# main
while
True
:
uname
=
raw_input
(
'
username:
'
)
if
User
.
objects
.
filter
(
username
=
uname
):
print
"
username %s already taken
"
%
uname
else
:
break
while
True
:
email
=
raw_input
(
'
email:
'
)
if
User
.
objects
.
filter
(
email
=
email
):
print
"
email %s already taken
"
%
email
else
:
break
name
=
raw_input
(
'
Full name:
'
)
make_eamap
=
False
if
raw_input
(
'
Create MIT ExternalAuth? [n]
'
).
lower
()
==
'
y
'
:
if
not
email
.
endswith
(
'
@MIT.EDU
'
):
print
"
Failed - email must be @MIT.EDU
"
sys
.
exit
(
-
1
)
mit_domain
=
'
ssl:MIT
'
if
ExternalAuthMap
.
objects
.
filter
(
external_id
=
email
,
external_domain
=
mit_domain
):
print
"
Failed - email %s already exists as external_id
"
%
email
sys
.
exit
(
-
1
)
make_eamap
=
True
password
=
GenPasswd
(
12
)
else
:
while
True
:
password
=
getpass
()
password2
=
getpass
()
if
password
==
password2
:
break
print
"
Oops, passwords do not match, please retry
"
user
=
User
(
username
=
uname
,
email
=
email
,
is_active
=
True
)
user
.
set_password
(
password
)
try
:
user
.
save
()
except
IntegrityError
:
print
"
Oops, failed to create user %s, IntegrityError
"
%
user
raise
r
=
Registration
()
r
.
register
(
user
)
up
=
UserProfile
(
user
=
user
)
up
.
name
=
name
up
.
save
()
if
make_eamap
:
credentials
=
"
/C=US/ST=Massachusetts/O=Massachusetts Institute of Technology/OU=Client CA v1/CN=%s/emailAddress=%s
"
%
(
name
,
email
)
eamap
=
ExternalAuthMap
(
external_id
=
email
,
external_email
=
email
,
external_domain
=
mit_domain
,
external_name
=
name
,
internal_password
=
password
,
external_credentials
=
json
.
dumps
(
credentials
),
)
eamap
.
user
=
user
eamap
.
dtsignup
=
datetime
.
datetime
.
now
()
eamap
.
save
()
print
"
User %s created successfully!
"
%
user
if
not
raw_input
(
'
Add user %s to any groups? [n]
'
%
user
).
lower
()
==
'
y
'
:
sys
.
exit
(
0
)
print
"
Here are the groups available:
"
groups
=
[
str
(
g
.
name
)
for
g
in
Group
.
objects
.
all
()]
print
groups
completer
=
MyCompleter
(
groups
)
readline
.
set_completer
(
completer
.
complete
)
readline
.
parse_and_bind
(
'
tab: complete
'
)
while
True
:
gname
=
raw_input
(
"
Add group (tab to autocomplete, empty line to end):
"
)
if
not
gname
:
break
if
not
gname
in
groups
:
print
"
Unknown group %s
"
%
gname
continue
g
=
Group
.
objects
.
get
(
name
=
gname
)
user
.
groups
.
add
(
g
)
print
"
Added %s to group %s
"
%
(
user
,
g
)
print
"
Done!
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment