增加了比赛排名页面的分页功能
This commit is contained in:
@@ -14,7 +14,7 @@ from django.conf import settings
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from utils.shortcuts import (serializer_invalid_response, error_response,
|
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.models import SUPER_ADMIN, User
|
||||||
from account.decorators import login_required
|
from account.decorators import login_required
|
||||||
from group.models import Group
|
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]))
|
r.set(cache_key, json.dumps([dict(item) for item in rank]))
|
||||||
else:
|
else:
|
||||||
rank = json.loads(rank)
|
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",
|
return render(request, "oj/contest/contest_rank.html",
|
||||||
{"rank": rank, "contest": contest,
|
{"rank": rank, "contest": contest,
|
||||||
"contest_problems": contest_problems,
|
"contest_problems": contest_problems,
|
||||||
|
"paging_info": paging_info,
|
||||||
"auto_refresh": request.GET.get("auto_refresh", None) == "true",
|
"auto_refresh": request.GET.get("auto_refresh", None) == "true",
|
||||||
"show_real_name": request.GET.get("show_real_name", None) == "true",})
|
"show_real_name": request.GET.get("show_real_name", None) == "true",})
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
||||||
</li>
|
</li>
|
||||||
<li role="presentation">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
{% include "oj/contest/_contest_header.html" %}
|
{% include "oj/contest/_contest_header.html" %}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
||||||
</li>
|
</li>
|
||||||
<li role="presentation">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
||||||
</li>
|
</li>
|
||||||
<li role="presentation" class="active">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@
|
|||||||
<tbody class="rank">
|
<tbody class="rank">
|
||||||
{% for item in rank %}
|
{% for item in rank %}
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">{{ forloop.counter }}</th>
|
<th scope="row">{{ forloop.counter|add:paging_info.offset}}</th>
|
||||||
<td>
|
<td>
|
||||||
<a href="/contest/{{ contest.id }}/submissions/?user_id={{ item.user__id }}">
|
<a href="/contest/{{ contest.id }}/submissions/?user_id={{ item.user__id }}">
|
||||||
{{ item.user__username }}
|
{{ item.user__username }}
|
||||||
@@ -69,8 +69,25 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<input type="checkbox" id="auto-refresh" {% if auto_refresh %}checked{% endif %}
|
<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">←</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">→</span>
|
||||||
|
</a></li>
|
||||||
|
{% endif %}
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>还没有结果</p>
|
<p>还没有结果</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
||||||
</li>
|
</li>
|
||||||
<li role="presentation">
|
<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>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
<a href="/contest/{{ contest.id }}/submissions/">提交</a>
|
||||||
</li>
|
</li>
|
||||||
<li role="presentation">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ def success_response(data):
|
|||||||
return Response(data={"code": 0, "data": 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,那么将返回全部数据。类似
|
如果 url 里面不含有paging=true,那么将返回全部数据。类似
|
||||||
@@ -39,38 +39,26 @@ def paginate(request, query_set, object_serializer):
|
|||||||
如果 url 中有 paging=true 的参数,
|
如果 url 中有 paging=true 的参数,
|
||||||
然后还需要读取其余的两个参数,page=[int],需要的页码,p
|
然后还需要读取其余的两个参数,page=[int],需要的页码,p
|
||||||
age_size=[int],一页的数据条数
|
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 query_set 数据库查询结果
|
||||||
:param object_serializer: 序列化单个object的serializer
|
:param object_serializer: 序列化单个object的serializer
|
||||||
:return response
|
|
||||||
"""
|
"""
|
||||||
need_paginate = request.GET.get("paging", None)
|
need_paginate = request.GET.get("paging", None)
|
||||||
# 如果请求的参数里面没有paging=true的话 就返回全部数据
|
# 如果请求的参数里面没有paging=true的话 就返回全部数据
|
||||||
if need_paginate != "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)
|
page_size = request.GET.get("page_size", None)
|
||||||
if not page_size:
|
if not page_size:
|
||||||
return error_response(u"参数错误")
|
raise ValueError("Error parameter page_size")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
page_size = int(page_size)
|
page_size = int(page_size)
|
||||||
except Exception:
|
except Exception:
|
||||||
return error_response(u"参数错误")
|
raise ValueError("Error parameter page_size")
|
||||||
|
|
||||||
paginator = Paginator(query_set, page_size)
|
paginator = Paginator(query_set, page_size)
|
||||||
page = request.GET.get("page", None)
|
page = request.GET.get("page", None)
|
||||||
@@ -78,11 +66,17 @@ def paginate(request, query_set, object_serializer):
|
|||||||
try:
|
try:
|
||||||
current_page = paginator.page(page)
|
current_page = paginator.page(page)
|
||||||
except Exception:
|
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,
|
"previous_page": None,
|
||||||
"next_page": None,
|
"next_page": None,
|
||||||
|
"page_size": page_size,
|
||||||
|
"current_page": page,
|
||||||
"count": paginator.count,
|
"count": paginator.count,
|
||||||
"total_page": paginator.num_pages}
|
"total_page": paginator.num_pages}
|
||||||
|
|
||||||
@@ -96,6 +90,14 @@ def paginate(request, query_set, object_serializer):
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
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)
|
return success_response(data)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user