Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-notes-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-notes-api
Commits
271bc958
Commit
271bc958
authored
Jan 05, 2015
by
Oleg Marshev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use create.
parent
977c5c35
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
52 deletions
+40
-52
notesapi/v1/models.py
+12
-18
notesapi/v1/tests/test_models.py
+18
-24
notesapi/v1/tests/test_views.py
+3
-3
notesapi/v1/views.py
+7
-7
No files found.
notesapi/v1/models.py
View file @
271bc958
...
@@ -18,36 +18,30 @@ class Note(models.Model):
...
@@ -18,36 +18,30 @@ class Note(models.Model):
usage_id
=
models
.
CharField
(
max_length
=
255
,
help_text
=
"ID of XBlock where the text comes from"
)
usage_id
=
models
.
CharField
(
max_length
=
255
,
help_text
=
"ID of XBlock where the text comes from"
)
quote
=
models
.
TextField
(
default
=
""
)
quote
=
models
.
TextField
(
default
=
""
)
text
=
models
.
TextField
(
default
=
""
,
help_text
=
"Student's thoughts on the quote"
)
text
=
models
.
TextField
(
default
=
""
,
help_text
=
"Student's thoughts on the quote"
)
ranges
=
models
.
TextField
(
default
=
""
,
help_text
=
"JSON, describes position of quote in the source text"
)
ranges
=
models
.
TextField
(
help_text
=
"JSON, describes position of quote in the source text"
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
updated
=
models
.
DateTimeField
(
auto_now
=
True
)
updated
=
models
.
DateTimeField
(
auto_now
=
True
)
def
clean
(
self
,
note
):
@classmethod
def
create
(
cls
,
note_dict
):
"""
"""
C
lean the note object or raises a ValidationError
.
C
reate the note object
.
"""
"""
if
not
isinstance
(
note
,
dict
):
if
not
isinstance
(
note
_dict
,
dict
):
raise
ValidationError
(
'Note must be a dictionary.'
)
raise
ValidationError
(
'Note must be a dictionary.'
)
if
len
(
note
)
==
0
:
if
len
(
note
_dict
)
==
0
:
raise
ValidationError
(
'Note must have a body.'
)
raise
ValidationError
(
'Note must have a body.'
)
self
.
text
=
note
.
get
(
'text'
,
''
)
ranges
=
note_dict
.
get
(
'ranges'
,
list
())
self
.
quote
=
note
.
get
(
'quote'
,
''
)
try
:
self
.
course_id
=
note
[
'course_id'
]
self
.
usage_id
=
note
[
'usage_id'
]
if
not
self
.
user_id
:
self
.
user_id
=
note
[
'user'
]
except
KeyError
as
error
:
raise
ValidationError
(
'Note must have a course_id and usage_id and user_id.'
)
ranges
=
note
.
get
(
'ranges'
)
if
len
(
ranges
)
<
1
:
if
not
ranges
:
raise
ValidationError
(
'Note must contain at least one range.'
)
raise
ValidationError
(
'Note must contain at least one range.'
)
self
.
ranges
=
json
.
dumps
(
ranges
)
note_dict
[
'ranges'
]
=
json
.
dumps
(
ranges
)
note_dict
[
'user_id'
]
=
note_dict
.
pop
(
'user'
,
None
)
return
cls
(
**
note_dict
)
def
as_dict
(
self
):
def
as_dict
(
self
):
"""
"""
...
...
notesapi/v1/tests/test_models.py
View file @
271bc958
...
@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
...
@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
class
NoteTest
(
TestCase
):
class
NoteTest
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
note
=
{
self
.
note
_dict
=
{
"user"
:
u"test_user_id"
,
"user"
:
u"test_user_id"
,
"usage_id"
:
u"i4x://org/course/html/52aa9816425a4ce98a07625b8cb70811"
,
"usage_id"
:
u"i4x://org/course/html/52aa9816425a4ce98a07625b8cb70811"
,
"course_id"
:
u"org/course/run"
,
"course_id"
:
u"org/course/run"
,
...
@@ -22,40 +22,34 @@ class NoteTest(TestCase):
...
@@ -22,40 +22,34 @@ class NoteTest(TestCase):
],
],
}
}
def
test_c
lean
_valid_note
(
self
):
def
test_c
reate
_valid_note
(
self
):
note
=
Note
(
)
note
=
Note
.
create
(
self
.
note_dict
.
copy
()
)
note
.
clean
(
self
.
note
)
note
.
save
(
)
self
.
note
.
update
({
result_note
=
note
.
as_dict
()
'id'
:
None
,
del
result_note
[
'id'
]
'created'
:
None
,
del
result_note
[
'created'
]
'updated'
:
None
,
del
result_note
[
'updated'
]
})
self
.
assertEqual
(
note
.
as_dict
(),
self
.
note
)
def
test_clean_invalid_note
(
self
):
self
.
assertEqual
(
result_note
,
self
.
note_dict
)
def
test_create_invalid_note
(
self
):
note
=
Note
()
note
=
Note
()
for
empty_type
in
(
None
,
''
,
0
,
[]):
for
empty_type
in
(
None
,
''
,
[]):
with
self
.
assertRaises
(
ValidationError
):
with
self
.
assertRaises
(
ValidationError
):
note
.
c
lean
(
empty_type
)
note
.
c
reate
(
empty_type
)
def
test_must_have_fields
(
self
):
def
test_must_have_fields_create
(
self
):
note
=
Note
()
for
field
in
[
'user'
,
'usage_id'
,
'course_id'
,
'ranges'
]:
for
field
in
[
'user'
,
'usage_id'
,
'course_id'
,
'ranges'
]:
payload
=
self
.
note
.
copy
()
payload
=
self
.
note
_dict
.
copy
()
payload
.
pop
(
field
)
payload
.
pop
(
field
)
with
self
.
assertRaises
(
ValidationError
):
with
self
.
assertRaises
(
ValidationError
):
note
.
clean
(
payload
)
note
=
Note
.
create
(
payload
)
note
.
full_clean
()
def
test_save
(
self
):
note
=
Note
()
note
.
clean
(
self
.
note
)
note
.
save
()
def
test_extract_document
(
self
):
def
test_extract_document
(
self
):
note
=
Note
()
note
=
Note
.
create
(
self
.
note_dict
.
copy
())
note
.
clean
(
self
.
note
)
note
.
save
()
note
.
save
()
self
.
assertEqual
(
NoteMappingType
.
extract_document
(
note
.
id
),
note
.
as_dict
())
self
.
assertEqual
(
NoteMappingType
.
extract_document
(
note
.
id
),
note
.
as_dict
())
...
...
notesapi/v1/tests/test_views.py
View file @
271bc958
...
@@ -166,7 +166,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -166,7 +166,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
"""
"""
Test if annotation 'created' field is not used by API.
Test if annotation 'created' field is not used by API.
"""
"""
self
.
payload
[
'created'
]
=
'
abc
'
self
.
payload
[
'created'
]
=
'
2015-01-05T11:46:58.837059+00:00
'
response
=
self
.
client
.
post
(
reverse
(
'api:v1:annotations'
),
self
.
payload
,
format
=
'json'
)
response
=
self
.
client
.
post
(
reverse
(
'api:v1:annotations'
),
self
.
payload
,
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
...
@@ -177,7 +177,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -177,7 +177,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
"""
"""
Test if annotation 'updated' field is not used by API.
Test if annotation 'updated' field is not used by API.
"""
"""
self
.
payload
[
'updated'
]
=
'
abc
'
self
.
payload
[
'updated'
]
=
'
2015-01-05T11:46:58.837059+00:00
'
response
=
self
.
client
.
post
(
reverse
(
'api:v1:annotations'
),
self
.
payload
,
format
=
'json'
)
response
=
self
.
client
.
post
(
reverse
(
'api:v1:annotations'
),
self
.
payload
,
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
...
@@ -458,7 +458,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -458,7 +458,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
"""
"""
for
i
in
xrange
(
5
):
for
i
in
xrange
(
5
):
kwargs
=
{
'text'
:
'Foo_{}'
.
format
(
i
)}
kwargs
=
{
'text'
:
'Foo_{}'
.
format
(
i
)}
self
.
_create_annotation
(
refresh
=
False
,
**
kwargs
)
self
.
_create_annotation
(
**
kwargs
)
url
=
reverse
(
'api:v1:annotations'
)
url
=
reverse
(
'api:v1:annotations'
)
response
=
self
.
client
.
get
(
url
,
self
.
headers
)
response
=
self
.
client
.
get
(
url
,
self
.
headers
)
...
...
notesapi/v1/views.py
View file @
271bc958
...
@@ -72,12 +72,11 @@ class AnnotationListView(APIView):
...
@@ -72,12 +72,11 @@ class AnnotationListView(APIView):
if
'id'
in
self
.
request
.
DATA
:
if
'id'
in
self
.
request
.
DATA
:
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
note
=
Note
()
try
:
try
:
note
.
clean
(
self
.
request
.
DATA
)
note
=
Note
.
create
(
self
.
request
.
DATA
)
note
.
full_clean
()
except
ValidationError
as
error
:
except
ValidationError
as
error
:
log
.
debug
(
error
)
log
.
debug
(
error
,
exc_info
=
True
)
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
note
.
save
()
note
.
save
()
...
@@ -118,9 +117,10 @@ class AnnotationDetailView(APIView):
...
@@ -118,9 +117,10 @@ class AnnotationDetailView(APIView):
return
Response
(
'Annotation not found! No update performed.'
,
status
=
status
.
HTTP_404_NOT_FOUND
)
return
Response
(
'Annotation not found! No update performed.'
,
status
=
status
.
HTTP_404_NOT_FOUND
)
try
:
try
:
note
.
clean
(
self
.
request
.
DATA
)
note
.
text
=
self
.
request
.
data
[
'text'
]
except
ValidationError
as
e
:
note
.
full_clean
()
log
.
debug
(
e
)
except
KeyError
as
error
:
log
.
debug
(
error
,
exc_info
=
True
)
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
note
.
save
()
note
.
save
()
...
...
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