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
6941a560
Commit
6941a560
authored
Aug 18, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored code to render ajax content
parent
03370e95
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
42 additions
and
105 deletions
+42
-105
lms/djangoapps/django_comment_client/base/views.py
+21
-63
lms/djangoapps/django_comment_client/forum/views.py
+3
-24
lms/djangoapps/django_comment_client/utils.py
+12
-12
lms/lib/comment_client/comment.py
+1
-1
lms/lib/comment_client/thread.py
+1
-1
lms/templates/discussion/ajax_create_comment.html
+1
-1
lms/templates/discussion/ajax_create_thread.html
+1
-1
lms/templates/discussion/ajax_update_comment.html
+1
-1
lms/templates/discussion/ajax_update_thread.html
+1
-1
No files found.
lms/djangoapps/django_comment_client/base/views.py
View file @
6941a560
...
@@ -45,6 +45,19 @@ def permitted(fn):
...
@@ -45,6 +45,19 @@ def permitted(fn):
return
JsonError
(
"unauthorized"
)
return
JsonError
(
"unauthorized"
)
return
wrapper
return
wrapper
def
ajax_content_response
(
request
,
course_id
,
content
,
template_name
):
context
=
{
'course_id'
:
course_id
,
'content'
:
content
,
}
html
=
render_to_string
(
template_name
,
context
)
annotated_content_info
=
utils
.
get_annotated_content_info
(
course_id
,
content
,
request
.
user
)
return
JsonResponse
({
'html'
:
html
,
'content'
:
content
,
'annotated_content_info'
:
annotated_content_info
,
})
@require_POST
@require_POST
@login_required
@login_required
@permitted
@permitted
...
@@ -60,20 +73,7 @@ def create_thread(request, course_id, commentable_id):
...
@@ -60,20 +73,7 @@ def create_thread(request, course_id, commentable_id):
user
=
cc
.
User
.
from_django_user
(
request
.
user
)
user
=
cc
.
User
.
from_django_user
(
request
.
user
)
user
.
follow
(
thread
)
user
.
follow
(
thread
)
if
request
.
is_ajax
():
if
request
.
is_ajax
():
context
=
{
return
ajax_content_response
(
request
,
course_id
,
thread
.
to_dict
(),
'discussion/ajax_create_thread.html'
)
'course_id'
:
course_id
,
'thread'
:
thread
.
to_dict
(),
}
html
=
render_to_string
(
'discussion/ajax_create_thread.html'
,
context
)
annotated_content_info
=
utils
.
get_annotated_content_info
(
course_id
,
thread
.
to_dict
(),
request
.
user
,
'thread'
)
return
JsonResponse
({
'html'
:
html
,
'content'
:
thread
.
to_dict
(),
'annotated_content_info'
:
annotated_content_info
,
})
else
:
else
:
return
JsonResponse
(
thread
.
to_dict
())
return
JsonResponse
(
thread
.
to_dict
())
...
@@ -85,20 +85,7 @@ def update_thread(request, course_id, thread_id):
...
@@ -85,20 +85,7 @@ def update_thread(request, course_id, thread_id):
thread
.
update_attributes
(
**
extract
(
request
.
POST
,
[
'body'
,
'title'
,
'tags'
]))
thread
.
update_attributes
(
**
extract
(
request
.
POST
,
[
'body'
,
'title'
,
'tags'
]))
thread
.
save
()
thread
.
save
()
if
request
.
is_ajax
():
if
request
.
is_ajax
():
context
=
{
return
ajax_content_response
(
request
,
course_id
,
thread
.
to_dict
(),
'discussion/ajax_update_thread.html'
)
'thread'
:
thread
.
to_dict
(),
'course_id'
:
course_id
,
}
html
=
render_to_string
(
'discussion/ajax_update_thread.html'
,
context
)
annotated_content_info
=
utils
.
get_annotated_content_info
(
course_id
,
thread
.
to_dict
(),
request
.
user
,
'thread'
)
return
JsonResponse
({
'html'
:
html
,
'content'
:
thread
.
to_dict
(),
'annotated_content_info'
:
annotated_content_info
,
})
else
:
else
:
return
JsonResponse
(
thread
.
to_dict
())
return
JsonResponse
(
thread
.
to_dict
())
...
@@ -116,20 +103,7 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None):
...
@@ -116,20 +103,7 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None):
user
=
cc
.
User
.
from_django_user
(
request
.
user
)
user
=
cc
.
User
.
from_django_user
(
request
.
user
)
user
.
follow
(
comment
.
thread
)
user
.
follow
(
comment
.
thread
)
if
request
.
is_ajax
():
if
request
.
is_ajax
():
context
=
{
return
ajax_content_response
(
request
,
course_id
,
comment
.
to_dict
(),
'discussion/ajax_create_comment.html'
)
'comment'
:
comment
.
to_dict
(),
'course_id'
:
course_id
,
}
html
=
render_to_string
(
'discussion/ajax_create_comment.html'
,
context
)
annotated_content_info
=
utils
.
get_annotated_content_info
(
course_id
,
comment
.
to_dict
(),
request
.
user
,
'comment'
)
return
JsonResponse
({
'html'
:
html
,
'content'
:
comment
.
to_dict
(),
'annotated_content_info'
:
annotated_content_info
,
})
else
:
else
:
return
JsonResponse
(
comment
.
to_dict
())
return
JsonResponse
(
comment
.
to_dict
())
...
@@ -155,20 +129,7 @@ def update_comment(request, course_id, comment_id):
...
@@ -155,20 +129,7 @@ def update_comment(request, course_id, comment_id):
comment
.
update_attributes
(
**
extract
(
request
.
POST
,
[
'body'
]))
comment
.
update_attributes
(
**
extract
(
request
.
POST
,
[
'body'
]))
comment
.
save
()
comment
.
save
()
if
request
.
is_ajax
():
if
request
.
is_ajax
():
context
=
{
return
ajax_content_response
(
request
,
course_id
,
comment
.
to_dict
(),
'discussion/ajax_update_comment.html'
)
'comment'
:
comment
.
to_dict
(),
'course_id'
:
course_id
,
}
html
=
render_to_string
(
'discussion/ajax_update_comment.html'
,
context
)
annotated_content_info
=
utils
.
get_annotated_content_info
(
course_id
,
comment
.
to_dict
(),
request
.
user
,
'comment'
)
return
JsonResponse
({
'html'
:
html
,
'content'
:
comment
.
to_dict
(),
'annotated_content_info'
:
annotated_content_info
,
})
else
:
else
:
return
JsonResponse
(
comment
.
to_dict
()),
return
JsonResponse
(
comment
.
to_dict
()),
...
@@ -329,15 +290,12 @@ def update_moderator_status(request, course_id, user_id):
...
@@ -329,15 +290,12 @@ def update_moderator_status(request, course_id, user_id):
def
search_similar_threads
(
request
,
course_id
,
commentable_id
):
def
search_similar_threads
(
request
,
course_id
,
commentable_id
):
text
=
request
.
GET
.
get
(
'text'
,
None
)
text
=
request
.
GET
.
get
(
'text'
,
None
)
if
text
:
if
text
:
return
JsonResponse
(
query_params
=
{
cc
.
search_similar_threads
(
course_id
,
recursive
=
False
,
query_params
=
{
'text'
:
text
,
'text'
:
text
,
'commentable_id'
:
commentable_id
,
'commentable_id'
:
commentable_id
,
},
}
))
result
=
cc
.
search_similar_threads
(
course_id
,
recursive
=
False
,
query_params
=
query_params
)
return
JsonResponse
(
result
)
else
:
else
:
return
JsonResponse
([])
return
JsonResponse
([])
...
...
lms/djangoapps/django_comment_client/forum/views.py
View file @
6941a560
...
@@ -66,7 +66,7 @@ def render_discussion(request, course_id, threads, *args, **kwargs):
...
@@ -66,7 +66,7 @@ def render_discussion(request, course_id, threads, *args, **kwargs):
}[
discussion_type
]()
}[
discussion_type
]()
print
"start annotating"
print
"start annotating"
annotated_content_infos
=
map
(
lambda
x
:
utils
.
get_annotated_content_infos
(
course_id
,
x
,
request
.
user
,
type
=
'thread'
),
threads
)
annotated_content_infos
=
map
(
lambda
x
:
utils
.
get_annotated_content_infos
(
course_id
,
x
,
request
.
user
),
threads
)
print
"start merging annotations"
print
"start merging annotations"
annotated_content_info
=
reduce
(
utils
.
merge_dict
,
annotated_content_infos
,
{})
annotated_content_info
=
reduce
(
utils
.
merge_dict
,
annotated_content_infos
,
{})
print
"finished annotating"
print
"finished annotating"
...
@@ -161,32 +161,11 @@ def forum_form_discussion(request, course_id):
...
@@ -161,32 +161,11 @@ def forum_form_discussion(request, course_id):
}
}
return
render_to_response
(
'discussion/index.html'
,
context
)
return
render_to_response
(
'discussion/index.html'
,
context
)
def
get_annotated_content_info
(
course_id
,
content
,
user
,
is_thread
):
permissions
=
{
'editable'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"update_thread"
if
is_thread
else
"update_comment"
),
'can_reply'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"create_comment"
if
is_thread
else
"create_sub_comment"
),
'can_endorse'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"endorse_comment"
)
if
not
is_thread
else
False
,
'can_delete'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"delete_thread"
if
is_thread
else
"delete_comment"
),
'can_openclose'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"openclose_thread"
)
if
is_thread
else
False
,
'can_vote'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"vote_for_thread"
if
is_thread
else
"vote_for_comment"
),
}
return
permissions
def
get_annotated_content_infos
(
course_id
,
thread
,
user
,
is_thread
=
True
):
infos
=
{}
def
_annotate
(
content
,
is_thread
=
is_thread
):
infos
[
str
(
content
[
'id'
])]
=
get_annotated_content_info
(
course_id
,
content
,
user
,
is_thread
)
for
child
in
content
.
get
(
'children'
,
[]):
_annotate
(
child
,
is_thread
=
False
)
_annotate
(
thread
)
return
infos
def
render_single_thread
(
request
,
discussion_id
,
course_id
,
thread_id
):
def
render_single_thread
(
request
,
discussion_id
,
course_id
,
thread_id
):
thread
=
cc
.
Thread
.
find
(
thread_id
)
.
retrieve
(
recursive
=
True
)
thread
=
cc
.
Thread
.
find
(
thread_id
)
.
retrieve
(
recursive
=
True
)
annotated_content_info
=
utils
.
get_annotated_content_infos
(
course_id
,
thread
=
thread
.
to_dict
(),
\
annotated_content_info
=
utils
.
get_annotated_content_infos
(
course_id
,
thread
=
thread
.
to_dict
(),
user
=
request
.
user
)
user
=
request
.
user
,
type
=
'thread'
)
context
=
{
context
=
{
'discussion_id'
:
discussion_id
,
'discussion_id'
:
discussion_id
,
...
@@ -203,7 +182,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
...
@@ -203,7 +182,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
if
request
.
is_ajax
():
if
request
.
is_ajax
():
thread
=
cc
.
Thread
.
find
(
thread_id
)
.
retrieve
(
recursive
=
True
)
thread
=
cc
.
Thread
.
find
(
thread_id
)
.
retrieve
(
recursive
=
True
)
annotated_content_info
=
utils
.
get_annotated_content_infos
(
course_id
,
thread
,
request
.
user
,
type
=
'thread'
)
annotated_content_info
=
utils
.
get_annotated_content_infos
(
course_id
,
thread
,
request
.
user
)
context
=
{
'thread'
:
thread
.
to_dict
(),
'course_id'
:
course_id
}
context
=
{
'thread'
:
thread
.
to_dict
(),
'course_id'
:
course_id
}
html
=
render_to_string
(
'discussion/_ajax_single_thread.html'
,
context
)
html
=
render_to_string
(
'discussion/_ajax_single_thread.html'
,
context
)
...
...
lms/djangoapps/django_comment_client/utils.py
View file @
6941a560
...
@@ -154,23 +154,23 @@ class QueryCountDebugMiddleware(object):
...
@@ -154,23 +154,23 @@ class QueryCountDebugMiddleware(object):
logging
.
info
(
'
%
s queries run, total
%
s seconds'
%
(
len
(
connection
.
queries
),
total_time
))
logging
.
info
(
'
%
s queries run, total
%
s seconds'
%
(
len
(
connection
.
queries
),
total_time
))
return
response
return
response
def
get_annotated_content_info
(
course_id
,
content
,
user
,
type
):
def
get_annotated_content_info
(
course_id
,
content
,
user
):
return
{
return
{
'editable'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"update_thread"
if
type
==
'thread'
else
"update_comment"
),
'editable'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"update_thread"
if
content
[
'type'
]
==
'thread'
else
"update_comment"
),
'can_reply'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"create_comment"
if
type
==
'thread'
else
"create_sub_comment"
),
'can_reply'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"create_comment"
if
content
[
'type'
]
==
'thread'
else
"create_sub_comment"
),
'can_endorse'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"endorse_comment"
)
if
type
==
'comment'
else
False
,
'can_endorse'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"endorse_comment"
)
if
content
[
'type'
]
==
'comment'
else
False
,
'can_delete'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"delete_thread"
if
type
==
'thread'
else
"delete_comment"
),
'can_delete'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"delete_thread"
if
content
[
'type'
]
==
'thread'
else
"delete_comment"
),
'can_openclose'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"openclose_thread"
)
if
type
==
'thread'
else
False
,
'can_openclose'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"openclose_thread"
)
if
content
[
'type'
]
==
'thread'
else
False
,
'can_vote'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"vote_for_thread"
if
type
==
'thread'
else
"vote_for_comment"
),
'can_vote'
:
check_permissions_by_view
(
user
,
course_id
,
content
,
"vote_for_thread"
if
content
[
'type'
]
==
'thread'
else
"vote_for_comment"
),
}
}
def
get_annotated_content_infos
(
course_id
,
thread
,
user
,
type
=
'thread'
):
def
get_annotated_content_infos
(
course_id
,
thread
,
user
):
infos
=
{}
infos
=
{}
def
_annotate
(
content
,
type
):
def
_annotate
(
content
):
infos
[
str
(
content
[
'id'
])]
=
get_annotated_content_info
(
course_id
,
content
,
user
,
type
)
infos
[
str
(
content
[
'id'
])]
=
get_annotated_content_info
(
course_id
,
content
,
user
)
for
child
in
content
.
get
(
'children'
,
[]):
for
child
in
content
.
get
(
'children'
,
[]):
_annotate
(
child
,
'comment'
)
_annotate
(
child
)
_annotate
(
thread
,
type
)
_annotate
(
thread
)
return
infos
return
infos
def
pluralize
(
singular_term
,
count
):
def
pluralize
(
singular_term
,
count
):
...
...
lms/lib/comment_client/comment.py
View file @
6941a560
...
@@ -10,7 +10,7 @@ class Comment(models.Model):
...
@@ -10,7 +10,7 @@ class Comment(models.Model):
'endorsed'
,
'parent_id'
,
'thread_id'
,
'endorsed'
,
'parent_id'
,
'thread_id'
,
'username'
,
'votes'
,
'user_id'
,
'closed'
,
'username'
,
'votes'
,
'user_id'
,
'closed'
,
'created_at'
,
'updated_at'
,
'depth'
,
'created_at'
,
'updated_at'
,
'depth'
,
'at_position_list'
,
'at_position_list'
,
'type'
,
]
]
updatable_fields
=
[
updatable_fields
=
[
...
...
lms/lib/comment_client/thread.py
View file @
6941a560
...
@@ -10,7 +10,7 @@ class Thread(models.Model):
...
@@ -10,7 +10,7 @@ class Thread(models.Model):
'course_id'
,
'closed'
,
'tags'
,
'votes'
,
'course_id'
,
'closed'
,
'tags'
,
'votes'
,
'commentable_id'
,
'username'
,
'user_id'
,
'commentable_id'
,
'username'
,
'user_id'
,
'created_at'
,
'updated_at'
,
'comments_count'
,
'created_at'
,
'updated_at'
,
'comments_count'
,
'at_position_list'
,
'children'
,
'at_position_list'
,
'children'
,
'type'
,
]
]
updatable_fields
=
[
updatable_fields
=
[
...
...
lms/templates/discussion/ajax_create_comment.html
View file @
6941a560
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
${renderer.render_comment(co
mm
ent)}
${renderer.render_comment(co
nt
ent)}
lms/templates/discussion/ajax_create_thread.html
View file @
6941a560
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
${renderer.render_thread(course_id,
thread
)}
${renderer.render_thread(course_id,
content
)}
lms/templates/discussion/ajax_update_comment.html
View file @
6941a560
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
${renderer.render_content(co
mm
ent, "comment")}
${renderer.render_content(co
nt
ent, "comment")}
lms/templates/discussion/ajax_update_thread.html
View file @
6941a560
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
<
%
namespace
name=
"renderer"
file=
"_thread.html"
/>
${renderer.render_content(
thread
, "thread")}
${renderer.render_content(
content
, "thread")}
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