Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
5882a89c
Commit
5882a89c
authored
Aug 08, 2012
by
Victor Shnayder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add management command to create random users
* required some refactoring in create_account() view
parent
2005492f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
48 deletions
+103
-48
common/djangoapps/student/management/commands/create_random_users.py
+36
-0
common/djangoapps/student/views.py
+67
-48
No files found.
common/djangoapps/student/management/commands/create_random_users.py
0 → 100644
View file @
5882a89c
##
## A script to create some dummy users
from
django.core.management.base
import
BaseCommand
from
django.conf
import
settings
from
django.contrib.auth.models
import
User
from
student.models
import
UserProfile
,
CourseEnrollment
from
student.views
import
_do_create_account
,
get_random_post_override
def
create
(
n
,
course_id
):
"""Create n users, enrolling them in course_id if it's not None"""
for
i
in
range
(
n
):
(
user
,
user_profile
,
_
)
=
_do_create_account
(
get_random_post_override
())
if
course_id
is
not
None
:
CourseEnrollment
.
objects
.
create
(
user
=
user
,
course_id
=
course_id
)
class
Command
(
BaseCommand
):
help
=
"""Create N new users, with random parameters.
Usage: create_random_users.py N [course_id_to_enroll_in].
Examples:
create_random_users.py 1
create_random_users.py 10 MITx/6.002x/2012_Fall
create_random_users.py 100 HarvardX/CS50x/2012
"""
def
handle
(
self
,
*
args
,
**
options
):
if
len
(
args
)
<
1
or
len
(
args
)
>
2
:
print
Command
.
help
return
n
=
int
(
args
[
0
])
course_id
=
args
[
1
]
if
len
(
args
)
==
2
else
None
create
(
n
,
course_id
)
common/djangoapps/student/views.py
View file @
5882a89c
...
...
@@ -278,6 +278,58 @@ def change_setting(request):
return
HttpResponse
(
json
.
dumps
({
'success'
:
True
,
'location'
:
up
.
location
,
}))
def
_do_create_account
(
post_vars
):
"""
Given cleaned post variables, create the User and UserProfile objects, as well as the
registration for this user.
Returns a tuple (User, UserProfile, Registration).
Note: this function is also used for creating test users.
"""
u
=
User
(
username
=
post_vars
[
'username'
],
email
=
post_vars
[
'email'
],
is_active
=
False
)
u
.
set_password
(
post_vars
[
'password'
])
r
=
Registration
()
# TODO: Rearrange so that if part of the process fails, the whole process fails.
# Right now, we can have e.g. no registration e-mail sent out and a zombie account
try
:
u
.
save
()
except
IntegrityError
:
# Figure out the cause of the integrity error
if
len
(
User
.
objects
.
filter
(
username
=
post_vars
[
'username'
]))
>
0
:
js
[
'value'
]
=
"An account with this username already exists."
js
[
'field'
]
=
'username'
return
HttpResponse
(
json
.
dumps
(
js
))
if
len
(
User
.
objects
.
filter
(
email
=
post_vars
[
'email'
]))
>
0
:
js
[
'value'
]
=
"An account with this e-mail already exists."
js
[
'field'
]
=
'email'
return
HttpResponse
(
json
.
dumps
(
js
))
raise
r
.
register
(
u
)
up
=
UserProfile
(
user
=
u
)
up
.
name
=
post_vars
[
'name'
]
up
.
level_of_education
=
post_vars
.
get
(
'level_of_education'
)
up
.
gender
=
post_vars
.
get
(
'gender'
)
up
.
mailing_address
=
post_vars
.
get
(
'mailing_address'
)
up
.
goals
=
post_vars
.
get
(
'goals'
)
try
:
up
.
year_of_birth
=
int
(
post_vars
[
'year_of_birth'
])
except
(
ValueError
,
KeyError
):
up
.
year_of_birth
=
None
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
try
:
up
.
save
()
except
Exception
:
log
.
exception
(
"UserProfile creation failed for user {0}."
.
format
(
u
.
id
))
return
(
u
,
up
,
r
)
@ensure_csrf_cookie
def
create_account
(
request
,
post_override
=
None
):
...
...
@@ -349,47 +401,8 @@ def create_account(request, post_override=None):
js
[
'field'
]
=
'username'
return
HttpResponse
(
json
.
dumps
(
js
))
u
=
User
(
username
=
post_vars
[
'username'
],
email
=
post_vars
[
'email'
],
is_active
=
False
)
u
.
set_password
(
post_vars
[
'password'
])
r
=
Registration
()
# TODO: Rearrange so that if part of the process fails, the whole process fails.
# Right now, we can have e.g. no registration e-mail sent out and a zombie account
try
:
u
.
save
()
except
IntegrityError
:
# Figure out the cause of the integrity error
if
len
(
User
.
objects
.
filter
(
username
=
post_vars
[
'username'
]))
>
0
:
js
[
'value'
]
=
"An account with this username already exists."
js
[
'field'
]
=
'username'
return
HttpResponse
(
json
.
dumps
(
js
))
if
len
(
User
.
objects
.
filter
(
email
=
post_vars
[
'email'
]))
>
0
:
js
[
'value'
]
=
"An account with this e-mail already exists."
js
[
'field'
]
=
'email'
return
HttpResponse
(
json
.
dumps
(
js
))
raise
r
.
register
(
u
)
up
=
UserProfile
(
user
=
u
)
up
.
name
=
post_vars
[
'name'
]
up
.
level_of_education
=
post_vars
.
get
(
'level_of_education'
)
up
.
gender
=
post_vars
.
get
(
'gender'
)
up
.
mailing_address
=
post_vars
.
get
(
'mailing_address'
)
up
.
goals
=
post_vars
.
get
(
'goals'
)
try
:
up
.
year_of_birth
=
int
(
post_vars
[
'year_of_birth'
])
except
(
ValueError
,
KeyError
):
up
.
year_of_birth
=
None
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
try
:
up
.
save
()
except
Exception
:
log
.
exception
(
"UserProfile creation failed for user {0}."
.
format
(
u
.
id
))
# Ok, looks like everything is legit. Create the account.
(
u
,
up
,
r
)
=
_do_create_account
(
post_vars
)
d
=
{
'name'
:
post_vars
[
'name'
],
'key'
:
r
.
activation_key
,
...
...
@@ -437,24 +450,30 @@ def create_account(request, post_override=None):
return
HttpResponse
(
json
.
dumps
(
js
),
mimetype
=
"application/json"
)
def
create_random_account
(
create_account_function
):
def
get_random_post_override
():
"""
Return a dictionary suitable for passing to post_vars of _do_create_account or post_override
of create_account, with random user info.
"""
def
id_generator
(
size
=
6
,
chars
=
string
.
ascii_uppercase
+
string
.
ascii_lowercase
+
string
.
digits
):
return
''
.
join
(
random
.
choice
(
chars
)
for
x
in
range
(
size
))
def
inner_create_random_account
(
request
):
post_override
=
{
'username'
:
"random_"
+
id_generator
(),
return
{
'username'
:
"random_"
+
id_generator
(),
'email'
:
id_generator
(
size
=
10
,
chars
=
string
.
ascii_lowercase
)
+
"_dummy_test@mitx.mit.edu"
,
'password'
:
id_generator
(),
'location'
:
id_generator
(
size
=
5
,
chars
=
string
.
ascii_uppercase
),
'name'
:
id_generator
(
size
=
5
,
chars
=
string
.
ascii_lowercase
)
+
" "
+
id_generator
(
size
=
7
,
chars
=
string
.
ascii_lowercase
),
'name'
:
(
id_generator
(
size
=
5
,
chars
=
string
.
ascii_lowercase
)
+
" "
+
id_generator
(
size
=
7
,
chars
=
string
.
ascii_lowercase
)
),
'honor_code'
:
u'true'
,
'terms_of_service'
:
u'true'
,
}
return
create_account_function
(
request
,
post_override
=
post_override
)
def
create_random_account
(
create_account_function
):
def
inner_create_random_account
(
request
):
return
create_account_function
(
request
,
post_override
=
get_random_post_override
())
return
inner_create_random_account
# TODO (vshnayder): do we need GENERATE_RANDOM_USER_CREDENTIALS for anything?
if
settings
.
GENERATE_RANDOM_USER_CREDENTIALS
:
create_account
=
create_random_account
(
create_account
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment