增加用户排名功能

This commit is contained in:
virusdefender 2016-04-24 13:21:22 +08:00
parent 5f6ee73b47
commit 794ce8ff46
No known key found for this signature in database
GPG Key ID: 1686FB5677979E61
7 changed files with 124 additions and 3 deletions

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-04-24 04:41
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('account', '0019_user_is_forbidden'),
]
operations = [
migrations.RenameField(
model_name='userprofile',
old_name='accepted_number',
new_name='accepted_problem_number',
),
migrations.RemoveField(
model_name='userprofile',
name='rank',
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-04-24 04:43
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('account', '0020_auto_20160424_1241'),
]
operations = [
migrations.RenameField(
model_name='userprofile',
old_name='submissions_number',
new_name='submission_number',
),
]

View File

@ -66,9 +66,8 @@ class UserProfile(models.Model):
hduoj_username = models.CharField(max_length=30, blank=True, null=True)
bestcoder_username = models.CharField(max_length=30, blank=True, null=True)
codeforces_username = models.CharField(max_length=30, blank=True, null=True)
rank = models.IntegerField(default=65535)
accepted_number = models.IntegerField(default=0)
submissions_number = models.IntegerField(default=0)
accepted_problem_number = models.IntegerField(default=0)
submission_number = models.IntegerField(default=0)
# JSON字典用来表示该用户的问题的解决状态 1为ac2为正在进行
problems_status = JSONField(default={})
phone_number = models.CharField(max_length=15, blank=True, null=True)

View File

@ -5,6 +5,7 @@ import StringIO
from django import http
from django.contrib import auth
from django.shortcuts import render
from django.core.paginator import Paginator
from django.db.models import Q
from django.conf import settings
from django.http import HttpResponse
@ -472,3 +473,25 @@ class TwoFactorAuthAPIView(APIView):
return error_response(u"验证码错误")
else:
return serializer_invalid_response(serializer)
def user_rank_page(request, page=1):
ranks = UserProfile.objects.filter(submission_number__gt=0).order_by("-accepted_problem_number", "-submission_number")
paginator = Paginator(ranks, 20)
try:
ranks = paginator.page(int(page))
except Exception:
return error_page(request, u"不存在的页码")
previous_page = next_page = None
try:
previous_page = ranks.previous_page_number()
except Exception:
pass
try:
next_page = ranks.next_page_number()
except Exception:
pass
return render(request, "utils/rank.html", {"ranks": ranks, "page": page,
"previous_page": previous_page,
"next_page": next_page,
"start_id": int(page) * 20 - 20,})

View File

@ -139,6 +139,8 @@ urlpatterns = [
url(r'^reset_password/t/(?P<token>\w+)/$', "account.views.reset_password_page", name="reset_password_page"),
url(r'^api/two_factor_auth/$', TwoFactorAuthAPIView.as_view(), name="two_factor_auth_api"),
url(r'^two_factor_auth/$', TemplateView.as_view(template_name="oj/account/two_factor_auth.html"), name="two_factor_auth_page"),
url(r'^rank/(?P<page>\d+)/$', "account.views.user_rank_page", name="user_rank_page"),
url(r'^rank/$', "account.views.user_rank_page", name="user_rank_page"),
]

View File

@ -54,6 +54,7 @@
<li><a href="/submissions/">提交</a></li>
<li><a href="/contests/">比赛</a></li>
<li><a href="/groups/">小组</a></li>
<li><a href="/rank/">排名</a></li>
<li><a href="/help/">帮助</a></li>
</ul>
{% if request.user.is_authenticated %}

View File

@ -0,0 +1,52 @@
{% extends "oj_base.html" %}
{% block body %}
<div class="container main">
<div class="col-md-12 col-lg-12">
<table class="table table-striped">
{% if request.user.is_authenticated %}
<caption>我的数据: 通过的题目数量: {{ request.user.userprofile.accepted_problem_number }}
总提交数量: {{ request.user.userprofile.submission_number }}</caption>
{% endif %}
<thead>
<tr>
<th>#</th>
<th>用户名</th>
<th>签名</th>
<th>通过的题目数量</th>
<th>总提交数量</th>
<th>AC 率</th>
</tr>
</thead>
<tbody>
{% for rank in ranks %}
<tr>
<th scope="row">{{ forloop.counter |add:start_id }}</th>
<td><a href="/user/{{ rank.user.username }}">{{ rank.user.username }}</a></td>
<td>{% if rank.mood %}{{ rank.mood }}{% endif %}</td>
<td>{{ rank.accepted_problem_number }}</td>
<td>{{ rank.submission_number }}</td>
<td>{% widthratio rank.accepted_problem_number rank.submission_number 100 %}%</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav>
<ul class="pager">
{% if previous_page %}
<li class="previous">
<a href="/rank/{{ previous_page }}/">
<span aria-hidden="true">&larr;</span> 上一页</a></li>
{% endif %}
{% if next_page %}
<li class="next">
<a href="/rank/{{ next_page }}/">下一页
<span aria-hidden="true">&rarr;</span></a></li>
{% endif %}
</ul>
</nav>
</div>
</div>
{% endblock %}