Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-analytics-data-api
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-analytics-data-api
Commits
7513f248
Commit
7513f248
authored
Jun 24, 2014
by
Carlos Andrés Rocha
Committed by
Gerrit Code Review
Jun 24, 2014
Browse files
Options
Browse Files
Download
Plain Diff
Merge "Add command to set the API token key for a specific user."
parents
4d889f49
c7adb7a1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
1 deletions
+83
-1
Makefile
+1
-1
analyticsdata/management/__init__.py
+0
-0
analyticsdata/management/commands/__init__.py
+0
-0
analyticsdata/management/commands/set_api_key.py
+82
-0
No files found.
Makefile
View file @
7513f248
...
@@ -36,7 +36,7 @@ quality:
...
@@ -36,7 +36,7 @@ quality:
pylint
--rcfile
=
.pylintrc
$(PACKAGES)
pylint
--rcfile
=
.pylintrc
$(PACKAGES)
# Ignore module level docstrings and all test files
# Ignore module level docstrings and all test files
pep257
--ignore
=
D100
--match
=
'(?!test).*py'
$(PACKAGES)
pep257
--ignore
=
D100
,D203
--match
=
'(?!test).*py'
$(PACKAGES)
syncdb
:
syncdb
:
$
(
foreach db_name,
$(DATABASES)
,./manage.py syncdb
--migrate
--database
=
$(db_name)
;
)
$
(
foreach db_name,
$(DATABASES)
,./manage.py syncdb
--migrate
--database
=
$(db_name)
;
)
analyticsdata/management/__init__.py
0 → 100644
View file @
7513f248
analyticsdata/management/commands/__init__.py
0 → 100644
View file @
7513f248
analyticsdata/management/commands/set_api_key.py
0 → 100644
View file @
7513f248
"""A command to set the API key for a user using when using TokenAuthentication."""
from
optparse
import
make_option
from
django.contrib.auth.models
import
User
from
django.core.exceptions
import
ObjectDoesNotExist
from
django.core.management.base
import
BaseCommand
,
CommandError
from
rest_framework.authtoken.models
import
Token
class
Command
(
BaseCommand
):
"""A command to set the API key for a user using when using TokenAuthentication."""
help
=
'Set the API key for the specified user.'
args
=
'<username> <api_key>'
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--create-user'
,
action
=
'store_true'
,
default
=
False
,
help
=
"Create a user if it doesn't exists"
),
make_option
(
'--delete-key'
,
action
=
'store_true'
,
default
=
False
,
help
=
"Delete API key for user"
)
)
def
handle
(
self
,
*
args
,
**
options
):
"""Default Django BaseCommand handler."""
if
options
[
'delete_key'
]:
self
.
_delete_user
(
*
args
)
else
:
self
.
_set_api_key
(
*
args
,
**
options
)
def
_delete_user
(
self
,
*
args
):
if
len
(
args
)
!=
1
:
raise
CommandError
(
'Invalid or misssing arguments'
)
username
=
args
[
0
]
tokens
=
Token
.
objects
.
filter
(
user__username
=
username
)
if
tokens
.
exists
():
tokens
.
delete
()
self
.
stdout
.
write
(
'Removed API key for user: <{0}>
\n
'
.
format
(
username
))
else
:
self
.
stdout
.
write
(
'Unknown user or user without an API key: <{0}>
\n
'
.
format
(
username
))
def
_set_api_key
(
self
,
*
args
,
**
options
):
if
len
(
args
)
!=
2
:
raise
CommandError
(
'Invalid or missing arguments'
)
username
,
key
=
args
[
0
],
args
[
1
]
user
=
self
.
_get_user
(
username
,
options
[
'create_user'
])
self
.
_set_token
(
user
,
key
)
def
_get_user
(
self
,
username
,
create_user
=
False
):
if
create_user
:
user
,
created
=
User
.
objects
.
get_or_create
(
username
=
username
)
if
created
:
self
.
stdout
.
write
(
'Created user: <{0}>
\n
'
.
format
(
user
))
else
:
try
:
user
=
User
.
objects
.
get
(
username
=
username
)
except
ObjectDoesNotExist
:
raise
CommandError
(
'Unknown user: <{0}>'
.
format
(
username
))
return
user
def
_set_token
(
self
,
user
,
key
):
# Check that no other user has the same key
tokens
=
Token
.
objects
.
filter
(
key
=
key
)
if
tokens
.
exists
()
and
tokens
[
0
]
.
user
!=
user
:
raise
CommandError
(
'Key already in use.'
)
# Get and update the user key
_
,
created
=
Token
.
objects
.
get_or_create
(
user
=
user
)
count
=
Token
.
objects
.
filter
(
user
=
user
)
.
update
(
key
=
key
)
if
count
:
action
=
'Created'
if
created
else
'Updated'
self
.
stdout
.
write
(
'{0} API key for user: <{1}>
\n
'
.
format
(
action
,
user
))
else
:
raise
CommandError
(
'Something went wrong.'
)
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