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
3e2cf187
Commit
3e2cf187
authored
Oct 23, 2012
by
Carlos Andrés Rocha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add some extra console commands for handling user permissions
parent
be947770
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
22 deletions
+113
-22
common/djangoapps/student/management/commands/add_to_group.py
+62
-0
common/djangoapps/student/management/commands/set_staff.py
+23
-13
lms/djangoapps/django_comment_client/management/commands/assign_role.py
+28
-9
No files found.
common/djangoapps/student/management/commands/add_to_group.py
0 → 100644
View file @
3e2cf187
from
optparse
import
make_option
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.contrib.auth.models
import
User
,
Group
class
Command
(
BaseCommand
):
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--list'
,
action
=
'store_true'
,
dest
=
'list'
,
default
=
False
,
help
=
'List available groups'
),
make_option
(
'--create'
,
action
=
'store_true'
,
dest
=
'create'
,
default
=
False
,
help
=
'Create the group if it does not exist'
),
make_option
(
'--remove'
,
action
=
'store_true'
,
dest
=
'remove'
,
default
=
False
,
help
=
'Remove the user from the group instead of adding it'
),
)
args
=
'<user|email> <group>'
help
=
'Add a user to a group'
def
print_groups
(
self
):
print
'Groups available:'
for
group
in
Group
.
objects
.
all
()
.
distinct
():
print
' '
,
group
.
name
def
handle
(
self
,
*
args
,
**
options
):
if
options
[
'list'
]:
self
.
print_groups
()
return
if
len
(
args
)
!=
2
:
raise
CommandError
(
'Usage is add_to_group {0}'
.
format
(
self
.
args
))
name_or_email
,
group_name
=
args
if
'@'
in
name_or_email
:
user
=
User
.
objects
.
get
(
email
=
name_or_email
)
else
:
user
=
User
.
objects
.
get
(
username
=
name_or_email
)
try
:
group
=
Group
.
objects
.
get
(
name
=
group_name
)
except
Group
.
DoesNotExist
:
if
options
[
'create'
]:
group
=
Group
(
name
=
group_name
)
group
.
save
()
else
:
raise
CommandError
(
'Group {} does not exist'
.
format
(
group_name
))
if
options
[
'remove'
]:
user
.
groups
.
remove
(
group
)
else
:
user
.
groups
.
add
(
group
)
print
'Success!'
common/djangoapps/student/management/commands/set_staff.py
View file @
3e2cf187
from
optparse
import
make_option
from
django.contrib.auth.models
import
User
from
django.core.management.base
import
BaseCommand
,
CommandError
import
re
class
Command
(
BaseCommand
):
args
=
'<user/email user/email ...>'
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--unset'
,
action
=
'store_true'
,
dest
=
'unset'
,
default
=
False
,
help
=
'Set is_staff to False instead of True'
),
)
args
=
'<user|email> [user|email ...]>'
help
=
"""
This command will set isstaff to true for one or more users.
This command will set is
_
staff to true for one or more users.
Lookup by username or email address, assumes usernames
do not look like email addresses.
"""
def
handle
(
self
,
*
args
,
**
kwargs
):
def
handle
(
self
,
*
args
,
**
options
):
if
len
(
args
)
<
1
:
print
Command
.
help
return
raise
CommandError
(
'Usage is set_staff {0}'
.
format
(
self
.
args
))
for
user
in
args
:
if
re
.
match
(
'[^@]+@[^@]+
\
.[^@]+'
,
user
):
try
:
v
=
User
.
objects
.
get
(
email
=
user
)
except
:
raise
CommandError
(
"User {0} does not exist"
.
format
(
user
))
raise
CommandError
(
"User {0} does not exist"
.
format
(
user
))
else
:
try
:
v
=
User
.
objects
.
get
(
username
=
user
)
except
:
raise
CommandError
(
"User {0} does not exist"
.
format
(
user
))
raise
CommandError
(
"User {0} does not exist"
.
format
(
user
))
if
options
[
'unset'
]:
v
.
is_staff
=
False
else
:
v
.
is_staff
=
True
v
.
is_staff
=
True
v
.
save
()
print
'Success!'
lms/djangoapps/django_comment_client/management/commands/assign_role.py
View file @
3e2cf187
from
optparse
import
make_option
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django_comment_client.models
import
Permission
,
Role
from
django_comment_client.models
import
Role
from
django.contrib.auth.models
import
User
class
Command
(
BaseCommand
):
args
=
'user role course_id'
help
=
'Assign a role to a user'
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--remove'
,
action
=
'store_true'
,
dest
=
'remove'
,
default
=
False
,
help
=
'Remove the role instead of adding it'
),
)
args
=
'<user|email> <role> <course_id>'
help
=
'Assign a discussion forum role to a user '
def
handle
(
self
,
*
args
,
**
options
):
role
=
Role
.
objects
.
get
(
name
=
args
[
1
],
course_id
=
args
[
2
])
if
len
(
args
)
!=
3
:
raise
CommandError
(
'Usage is assign_role {0}'
.
format
(
self
.
args
))
name_or_email
,
role
,
course_id
=
args
role
=
Role
.
objects
.
get
(
name
=
role
,
course_id
=
course_id
)
if
'@'
in
name_or_email
:
user
=
User
.
objects
.
get
(
email
=
name_or_email
)
else
:
user
=
User
.
objects
.
get
(
username
=
name_or_email
)
if
'@'
in
args
[
0
]:
user
=
User
.
objects
.
get
(
email
=
args
[
0
]
)
if
options
[
'remove'
]:
user
.
roles
.
remove
(
role
)
else
:
user
=
User
.
objects
.
get
(
username
=
args
[
0
]
)
user
.
roles
.
add
(
role
)
user
.
roles
.
add
(
role
)
\ No newline at end of file
print
'Success!'
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