Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-ora2
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-ora2
Commits
264b3d1e
Commit
264b3d1e
authored
Jan 29, 2014
by
Stephen Sanchez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Quick changes to clean up api using serializer save
parent
4ae638e9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
16 deletions
+15
-16
apps/submissions/api.py
+10
-12
apps/submissions/tests/test_api.py
+5
-4
No files found.
apps/submissions/api.py
View file @
264b3d1e
...
...
@@ -137,13 +137,16 @@ def create_submission(student_item_dict, answer, submitted_at=None,
model_kwargs
[
"submitted_at"
]
=
submitted_at
try
:
# Serializer validation requires the student item primary key, rather
# than the student item model itself. Create a copy of the submission
# kwargs and replace the student item model with it's primary key.
validation_data
=
model_kwargs
.
copy
()
validation_data
[
"student_item"
]
=
student_item_model
.
pk
submission_serializer
=
SubmissionSerializer
(
data
=
validation_data
)
submission_serializer
.
is_valid
()
if
submission_serializer
.
errors
:
if
not
submission_serializer
.
is_valid
():
raise
SubmissionRequestError
(
submission_serializer
.
errors
)
submission
=
Submission
.
objects
.
create
(
**
model_kwargs
)
submission_serializer
.
save
()
return
submission_serializer
.
data
except
DatabaseError
:
error_message
=
u"An error occurred while creating submission {} for student item: {}"
.
format
(
model_kwargs
,
...
...
@@ -152,8 +155,6 @@ def create_submission(student_item_dict, answer, submitted_at=None,
logger
.
exception
(
error_message
)
raise
SubmissionInternalError
(
error_message
)
return
SubmissionSerializer
(
submission
)
.
data
def
get_submissions
(
student_item_dict
,
limit
=
None
):
"""Retrieves the submissions for the specified student item,
...
...
@@ -264,17 +265,14 @@ def _get_or_create_student_item(student_item_dict):
"""
try
:
try
:
student_item_model
=
StudentItem
.
objects
.
get
(
**
student_item_dict
)
return
StudentItem
.
objects
.
get
(
**
student_item_dict
)
except
StudentItem
.
DoesNotExist
:
student_item_serializer
=
StudentItemSerializer
(
data
=
student_item_dict
)
student_item_serializer
.
is_valid
()
if
student_item_serializer
.
errors
:
student_item_serializer
=
StudentItemSerializer
(
data
=
student_item_dict
)
if
not
student_item_serializer
.
is_valid
():
raise
SubmissionRequestError
(
student_item_serializer
.
errors
)
student_item_model
=
StudentItem
.
objects
.
create
(
**
student_item_dict
)
return
student_item_serializer
.
save
(
)
except
DatabaseError
:
error_message
=
u"An error occurred creating student item: {}"
.
format
(
student_item_dict
)
logger
.
exception
(
error_message
)
raise
SubmissionInternalError
(
error_message
)
return
student_item_model
apps/submissions/tests/test_api.py
View file @
264b3d1e
...
...
@@ -7,7 +7,8 @@ from nose.tools import raises
from
mock
import
patch
from
submissions.api
import
create_submission
,
get_submissions
,
SubmissionRequestError
,
SubmissionInternalError
from
submissions.models
import
Submission
,
StudentItem
from
submissions.models
import
Submission
from
submissions.serializers
import
StudentItemSerializer
STUDENT_ITEM
=
dict
(
student_id
=
"Tim"
,
...
...
@@ -73,10 +74,10 @@ class TestApi(TestCase):
mock_filter
.
side_effect
=
DatabaseError
(
"Bad things happened"
)
create_submission
(
STUDENT_ITEM
,
ANSWER_ONE
)
@patch.object
(
StudentItem
.
objects
,
'creat
e'
)
@patch.object
(
StudentItem
Serializer
,
'sav
e'
)
@raises
(
SubmissionInternalError
)
def
test_create_student_item_validation
(
self
,
mock_
creat
e
):
mock_
creat
e
.
side_effect
=
DatabaseError
(
"Bad things happened"
)
def
test_create_student_item_validation
(
self
,
mock_
sav
e
):
mock_
sav
e
.
side_effect
=
DatabaseError
(
"Bad things happened"
)
create_submission
(
STUDENT_ITEM
,
ANSWER_ONE
)
def
test_unicode_enforcement
(
self
):
...
...
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