增加了比赛排名页面的分页功能

This commit is contained in:
virusdefender 2015-10-08 14:03:50 +08:00
parent a8ba7f9326
commit ee0b076b55
7 changed files with 66 additions and 30 deletions

View File

@ -14,7 +14,7 @@ from django.conf import settings
from rest_framework.views import APIView
from utils.shortcuts import (serializer_invalid_response, error_response,
success_response, paginate, error_page)
success_response, paginate, error_page, paginate_data)
from account.models import SUPER_ADMIN, User
from account.decorators import login_required
from group.models import Group
@ -398,9 +398,26 @@ def contest_rank_page(request, contest_id):
r.set(cache_key, json.dumps([dict(item) for item in rank]))
else:
rank = json.loads(rank)
try:
paging_rank = paginate_data(request, rank, None)
if request.GET.get("paging", None):
rank = paging_rank["results"]
else:
rank = paging_rank
except Exception as e:
return error_page(request, e.message)
if request.GET.get("paging", None):
paging_info = paging_rank
paging_info["offset"] = paging_rank["page_size"] * (int(paging_rank["current_page"]) - 1)
else:
paging_info = {"previous_page": None, "next_page": None, "count": 0, "total_page": 0, "offset": 0}
return render(request, "oj/contest/contest_rank.html",
{"rank": rank, "contest": contest,
"contest_problems": contest_problems,
"paging_info": paging_info,
"auto_refresh": request.GET.get("auto_refresh", None) == "true",
"show_real_name": request.GET.get("show_real_name", None) == "true",})

View File

@ -15,7 +15,7 @@
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
</li>
<li role="presentation">
<a href="/contest/{{ contest.id }}/rank/">排名</a>
<a href="/contest/{{ contest.id }}/rank/?paging=true&page=1&page_size=40">排名</a>
</li>
</ul>
{% include "oj/contest/_contest_header.html" %}

View File

@ -21,7 +21,7 @@
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
</li>
<li role="presentation">
<a href="/contest/{{ contest.id }}/rank/">排名</a>
<a href="/contest/{{ contest.id }}/rank/?paging=true&page=1&page_size=40">排名</a>
</li>
</ul>

View File

@ -17,7 +17,7 @@
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
</li>
<li role="presentation" class="active">
<a href="/contest/{{ contest.id }}/rank/">排名</a>
<a href="/contest/{{ contest.id }}/rank/?paging=true&page=1&page_size=40">排名</a>
</li>
</ul>
@ -48,7 +48,7 @@
<tbody class="rank">
{% for item in rank %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<th scope="row">{{ forloop.counter|add:paging_info.offset}}</th>
<td>
<a href="/contest/{{ contest.id }}/submissions/?user_id={{ item.user__id }}">
{{ item.user__username }}
@ -69,8 +69,25 @@
</tbody>
</table>
<input type="checkbox" id="auto-refresh" {% if auto_refresh %}checked{% endif %}
onchange="if(this.checked){location.href='?auto_refresh=true'}else{location.href=location.href.split('?')[0]}">
onchange="if(this.checked){location.href=location.href + '&auto_refresh=true'}else{location.href=location.href=location.href.replace('&auto_refresh=true', '')}">
自动刷新
<nav>
<ul class="pager">
{% if paging_info.previous_page %}
<li class="previous">
<a href="/contest/{{ contest.id }}/rank/?paging=true&page={{ paging_info.previous_page }}&page_size={{ paging_info.page_size }}&auto_refresh={% if auto_refresh %}true{% endif %}">
<span aria-hidden="true">&larr;</span> 上一页
</a></li>
{% endif %}
{% if paging_info.next_page %}
<li class="next">
<a href="/contest/{{ contest.id }}/rank/?paging=true&page={{ paging_info.next_page }}&page_size={{ paging_info.page_size }}&auto_refresh={% if auto_refresh %}true{% endif %}">
下一页 <span aria-hidden="true">&rarr;</span>
</a></li>
{% endif %}
</ul>
</nav>
{% else %}
<p>还没有结果</p>
{% endif %}

View File

@ -16,7 +16,7 @@
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
</li>
<li role="presentation">
<a href="/contest/{{ contest.id }}/rank/">排名</a>
<a href="/contest/{{ contest.id }}/rank/?paging=true&page=1&page_size=40">排名</a>
</li>
{% endif %}
</ul>

View File

@ -19,7 +19,7 @@
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
</li>
<li role="presentation">
<a href="/contest/{{ contest.id }}/rank/">排名</a>
<a href="/contest/{{ contest.id }}/rank/?paging=true&page=1&page_size=40">排名</a>
</li>
</ul>
</div>

View File

@ -26,7 +26,7 @@ def success_response(data):
return Response(data={"code": 0, "data": data})
def paginate(request, query_set, object_serializer):
def paginate_data(request, query_set, object_serializer):
"""
用于分页的函数
如果 url 里面不含有paging=true那么将返回全部数据类似
@ -39,38 +39,26 @@ def paginate(request, query_set, object_serializer):
如果 url 中有 paging=true 的参数
然后还需要读取其余的两个参数page=[int]需要的页码p
age_size=[int]一页的数据条数
参数错误的时候返回{"code": 1, "data": u"参数错误"}
返回的数据格式
{
"code": 0,
"data": {
"previous_page": null,
"results": [
{
"username": "1111111",
"password": "123456"
}
],
"next_page": 2
}
}
:param query_set 数据库查询结果
:param object_serializer: 序列化单个object的serializer
:return response
"""
need_paginate = request.GET.get("paging", None)
# 如果请求的参数里面没有paging=true的话 就返回全部数据
if need_paginate != "true":
return success_response(data=object_serializer(query_set, many=True).data)
if object_serializer:
return object_serializer(query_set, many=True).data
else:
return query_set
page_size = request.GET.get("page_size", None)
if not page_size:
return error_response(u"参数错误")
raise ValueError("Error parameter page_size")
try:
page_size = int(page_size)
except Exception:
return error_response(u"参数错误")
raise ValueError("Error parameter page_size")
paginator = Paginator(query_set, page_size)
page = request.GET.get("page", None)
@ -78,11 +66,17 @@ def paginate(request, query_set, object_serializer):
try:
current_page = paginator.page(page)
except Exception:
return error_response(u"参数错误")
raise ValueError("Error parameter current_page")
if object_serializer:
results = object_serializer(current_page, many=True).data
else:
results = current_page
data = {"results": object_serializer(current_page, many=True).data,
data = {"results": results,
"previous_page": None,
"next_page": None,
"page_size": page_size,
"current_page": page,
"count": paginator.count,
"total_page": paginator.num_pages}
@ -96,6 +90,14 @@ def paginate(request, query_set, object_serializer):
except Exception:
pass
return data
def paginate(request, query_set, object_serializer=None):
try:
data= paginate_data(request, query_set, object_serializer)
except Exception:
return error_response(u"参数错误")
return success_response(data)