增加题目提交总数和通过率统计字段

This commit is contained in:
virusdefender 2015-08-17 20:56:42 +08:00
parent 5eda65993b
commit 616aed6a5c
5 changed files with 38 additions and 4 deletions

View File

@ -17,7 +17,6 @@ class DBRouter(object):
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label == "submission":
if db == "submission":
return db == app_label
return db == app_label
else:
return db == "default"
return db == "default"

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('submission', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='submission',
name='is_counted',
field=models.BooleanField(default=False),
),
]

View File

@ -18,6 +18,7 @@ class Submission(models.Model):
accepted_answer_time = models.IntegerField(blank=True, null=True)
# 这个字段只有在题目是accepted 的时候才会用到,比赛题目的提交可能还会有得分等信息,存储在这里面
accepted_answer_info = models.TextField(blank=True, null=True)
is_counted = models.BooleanField(default=False)
class Meta:
db_table = "submission"

View File

@ -28,6 +28,9 @@ class SubmissionAPIView(APIView):
data = serializer.data
try:
problem = Problem.objects.get(id=data["problem_id"])
# 更新问题的总提交计数
problem.total_submit_number += 1
problem.save()
except Problem.DoesNotExist:
return error_response(u"题目不存在")
submission = Submission.objects.create(user_id=request.user.id, language=int(data["language"]),
@ -51,6 +54,18 @@ class SubmissionAPIView(APIView):
submission = Submission.objects.get(id=submission_id, user_id=request.user.id)
except Submission.DoesNotExist:
return error_response(u"提交不存在")
# 标记这个submission 已经被统计
if not submission.is_counted:
submission.is_counted = True
submission.save()
if submission.result == result["accepted"]:
# 更新题目的 ac 计数器
try:
problem = Problem.objects.get(id=submission.problem_id)
problem.total_accepted_number += 1
problem.save()
except Problem.DoesNotExist:
pass
response_data = {"result": submission.result}
if submission.result == 0:
response_data["accepted_answer_time"] = submission.accepted_answer_time

View File

@ -30,7 +30,7 @@
<th scope="row"><a href="/problem/{{ item.id }}/">{{ item.id }}</a></th>
<td><a href="/problem/{{ item.id }}/">{{ item.title }}</a></td>
<td>{{ item.difficulty }}</td>
<td>{{ item|accepted_radio }}</td>
<td>{{ item|accepted_radio }}%</td>
</tr>
{% endfor %}
</tbody>