diff --git a/account/migrations/0006_auto_20150924_1530.py b/account/migrations/0006_auto_20150924_1530.py new file mode 100644 index 00000000..29a7222e --- /dev/null +++ b/account/migrations/0006_auto_20150924_1530.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import utils.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0005_user_problems_status'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='problems_status', + field=utils.models.JsonField(default={}), + ), + ] diff --git a/account/models.py b/account/models.py index 32453336..6c849ddb 100644 --- a/account/models.py +++ b/account/models.py @@ -2,6 +2,8 @@ from django.db import models from django.contrib.auth.models import AbstractBaseUser +from jsonfield import JSONField + class AdminGroup(models.Model): pass @@ -31,7 +33,9 @@ class User(AbstractBaseUser): # 0代表不是管理员 1是普通管理员 2是超级管理员 admin_type = models.IntegerField(default=0) # JSON字典用来表示该用户的问题的解决状态 1为ac,2为正在进行 - problems_status = models.TextField(default="{}") + problems_status = JSONField(default={}) + # 找回密码用的token + # reset_password_token = models.CharField(max_length=40, blank=True, null=True) USERNAME_FIELD = 'username' REQUIRED_FIELDS = [] diff --git a/account/serializers.py b/account/serializers.py index c2852a3c..ddd5aaba 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -46,3 +46,9 @@ class EditUserSerializer(serializers.Serializer): password = serializers.CharField(max_length=30, min_length=6, required=False, default=None) email = serializers.EmailField(max_length=254) admin_type = serializers.IntegerField(default=0) + + +class ApplyResetPasswordSerializer(serializers.Serializer): + username = serializers.CharField(max_length=30) + email = serializers.EmailField() + captcha = serializers.CharField(max_length=4, min_length=4) \ No newline at end of file diff --git a/account/views.py b/account/views.py index 2583f364..09c1e6a7 100644 --- a/account/views.py +++ b/account/views.py @@ -3,17 +3,20 @@ from django import http from django.contrib import auth from django.shortcuts import render from django.db.models import Q +from django.conf import settings from rest_framework.views import APIView from rest_framework.response import Response -from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate +from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate, rand_str from utils.captcha import Captcha from .decorators import login_required from .models import User from .serializers import (UserLoginSerializer, UsernameCheckSerializer, UserRegisterSerializer, UserChangePasswordSerializer, - EmailCheckSerializer, UserSerializer, EditUserSerializer) + EmailCheckSerializer, UserSerializer, EditUserSerializer, + ApplyResetPasswordSerializer) +from .decorators import super_admin_required class UserLoginAPIView(APIView): @@ -150,6 +153,7 @@ class EmailCheckAPIView(APIView): class UserAdminAPIView(APIView): + @super_admin_required def put(self, request): """ 用户编辑json api接口 @@ -181,6 +185,7 @@ class UserAdminAPIView(APIView): else: return serializer_invalid_response(serializer) + @super_admin_required def get(self, request): """ 用户分页json api接口 @@ -222,8 +227,39 @@ class AccountSecurityAPIView(APIView): username = request.GET.get("username", None) if username: try: - User.objects.get(username=username, admin_type__gt=0) + user = User.objects.get(username=username) except User.DoesNotExist: - return success_response({"applied_captcha": False}) - return success_response({"applied_captcha": True}) + return success_response({"applied_captcha": True}) + if user.admin_type > 0: + return success_response({"applied_captcha": True}) return success_response({"applied_captcha": False}) + + +class ApplyResetPasswordAPIView(APIView): + def post(self, request): + serializer = ApplyResetPasswordSerializer(data=request.data) + if serializer.is_valid(): + data = serializer.data + captcha = Captcha(request) + if not captcha.check(data["captcha"]): + return error_response(u"验证码错误") + try: + user = User.objects.get(username=data["username"], email=data["email"]) + except User.DoesNotExist: + return error_response(u"用户不存在") + user.reset_password_token = rand_str() + user.save() + # todo + email_template = open(settings.TEMPLATES[0]["DIRS"][0] + "utils/reset_password_email.html", "r").read() + email_template.replace("{{ username }}", user.username).replace("{{ link }}", "/reset_password/?token=" + user.reset_password_token) + return success_response(u"邮件发生成功") + else: + return serializer_invalid_response(serializer) + + +class ResetPasswordAPIView(APIView): + pass + + +def user_index_page(request, username): + return render(request, "oj/account/user_index.html") diff --git a/contest/migrations/0011_contestrank.py b/contest/migrations/0011_contestrank.py new file mode 100644 index 00000000..c014d7a9 --- /dev/null +++ b/contest/migrations/0011_contestrank.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.conf import settings +import utils.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('contest', '0010_auto_20150922_1703'), + ] + + operations = [ + migrations.CreateModel( + name='ContestRank', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('total_submission_number', models.IntegerField(default=0)), + ('total_ac_number', models.IntegerField(default=0)), + ('total_time', models.IntegerField(default=0)), + ('submission_info', utils.models.JsonField(default={})), + ('contest', models.ForeignKey(to='contest.Contest')), + ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/contest/models.py b/contest/models.py index 5786958c..1e4f45aa 100644 --- a/contest/models.py +++ b/contest/models.py @@ -6,6 +6,8 @@ from account.models import User from problem.models import AbstractProblem from group.models import Group from utils.models import RichTextField +from jsonfield import JSONField +from judge.judger.result import result GROUP_CONTEST = 0 @@ -104,3 +106,66 @@ class ContestSubmission(models.Model): class Meta: db_table = "contest_submission" + + +class ContestRank(models.Model): + user = models.ForeignKey(User) + contest = models.ForeignKey(Contest) + total_submission_number = models.IntegerField(default=0) + total_ac_number = models.IntegerField(default=0) + # ac 的题目才要加到这个字段里面 = ac 时间 + 错误次数 * 20 * 60 + # 没有 ac 的题目不计算罚时 单位是秒 + total_time = models.IntegerField(default=0) + # 数据结构{23: {"is_ac": True, "ac_time": 8999, "error_number": 2, "is_first_ac": True}} + # key 是比赛题目的id + submission_info = JSONField(default={}) + + def update_rank(self, submission): + if not submission.contest_id or submission.contest_id != self.contest_id: + raise ValueError("Error submission type") + + # 这道题以前提交过 + if str(submission.problem_id) in self.submission_info: + info = self.submission_info[str(submission.problem_id)] + # 如果这道题目已经 ac 了就跳过 + if info["is_ac"]: + return + + self.total_submission_number += 1 + + if submission.result == result["accepted"]: + + self.total_ac_number += 1 + + info["is_ac"] = True + info["ac_time"] = (submission.create_time - self.contest.start_time).total_seconds() + + # 之前已经提交过,但是是错误的,这次提交是正确的。错误的题目不计入罚时 + self.total_time += (info["ac_time"] + info["error_number"] * 20 * 60) + problem = ContestProblem.objects.get(id=submission.problem_id) + if problem.total_accepted_number == 0: + info["is_first_ac"] = True + + else: + info["error_number"] += 1 + info["is_ac"] = False + + else: + # 第一次提交这道题目 + self.total_submission_number += 1 + info = {"is_ac": False, "ac_time": 0, "error_number": 0, "is_first_ac": False} + if submission.result == result["accepted"]: + self.total_ac_number += 1 + info["is_ac"] = True + info["ac_time"] = (submission.create_time - self.contest.start_time).total_seconds() + self.total_time += info["ac_time"] + problem = ContestProblem.objects.get(id=submission.problem_id) + + if problem.total_accepted_number == 0: + info["is_first_ac"] = True + + else: + info["is_ac"] = False + info["error_number"] = 1 + self.submission_info[str(submission.problem_id)] = info + self.save() diff --git a/contest/views.py b/contest/views.py index 8e0a2ac4..ade14079 100644 --- a/contest/views.py +++ b/contest/views.py @@ -16,7 +16,8 @@ from utils.shortcuts import (serializer_invalid_response, error_response, from account.models import SUPER_ADMIN, User from account.decorators import login_required from group.models import Group -from .models import Contest, ContestProblem, ContestSubmission, CONTEST_ENDED, CONTEST_NOT_START, CONTEST_UNDERWAY +from .models import (Contest, ContestProblem, ContestSubmission, CONTEST_ENDED, + CONTEST_NOT_START, CONTEST_UNDERWAY, ContestRank) from .models import GROUP_CONTEST, PUBLIC_CONTEST, PASSWORD_PROTECTED_CONTEST from .decorators import check_user_contest_permission from .serializers import (CreateContestSerializer, ContestSerializer, EditContestSerializer, @@ -334,18 +335,6 @@ def contest_problems_list_page(request, contest_id): """ contest = Contest.objects.get(id=contest_id) contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index") - submissions = ContestSubmission.objects.filter(user=request.user, contest=contest) - state = {} - for item in submissions: - state[item.problem_id] = item.ac - for item in contest_problems: - if item.id in state: - if state[item.id]: - item.state = 1 - else: - item.state = 2 - else: - item.state = 0 return render(request, "oj/contest/contest_problems_list.html", {"contest_problems": contest_problems, "contest": {"id": contest_id}}) @@ -391,81 +380,16 @@ def contest_list_page(request, page=1): "keyword": keyword, "join": join}) -def _cmp(x, y): - if x["total_ac"] > y["total_ac"]: - return 1 - elif x["total_ac"] < y["total_ac"]: - return -1 - else: - if x["total_time"] < y["total_time"]: - return 1 - else: - return -1 - - -def get_the_formatted_time(seconds): - if not seconds: - return "" - result = str(seconds % 60) - if seconds % 60 < 10: - result = "0" + result - result = str((seconds % 3600) / 60) + ":" + result - if (seconds % 3600) / 60 < 10: - result = "0" + result - result = str(seconds / 3600) + ":" + result - if seconds / 3600 < 10: - result = "0" + result - return result - - @check_user_contest_permission def contest_rank_page(request, contest_id): contest = Contest.objects.get(id=contest_id) contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index") - r = redis.Redis(host=REDIS_CACHE["host"], port=REDIS_CACHE["port"], db=REDIS_CACHE["db"]) - if contest.real_time_rank: - # 更新rank - result = ContestSubmission.objects.filter(contest=contest).values("user_id"). \ - annotate(total_submit=Sum("total_submission_number")) - for i in range(0, len(result)): - # 这个人所有的提交 - submissions = ContestSubmission.objects.filter(user_id=result[i]["user_id"], contest_id=contest_id) - result[i]["submissions"] = {} - result[i]["problems"] = [] - for problem in contest_problems: - try: - status = submissions.get(problem=problem) - result[i]["problems"].append({ - "first_achieved": status.first_achieved, - "ac": status.ac, - "failed_number": status.total_submission_number, - "ac_time": get_the_formatted_time(status.ac_time)}) - if status.ac: - result[i]["problems"][-1]["failed_number"] -= 1 - except ContestSubmission.DoesNotExist: - result[i]["problems"].append({}) - result[i]["total_ac"] = submissions.filter(ac=True).count() - user= User.objects.get(id=result[i]["user_id"]) - result[i]["username"] = user.username - result[i]["real_name"] = user.real_name - result[i]["total_time"] = get_the_formatted_time(submissions.filter(ac=True).aggregate(total_time=Sum("total_time"))["total_time"]) - - result = sorted(result, cmp=_cmp, reverse=True) - r.set("contest_rank_" + contest_id, json.dumps(list(result))) - else: - # 从缓存读取排名信息 - result = r.get("contest_rank_" + contest_id) - if result: - result = json.loads(result) - else: - result = [] - + rank = ContestRank.objects.filter(contest_id=contest_id).order_by("-total_ac_number", "total_time") return render(request, "oj/contest/contest_rank.html", - {"contest": contest, "contest_problems": contest_problems, - "result": result, + {"rank": rank, "contest": contest, + "contest_problems": contest_problems, "auto_refresh": request.GET.get("auto_refresh", None) == "true", - "show_real_name": request.GET.get("show_real_name", None) == "true", - "real_time_rank": contest.real_time_rank}) + "show_real_name": request.GET.get("show_real_name", None) == "true",}) class ContestTimeAPIView(APIView): @@ -480,5 +404,4 @@ class ContestTimeAPIView(APIView): return error_response(u"比赛不存在") return success_response({"start": int((contest.start_time - now()).total_seconds() * 1000), "end": int((contest.end_time - now()).total_seconds() * 1000), - "status": contest.status}) - + "status": contest.status}) \ No newline at end of file diff --git a/contest_submission/views.py b/contest_submission/views.py index 59ee7fad..2d35e79b 100644 --- a/contest_submission/views.py +++ b/contest_submission/views.py @@ -49,6 +49,13 @@ class ContestSubmissionAPIView(APIView): judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id) except Exception: return error_response(u"提交判题任务失败") + # 修改用户解题状态 + problems_status = request.user.problems_status + if "contest_problems" not in problems_status: + problems_status["contest_problems"] = {} + problems_status["contest_problems"][str(data["problem_id"])] = 2 + request.user.problems_status = problems_status + request.user.save() # 增加redis 中判题队列长度的计数器 r = redis.Redis(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"]) r.incr("judge_queue_length") diff --git a/dockerfiles/oj_web_server/requirements.txt b/dockerfiles/oj_web_server/requirements.txt index b22146d0..a6cf75ff 100644 --- a/dockerfiles/oj_web_server/requirements.txt +++ b/dockerfiles/oj_web_server/requirements.txt @@ -9,4 +9,5 @@ gunicorn coverage django-extensions supervisor -pillow \ No newline at end of file +pillow +jsonfield \ No newline at end of file diff --git a/mq/scripts/mq.py b/mq/scripts/mq.py index b26a46fd..47231da1 100644 --- a/mq/scripts/mq.py +++ b/mq/scripts/mq.py @@ -10,7 +10,7 @@ from judge.judger_controller.settings import redis_config from judge.judger.result import result from submission.models import Submission from problem.models import Problem -from contest.models import ContestProblem, Contest, ContestSubmission +from contest.models import ContestProblem, Contest, ContestSubmission, CONTEST_UNDERWAY, ContestRank from account.models import User logger = logging.getLogger("app_info") @@ -25,12 +25,20 @@ class MessageQueue(object): while True: submission_id = self.conn.blpop(self.queue, 0)[1] logger.debug("receive submission_id: " + submission_id) + try: submission = Submission.objects.get(id=submission_id) except Submission.DoesNotExist: logger.warning("Submission does not exist, submission_id: " + submission_id) continue + # 更新该用户的解题状态 + try: + user = User.objects.get(pk=submission.user_id) + except User.DoesNotExist: + logger.warning("Submission user does not exist, submission_id: " + submission_id) + continue + if submission.result == result["accepted"] and not submission.contest_id: # 更新普通题目的 ac 计数器 try: @@ -40,15 +48,10 @@ class MessageQueue(object): except Problem.DoesNotExist: logger.warning("Submission problem does not exist, submission_id: " + submission_id) continue - # 更新该用户的解题状态 - try: - user = User.objects.get(pk=submission.user_id) - except User.DoesNotExist: - logger.warning("Submission user does not exist, submission_id: " + submission_id) - continue - problems_status = json.loads(user.problems_status) - problems_status[str(problem.id)] = 1 - user.problems_status = json.dumps(problems_status) + + problems_status = user.problems_status + problems_status["problems"][str(problem.id)] = 1 + user.problems_status = problems_status user.save() # 普通题目的话,到这里就结束了 @@ -57,7 +60,7 @@ class MessageQueue(object): # 能运行到这里的都是比赛题目 try: contest = Contest.objects.get(id=submission.contest_id) - if contest.status != 0: + if contest.status != CONTEST_UNDERWAY: logger.info("Contest debug mode, id: " + str(contest.id) + ", submission id: " + submission_id) continue contest_problem = ContestProblem.objects.get(contest=contest, id=submission.problem_id) @@ -68,50 +71,21 @@ class MessageQueue(object): logger.warning("Submission problem does not exist, submission_id: " + submission_id) continue - try: - contest_submission = ContestSubmission.objects.get(user_id=submission.user_id, contest=contest, - problem_id=contest_problem.id) - # 提交次数加1 - with transaction.atomic(): - if submission.result == result["accepted"]: - # 避免这道题已经 ac 了,但是又重新提交了一遍 - if not contest_submission.ac: - # 这种情况是这个题目前处于错误状态,就使用已经存储了的罚时加上这道题的实际用时 - contest_submission.ac_time = int((submission.create_time - contest.start_time).total_seconds()) - contest_submission.total_time += contest_submission.ac_time - contest_submission.total_submission_number += 1 - # 标记为已经通过 - if contest_problem.total_accepted_number == 0: - contest_submission.first_achieved = True - contest_submission.ac = True - # contest problem ac 计数器加1 - contest_problem.total_accepted_number += 1 - else: - # 如果这个提交是错误的,就罚时20分钟 - contest_submission.total_time += 1200 - contest_submission.total_submission_number += 1 - contest_submission.save() + with transaction.atomic(): + try: + contest_rank = ContestRank.objects.get(contest=contest, user=user) + contest_rank.update_rank(submission) + except ContestRank.DoesNotExist: + ContestRank.objects.create(contest=contest, user=user).update_rank(submission) + + if submission.result == result["accepted"]: + contest_problem.total_accepted_number += 1 contest_problem.save() - except ContestSubmission.DoesNotExist: - # 第一次提交 - with transaction.atomic(): - is_ac = submission.result == result["accepted"] - first_achieved = False - ac_time = 0 - if is_ac: - ac_time = int((submission.create_time - contest.start_time).total_seconds()) - total_time = int((submission.create_time - contest.start_time).total_seconds()) - # 增加题目总的ac数计数器 - if contest_problem.total_accepted_number == 0: - first_achieved = True - contest_problem.total_accepted_number += 1 - contest_problem.save() - else: - # 没过罚时20分钟 - total_time = 1200 - ContestSubmission.objects.create(user_id=submission.user_id, contest=contest, problem=contest_problem, - ac=is_ac, total_time=total_time, first_achieved=first_achieved, - ac_time=ac_time) + + problems_status = user.problems_status + problems_status["contest_problems"][str(contest_problem.id)] = 1 + user.problems_status = problems_status + user.save() logger.debug("Start message queue") MessageQueue().listen_task() diff --git a/problem/views.py b/problem/views.py index 8dc42d63..3b74ec2b 100644 --- a/problem/views.py +++ b/problem/views.py @@ -305,15 +305,11 @@ def problem_list_page(request, page=1): except Exception: pass - if request.user.is_authenticated(): - problems_status = json.loads(request.user.problems_status) - else: - problems_status = {} # 右侧标签列表 按照关联的题目的数量排序 排除题目数量为0的 tags = ProblemTag.objects.annotate(problem_number=Count("problem")).filter(problem_number__gt=0).order_by("-problem_number") return render(request, "oj/problem/problem_list.html", {"problems": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, - "keyword": keyword, "tag": tag_text,"problems_status": problems_status, + "keyword": keyword, "tag": tag_text, "tags": tags, "difficulty_order": difficulty_order}) diff --git a/submission/views.py b/submission/views.py index e73d3359..06f14039 100644 --- a/submission/views.py +++ b/submission/views.py @@ -53,9 +53,11 @@ class SubmissionAPIView(APIView): logger.error(e) return error_response(u"提交判题任务失败") # 修改用户解题状态 - problems_status = json.loads(request.user.problems_status) - problems_status[str(data["problem_id"])] = 2 - request.user.problems_status = json.dumps(problems_status) + problems_status = request.user.problems_status + if "problems" not in problems_status: + problems_status["problems"] = {} + problems_status["problems"][str(data["problem_id"])] = 2 + request.user.problems_status = problems_status request.user.save() # 增加redis 中判题队列长度的计数器 r = redis.Redis(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"]) @@ -124,7 +126,7 @@ def my_submission(request, submission_id): """ try: result = _get_submission(submission_id, request.user) - submission = request["submission"] + submission = result["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") @@ -231,7 +233,7 @@ class SubmissionShareAPIView(APIView): result = _get_submission(submission_id, request.user) except Submission.DoesNotExist: return error_response(u"提交不存在") - if not request["can_share"]: + if not result["can_share"]: return error_page(request, u"提交不存在") submission = result["submission"] submission.shared = not submission.shared diff --git a/template/src/oj/contest/contest_problems_list.html b/template/src/oj/contest/contest_problems_list.html index 2cf4182d..bca4c089 100644 --- a/template/src/oj/contest/contest_problems_list.html +++ b/template/src/oj/contest/contest_problems_list.html @@ -44,20 +44,7 @@ {% for item in contest_problems %} - - - + {{ item.sort_index }} diff --git a/template/src/oj/contest/contest_rank.html b/template/src/oj/contest/contest_rank.html index 48c9ec60..4b7f9398 100644 --- a/template/src/oj/contest/contest_rank.html +++ b/template/src/oj/contest/contest_rank.html @@ -3,6 +3,7 @@ 比赛排名 {% endblock %} {% block body %} + {% load contest %}