Commit 17d11a63 by Rocky Duan

pagination

parent f204e988
...@@ -3,18 +3,28 @@ from django.views.decorators.http import require_POST ...@@ -3,18 +3,28 @@ from django.views.decorators.http import require_POST
from django.http import HttpResponse from django.http import HttpResponse
from django.utils import simplejson from django.utils import simplejson
from django.core.context_processors import csrf from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from mitxmako.shortcuts import render_to_response, render_to_string from mitxmako.shortcuts import render_to_response, render_to_string
from courseware.courses import check_course from courseware.courses import check_course
import comment_client
import dateutil
from dateutil.tz import tzlocal from dateutil.tz import tzlocal
from datehelper import time_ago_in_words from datehelper import time_ago_in_words
from django_comment_client.utils import get_categorized_discussion_info from django_comment_client.utils import get_categorized_discussion_info
from urllib import urlencode
import json import json
import comment_client
import dateutil
THREADS_PER_PAGE = 20
PAGES_NEARBY_DELTA = 2
class HtmlResponse(HttpResponse):
def __init__(self, html=''):
super(HtmlResponse, self).__init__(html, content_type='text/plain')
def render_accordion(request, course, discussion_id): def render_accordion(request, course, discussion_id):
...@@ -29,29 +39,58 @@ def render_accordion(request, course, discussion_id): ...@@ -29,29 +39,58 @@ def render_accordion(request, course, discussion_id):
return render_to_string('discussion/_accordion.html', context) return render_to_string('discussion/_accordion.html', context)
def render_discussion(request, course_id, threads, discussion_id=None, with_search_bar=True, search_text='', template='discussion/_inline.html'): def render_discussion(request, course_id, threads, discussion_id=None, with_search_bar=True,
search_text='', discussion_type='inline', page=1, num_pages=None,
per_page=THREADS_PER_PAGE, url_for_page=None):
template = {
'inline': 'discussion/_inline.html',
'forum': 'discussion/_forum.html',
}[discussion_type]
def _url_for_inline_page(page, per_page):
raw_url = reverse('django_comment_client.forum.views.inline_discussion', args=[course_id, discussion_id])
return raw_url + '?' + urlencode({'page': page, 'per_page': per_page})
def _url_for_forum_page(page, per_page):
raw_url = reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id, discussion_id])
return raw_url + '?' + urlencode({'page': page, 'per_page': per_page})
url_for_page = {
'inline': _url_for_inline_page,
'forum': _url_for_forum_page,
}[discussion_type]
context = { context = {
'threads': threads, 'threads': threads,
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'search_bar': '' if not with_search_bar \ 'search_bar': '' if not with_search_bar \
else render_search_bar(request, course_id, discussion_id, text=search_text), else render_search_bar(request, course_id, discussion_id, text=search_text),
'user_info': comment_client.get_user_info(request.user.id, raw=True), 'user_info': comment_client.get_user_info(request.user.id, raw=True),
'tags': comment_client.get_threads_tags(raw=True),
'course_id': course_id, 'course_id': course_id,
'request': request, 'request': request,
'page': page,
'per_page': per_page,
'num_pages': num_pages,
'pages_nearby_delta': PAGES_NEARBY_DELTA,
'discussion_type': discussion_type,
'url_for_page': url_for_page,
} }
return render_to_string(template, context) return render_to_string(template, context)
def render_inline_discussion(*args, **kwargs): def render_inline_discussion(*args, **kwargs):
return render_discussion(template='discussion/_inline.html', *args, **kwargs)
return render_discussion(discussion_type='inline', *args, **kwargs)
def render_forum_discussion(*args, **kwargs): def render_forum_discussion(*args, **kwargs):
return render_discussion(template='discussion/_forum.html', *args, **kwargs) return render_discussion(discussion_type='forum', *args, **kwargs)
# discussion per page is fixed for now
def inline_discussion(request, course_id, discussion_id): def inline_discussion(request, course_id, discussion_id):
threads = comment_client.get_threads(discussion_id, recursive=False) formpage = request.GET.get('page', 1)
html = render_inline_discussion(request, course_id, threads, discussion_id=discussion_id) threads, page, num_pages = comment_client.get_threads(discussion_id, recursive=False, page=page, per_page=THREADS_PER_PAGE)
return HttpResponse(html, content_type="text/plain") html = render_inline_discussion(request, course_id, threads, discussion_id=discussion_id, num_pages=num_pages, page=page)
return HtmlResponse(html)
def render_search_bar(request, course_id, discussion_id=None, text=''): def render_search_bar(request, course_id, discussion_id=None, text=''):
if not discussion_id: if not discussion_id:
...@@ -66,18 +105,29 @@ def render_search_bar(request, course_id, discussion_id=None, text=''): ...@@ -66,18 +105,29 @@ def render_search_bar(request, course_id, discussion_id=None, text=''):
def forum_form_discussion(request, course_id, discussion_id): def forum_form_discussion(request, course_id, discussion_id):
course = check_course(course_id) course = check_course(course_id)
search_text = request.GET.get('text', '') search_text = request.GET.get('text', '')
page = request.GET.get('page', 1)
if len(search_text) > 0: if len(search_text) > 0:
threads = comment_client.search_threads({'text': search_text, 'commentable_id': discussion_id}) threads, page, per_page, num_pages = comment_client.search_threads({
'text': search_text,
'commentable_id': discussion_id,
'course_id': course_id,
'page': page,
'per_page': THREADS_PER_PAGE,
})
else: else:
threads = comment_client.get_threads(discussion_id, recursive=False) threads, page, per_page, num_pages = comment_client.get_threads(discussion_id, recursive=False, page=page, per_page=THREADS_PER_PAGE)
context = { context = {
'csrf': csrf(request)['csrf_token'], 'csrf': csrf(request)['csrf_token'],
'course': course, 'course': course,
'content': render_forum_discussion(request, course_id, threads, discussion_id=discussion_id, search_text=search_text), 'content': render_forum_discussion(request, course_id, threads,
discussion_id=discussion_id,
search_text=search_text,
num_pages=num_pages,
per_page=per_page,
page=page),
'accordion': render_accordion(request, course, discussion_id), 'accordion': render_accordion(request, course, discussion_id),
} }
...@@ -102,7 +152,6 @@ def render_single_thread(request, course_id, thread_id): ...@@ -102,7 +152,6 @@ def render_single_thread(request, course_id, thread_id):
'thread': thread, 'thread': thread,
'user_info': comment_client.get_user_info(request.user.id, raw=True), 'user_info': comment_client.get_user_info(request.user.id, raw=True),
'annotated_content_info': json.dumps(get_annotated_content_info(thread=thread, user_id=request.user.id)), 'annotated_content_info': json.dumps(get_annotated_content_info(thread=thread, user_id=request.user.id)),
'tags': comment_client.get_threads_tags(raw=True),
'course_id': course_id, 'course_id': course_id,
'request': request, 'request': request,
} }
......
...@@ -15,8 +15,9 @@ class CommentClientUnknownError(CommentClientError): ...@@ -15,8 +15,9 @@ class CommentClientUnknownError(CommentClientError):
def delete_threads(commentable_id, *args, **kwargs): def delete_threads(commentable_id, *args, **kwargs):
return _perform_request('delete', _url_for_commentable_threads(commentable_id), *args, **kwargs) return _perform_request('delete', _url_for_commentable_threads(commentable_id), *args, **kwargs)
def get_threads(commentable_id, recursive=False, *args, **kwargs): def get_threads(commentable_id, recursive=False, page=1, per_page=20, *args, **kwargs):
return _perform_request('get', _url_for_threads(commentable_id), {'recursive': recursive}, *args, **kwargs) response = _perform_request('get', _url_for_threads(commentable_id), {'recursive': recursive, 'page': page, 'per_page': per_page}, *args, **kwargs)
return response['collection'], response['page'], response['per_page'], response['num_pages']
def get_threads_tags(*args, **kwargs): def get_threads_tags(*args, **kwargs):
return _perform_request('get', _url_for_threads_tags(), {}, *args, **kwargs) return _perform_request('get', _url_for_threads_tags(), {}, *args, **kwargs)
...@@ -98,6 +99,8 @@ def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs): ...@@ -98,6 +99,8 @@ def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs):
return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id}) return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id})
def search_threads(attributes, *args, **kwargs): def search_threads(attributes, *args, **kwargs):
default_attributes = {'page': 1, 'per_page': 20}
attributes = dict(default_attributes.items() + attributes.items())
return _perform_request('get', _url_for_search_threads(), attributes, *args, **kwargs) return _perform_request('get', _url_for_search_threads(), attributes, *args, **kwargs)
def _perform_request(method, url, data_or_params=None, *args, **kwargs): def _perform_request(method, url, data_or_params=None, *args, **kwargs):
......
...@@ -280,7 +280,12 @@ $discussion_input_width: 90%; ...@@ -280,7 +280,12 @@ $discussion_input_width: 90%;
} }
} }
} }
.discussion-paginator {
margin-top: 40px;
div {
display: inline-block;
}
}
} }
......
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
${search_bar} ${search_bar}
<div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div> <div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div>
</div> </div>
<%include file="_paginator.html" />
% for thread in threads: % for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)} ${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
% endfor % endfor
<%include file="_paginator.html" />
</section> </section>
<%! <%!
...@@ -21,5 +23,4 @@ ...@@ -21,5 +23,4 @@
<script type="text/javascript"> <script type="text/javascript">
var $$user_info = JSON.parse('${user_info | escape_quotes}'); var $$user_info = JSON.parse('${user_info | escape_quotes}');
var $$course_id = "${course_id}"; var $$course_id = "${course_id}";
var $$tags = JSON.parse("${tags | escape_quotes}");
</script> </script>
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
${search_bar} ${search_bar}
<div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div> <div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div>
</div> </div>
<%include file="_paginator.html" />
% for thread in threads: % for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)} ${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
% endfor % endfor
<%include file="_paginator.html" />
</section> </section>
<%! <%!
...@@ -21,5 +23,4 @@ ...@@ -21,5 +23,4 @@
<script type="text/javascript"> <script type="text/javascript">
var $$user_info = JSON.parse('${user_info | escape_quotes}'); var $$user_info = JSON.parse('${user_info | escape_quotes}');
var $$course_id = "${course_id}"; var $$course_id = "${course_id}";
var $$tags = JSON.parse("${tags | escape_quotes}");
</script> </script>
<%def name="link_to_page(_page)">
% if _page != page:
<div class="page-link">
<a href="${url_for_page(_page, per_page)}">${_page}</a>
</div>
% else:
<div class="page-link">${_page}</div>
% endif
</%def>
<%def name="list_pages(*args)">
% for arg in args:
% if arg == 'dots':
<div class="page-dots">...</div>
% elif isinstance(arg, list):
% for _page in arg:
${link_to_page(_page)}
% endfor
% else:
${link_to_page(arg)}
% endif
% endfor
</%def>
<div class="discussion-${discussion_type}-paginator discussion-paginator">
<div class="prev-page">
% if page > 1:
<a href="${url_for_page(page - 1, per_page)}">&lt; Previous page</a>
% else:
&lt; Previous page
% endif
</div>
% if num_pages <= 2 * pages_nearby_delta + 2:
${list_pages(range(1, num_pages + 1))}
% else:
% if page <= 2 * pages_nearby_delta:
${list_pages(range(1, 2 * pages_nearby_delta + 2), 'dots', num_pages)}
% elif num_pages - page + 1 <= 2 * pages_nearby_delta:
${list_pages(1, 'dots', range(num_pages - 2 * pages_nearby_delta, num_pages + 1))}
% else:
${list_pages(1, 'dots', range(page - pages_nearby_delta, page + pages_nearby_delta + 1), 'dots', num_pages)}
% endif
% endif
<div class="next-page">
% if page < num_pages:
<a href="${url_for_page(page + 1, per_page)}">Next page &gt;</a>
% else:
Next page &gt;
% endif
</div>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment