diff --git a/account/migrations/0002_user_problems_status.py b/account/migrations/0002_user_problems_status.py new file mode 100644 index 00000000..58c8a31c --- /dev/null +++ b/account/migrations/0002_user_problems_status.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='problems_status', + field=models.TextField(blank=True), + ), + ] diff --git a/account/models.py b/account/models.py index df25329c..754e178d 100644 --- a/account/models.py +++ b/account/models.py @@ -30,6 +30,9 @@ class User(AbstractBaseUser): create_time = models.DateTimeField(auto_now_add=True) # 0代表不是管理员 1是普通管理员 2是超级管理员 admin_type = models.IntegerField(default=0) + # JSON字典用来表示该用户的问题的解决状态 1为ac,2为正在进行 + problems_status = models.TextField(blank=True) + USERNAME_FIELD = 'username' REQUIRED_FIELDS = [] diff --git a/mq/scripts/info.py b/mq/scripts/info.py index 01ef6b2d..41a21273 100644 --- a/mq/scripts/info.py +++ b/mq/scripts/info.py @@ -2,6 +2,7 @@ import logging import redis +import json from judge.judger_controller.settings import redis_config from judge.judger.result import result @@ -9,6 +10,7 @@ from submission.models import Submission from problem.models import Problem from contest.models import ContestProblem, Contest, ContestSubmission from account.models import User + logger = logging.getLogger("app_info") @@ -35,6 +37,20 @@ class MessageQueue(object): problem.save() except Problem.DoesNotExist: logger.warning("Submission problem does not exist, submission_id: " + submission_id) + # 更新该用户的解题状态 + 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 user.problems_status: + problems_status = json.loads(user.problems_status) + else: + problems_status = {} + problems_status[str(problem.id)] = 1 + user.problems_status = json.dumps(problems_status) + user.save() + # 普通题目的话,到这里就结束了 continue diff --git a/problem/views.py b/problem/views.py index c66b90ed..18d7adf4 100644 --- a/problem/views.py +++ b/problem/views.py @@ -282,11 +282,16 @@ def problem_list_page(request, page=1): except Exception: pass + if request.user.is_authenticated() and request.user.problems_status: + problems_status = json.loads(request.user.problems_status) + else: + problems_status = {} + print 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, + "keyword": keyword, "tag": tag_text,"problems_status": problems_status, "tags": tags, "difficulty_order": difficulty_order}) diff --git a/submission/views.py b/submission/views.py index 45e1875c..baa81d4d 100644 --- a/submission/views.py +++ b/submission/views.py @@ -46,7 +46,14 @@ class SubmissionAPIView(APIView): judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id) except Exception: return error_response(u"提交判题任务失败") - + # 修改用户解题状态 + if request.user.problems_status: + problems_status = json.loads(request.user.problems_status) + else: + problems_status = {} + problems_status[str(data["problem_id"])] = 2 + request.user.problems_status = json.dumps(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/template/src/oj/problem/problem_list.html b/template/src/oj/problem/problem_list.html index 7a9b1dce..1a1cd139 100644 --- a/template/src/oj/problem/problem_list.html +++ b/template/src/oj/problem/problem_list.html @@ -28,7 +28,7 @@ {% for item in problems %} - + {{ item.id }} {{ item.title }} diff --git a/utils/templatetags/problem.py b/utils/templatetags/problem.py index 412c91db..31d290df 100644 --- a/utils/templatetags/problem.py +++ b/utils/templatetags/problem.py @@ -8,7 +8,16 @@ def get_problem_accepted_radio(problem): return "0%" +def get_problem_status(problems_status, problem_id): + + if str(problem_id) in problems_status: + if problems_status[str(problem_id)] == 1: + return "glyphicon glyphicon-ok ac-flag" + return "glyphicon glyphicon-minus dealing-flag" + return "" + from django import template register = template.Library() register.filter("accepted_radio", get_problem_accepted_radio) +register.simple_tag(get_problem_status, name="get_problem_status")