From 2a8d7ca35c65343174c9a8e12843b48b13bf8e25 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Fri, 18 Sep 2015 21:48:54 +0800 Subject: [PATCH 01/22] Merge branch 'debug' of https://git.coding.net/virusdefender/qduoj into debug --- dockerfiles/judger/Dockerfile | 1 + dockerfiles/judger/policy | 3 ++ dockerfiles/oj_web_server/mq.conf | 2 +- judge/judger/client.py | 2 + judge/judger/language.py | 5 ++- judge/judger_controller/tasks.py | 2 +- mq/scripts/{info.py => mq.py} | 0 static/src/js/app/oj/problem/problem.js | 4 +- tools/run.py | 58 +++++++++++++++++++++++++ 9 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 dockerfiles/judger/policy rename mq/scripts/{info.py => mq.py} (100%) create mode 100644 tools/run.py diff --git a/dockerfiles/judger/Dockerfile b/dockerfiles/judger/Dockerfile index 7718e06f..6090728e 100644 --- a/dockerfiles/judger/Dockerfile +++ b/dockerfiles/judger/Dockerfile @@ -18,4 +18,5 @@ RUN git clone https://github.com/quark-zju/lrun.git RUN cd lrun && make install RUN mkdir -p /var/judger/run/ && mkdir /var/judger/test_case/ && mkdir /var/judger/code/ RUN chmod -R 777 /var/judger/run/ +COPY policy /var/judger/run/ WORKDIR /var/judger/code/ \ No newline at end of file diff --git a/dockerfiles/judger/policy b/dockerfiles/judger/policy new file mode 100644 index 00000000..a057b210 --- /dev/null +++ b/dockerfiles/judger/policy @@ -0,0 +1,3 @@ +grant { + permission java.io.FilePermission "/tmp", "read"; +}; \ No newline at end of file diff --git a/dockerfiles/oj_web_server/mq.conf b/dockerfiles/oj_web_server/mq.conf index 9dbce3e4..ae1797c6 100644 --- a/dockerfiles/oj_web_server/mq.conf +++ b/dockerfiles/oj_web_server/mq.conf @@ -2,7 +2,7 @@ command=python manage.py runscript mq -directory=/code/qduoj/ +directory=/code/ user=root numprocs=1 stdout_logfile=/code/log/mq.log diff --git a/judge/judger/client.py b/judge/judger/client.py index 77405879..a5a1f471 100644 --- a/judge/judger/client.py +++ b/judge/judger/client.py @@ -62,6 +62,8 @@ class JudgeClient(object): " --max-real-time " + str(self._max_real_time / 1000.0 * 2) + \ " --max-memory " + str(self._max_memory * 1000 * 1000) + \ " --network false" + \ + " --syscalls '" + self._language["syscalls"] + "'" + \ + " --max-nprocess 20" + \ " --uid " + str(lrun_uid) + \ " --gid " + str(lrun_gid) diff --git a/judge/judger/language.py b/judge/judger/language.py index a61bd32b..c7a14eb4 100644 --- a/judge/judger/language.py +++ b/judge/judger/language.py @@ -6,6 +6,7 @@ languages = { "name": "c", "src_name": "main.c", "code": 1, + "syscalls": "!execve:k,flock:k,ptrace:k,sync:k,fdatasync:k,fsync:k,msync,sync_file_range:k,syncfs:k,unshare:k,setns:k,clone:k,query_module:k,sysinfo:k,syslog:k,sysfs:k", "compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 {src_path} -lm -o {exe_path}main", "execute_command": "{exe_path}main" }, @@ -13,6 +14,7 @@ languages = { "name": "cpp", "src_name": "main.cpp", "code": 2, + "syscalls": "!execve:k,flock:k,ptrace:k,sync:k,fdatasync:k,fsync:k,msync,sync_file_range:k,syncfs:k,unshare:k,setns:k,clone:k,query_module:k,sysinfo:k,syslog:k,sysfs:k", "compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 {src_path} -lm -o {exe_path}main", "execute_command": "{exe_path}main" }, @@ -20,8 +22,9 @@ languages = { "name": "java", "src_name": "Main.java", "code": 3, + "syscalls": "!execve:k,flock:k,ptrace:k,sync:k,fdatasync:k,fsync:k,msync,sync_file_range:k,syncfs:k,unshare:k,setns:k,clone[a&268435456==268435456]:k,query_module:k,sysinfo:k,syslog:k,sysfs:k", "compile_command": "javac {src_path} -d {exe_path}", - "execute_command": "java -cp {exe_path} Main" + "execute_command": "java -cp {exe_path} -Djava.security.manager -Djava.security.policy==policy Main" } } diff --git a/judge/judger_controller/tasks.py b/judge/judger_controller/tasks.py index 45749829..2de71d75 100644 --- a/judge/judger_controller/tasks.py +++ b/judge/judger_controller/tasks.py @@ -32,7 +32,7 @@ def judge(submission_id, time_limit, memory_limit, test_case_id): passwd=submission_db["password"], host=submission_db["host"], port=submission_db["port"], - character="utf8") + charset="utf8") cur = conn.cursor() cur.execute("update submission set result=%s, info=%s where id=%s", diff --git a/mq/scripts/info.py b/mq/scripts/mq.py similarity index 100% rename from mq/scripts/info.py rename to mq/scripts/mq.py diff --git a/static/src/js/app/oj/problem/problem.js b/static/src/js/app/oj/problem/problem.js index 4817309d..fe51417c 100644 --- a/static/src/js/app/oj/problem/problem.js +++ b/static/src/js/app/oj/problem/problem.js @@ -120,12 +120,12 @@ require(["jquery", "codeMirror", "csrfToken", "bsAlert", "ZeroClipboard"], if (code.indexOf("using namespace std") > -1||code.indexOf("") > -1) { return "2"; } - if (code.indexOf("printf")) + if (code.indexOf("printf") > -1) { return "1"; } //java - if (code.indexOf("public class Main")) { + if (code.indexOf("public class Main") > -1) { return "3"; } } diff --git a/tools/run.py b/tools/run.py new file mode 100644 index 00000000..b98c02b8 --- /dev/null +++ b/tools/run.py @@ -0,0 +1,58 @@ +# coding=utf-8 +import os +import json + +os.system("docker rm -f redis") +os.system("docker rm -f mysql") +os.system("docker rm -f oj_web_server") + +if os.system("docker run --name mysql -v /root/data:/var/lib/mysql -v /root/data/my.cnf:/etc/my.cnf -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server:latest"): + print "Error start mysql" + exit() + +if os.system("docker run --name redis -d redis"): + print "Error start redis" + exit() + +if os.system("docker run --name oj_web_server -e oj_env=server -v /root/qduoj:/code -v /root/test_case:/code/test_case -v /root/log:/code/log -v /root/upload:/code/upload -v /root/qduoj/dockerfiles/oj_web_server/supervisord.conf:/etc/supervisord.conf -v /root/qduoj/dockerfiles/oj_web_server/gunicorn.conf:/etc/gunicorn.conf -v /root/qduoj/dockerfiles/oj_web_server/mq.conf:/etc/mq.conf -d -p 127.0.0.1:8080:8080 --link mysql --link=redis oj_web_server"): + print "Erro start oj_web_server" + exit() + +inspect_redis = json.loads(os.popen("docker inspect redis").read()) + +if not inspect_redis: + print "Error when inspect redis ip" + exit() + +redis_ip = inspect_redis[0]["NetworkSettings"]["IPAddress"] +print "redis ip ", redis_ip + + +inspect_mysql = json.loads(os.popen("docker inspect mysql").read()) +if not inspect_mysql: + print "Error when inspect mysql ip" + exit() + +mysql_ip = inspect_mysql[0]["NetworkSettings"]["IPAddress"] +print "mysql ip ", mysql_ip + + +f = open("/etc/profile", "r") +content = "" +for line in f.readlines(): + if line.startswith("export REDIS_PORT_6379_TCP_ADDR"): + content += ("\nexport REDIS_PORT_6379_TCP_ADDR=" + redis_ip + "\n") + elif line.startswith("export submission_db_host"): + content += ("\nexport submission_db_host=" + mysql_ip + "\n") + else: + content += line + +f.close() + + +f = open("/etc/profile", "w") +f.write(content) +f.close() + +print "Please run source /etc/profile" + From b224a823fe7dc87ed9ad3aa6e3787c3283d13e65 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 22 Sep 2015 16:26:42 +0800 Subject: [PATCH 02/22] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20demo=20=E7=BD=91?= =?UTF-8?q?=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cdf478d7..d3e2a57d 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,12 @@ 文档:https://www.zybuluo.com/virusdefender/note/171932 +demo: https://qduoj.com + TODO: - 完善文档,目前还差很多 - 完善测试 - - 搭建 demo 站点 ![oj_previewindex.png][1] From c26fd6734d1dd9f249582c38acf0a79ad3075f98 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 22 Sep 2015 17:03:53 +0800 Subject: [PATCH 03/22] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=8C=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=20xss=20=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0003_auto_20150922_1703.py | 20 ++ announcement/models.py | 3 +- contest/migrations/0010_auto_20150922_1703.py | 25 +++ contest/models.py | 4 +- problem/migrations/0008_auto_20150922_1702.py | 20 ++ problem/models.py | 3 +- utils/models.py | 14 ++ utils/xss_filter.py | 202 ++++++++++++++++++ 8 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 announcement/migrations/0003_auto_20150922_1703.py create mode 100644 contest/migrations/0010_auto_20150922_1703.py create mode 100644 problem/migrations/0008_auto_20150922_1702.py create mode 100644 utils/models.py create mode 100644 utils/xss_filter.py diff --git a/announcement/migrations/0003_auto_20150922_1703.py b/announcement/migrations/0003_auto_20150922_1703.py new file mode 100644 index 00000000..4019377d --- /dev/null +++ b/announcement/migrations/0003_auto_20150922_1703.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 = [ + ('announcement', '0002_auto_20150818_1445'), + ] + + operations = [ + migrations.AlterField( + model_name='announcement', + name='content', + field=utils.models.RichTextField(), + ), + ] diff --git a/announcement/models.py b/announcement/models.py index c3267f12..7442721a 100644 --- a/announcement/models.py +++ b/announcement/models.py @@ -3,13 +3,14 @@ from django.db import models from account.models import User from group.models import Group +from utils.models import RichTextField class Announcement(models.Model): # 标题 title = models.CharField(max_length=50) # 公告的内容 HTML 格式 - content = models.TextField() + content = RichTextField() # 创建时间 create_time = models.DateTimeField(auto_now_add=True) # 这个公告是谁创建的 diff --git a/contest/migrations/0010_auto_20150922_1703.py b/contest/migrations/0010_auto_20150922_1703.py new file mode 100644 index 00000000..1a1609e5 --- /dev/null +++ b/contest/migrations/0010_auto_20150922_1703.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import utils.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0009_contestsubmission_first_achieved'), + ] + + operations = [ + migrations.AlterField( + model_name='contest', + name='description', + field=utils.models.RichTextField(), + ), + migrations.AlterField( + model_name='contestproblem', + name='description', + field=utils.models.RichTextField(), + ), + ] diff --git a/contest/models.py b/contest/models.py index a54619eb..5786958c 100644 --- a/contest/models.py +++ b/contest/models.py @@ -5,6 +5,8 @@ from django.utils.timezone import now from account.models import User from problem.models import AbstractProblem from group.models import Group +from utils.models import RichTextField + GROUP_CONTEST = 0 PUBLIC_CONTEST = 1 @@ -17,7 +19,7 @@ CONTEST_UNDERWAY = 0 class Contest(models.Model): title = models.CharField(max_length=40, unique=True) - description = models.TextField() + description = RichTextField() # 比赛模式:0 即为是acm模式,1 即为是按照总的 ac 题目数量排名模式 mode = models.IntegerField() # 是否显示实时排名结果 diff --git a/problem/migrations/0008_auto_20150922_1702.py b/problem/migrations/0008_auto_20150922_1702.py new file mode 100644 index 00000000..ae499a0f --- /dev/null +++ b/problem/migrations/0008_auto_20150922_1702.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 = [ + ('problem', '0007_remove_problem_last_update_time'), + ] + + operations = [ + migrations.AlterField( + model_name='problem', + name='description', + field=utils.models.RichTextField(), + ), + ] diff --git a/problem/models.py b/problem/models.py index dbc886b6..adb7dd4d 100644 --- a/problem/models.py +++ b/problem/models.py @@ -2,6 +2,7 @@ from django.db import models from account.models import User +from utils.models import RichTextField class ProblemTag(models.Model): @@ -15,7 +16,7 @@ class AbstractProblem(models.Model): # 标题 title = models.CharField(max_length=50) # 问题描述 HTML 格式 - description = models.TextField() + description = RichTextField() # 输入描述 input_description = models.CharField(max_length=10000) # 输出描述 diff --git a/utils/models.py b/utils/models.py new file mode 100644 index 00000000..f9844903 --- /dev/null +++ b/utils/models.py @@ -0,0 +1,14 @@ +# coding=utf-8 +from django.db import models + +from utils.xss_filter import XssHtml + + +class RichTextField(models.TextField): + __metaclass__ = models.SubfieldBase + + def get_prep_value(self, value): + parser = XssHtml() + parser.feed(value) + parser.close() + return parser.getHtml() \ No newline at end of file diff --git a/utils/xss_filter.py b/utils/xss_filter.py new file mode 100644 index 00000000..825fbc97 --- /dev/null +++ b/utils/xss_filter.py @@ -0,0 +1,202 @@ +# -*- coding: utf-8 -*- +""" +Python 富文本XSS过滤类 +@package XssHtml +@version 0.1 +@link http://phith0n.github.io/python-xss-filter +@since 20150407 +@copyright (c) Phithon All Rights Reserved + +Based on native Python module HTMLParser purifier of HTML, To Clear all javascript in html +You can use it in all python web framework +Written by Phithon in 2015 and placed in the public domain. +phithon 编写于20150407 +From: XDSEC & 离别歌 +GitHub Pages: https://github.com/phith0n/python-xss-filter +Usage: + parser = XssHtml() + parser.feed('') + parser.close() + html = parser.getHtml() + print html + +Requirements +Python 2.6+ or 3.2+ +Cannot defense xss in browser which is belowed IE7 +浏览器版本:IE7+ 或其他浏览器,无法防御IE6及以下版本浏览器中的XSS +""" +import re + +try: + from html.parser import HTMLParser +except: + from HTMLParser import HTMLParser + + +class XssHtml(HTMLParser): + allow_tags = ['a', 'img', 'br', 'strong', 'b', 'code', 'pre', + 'p', 'div', 'em', 'span', 'h1', 'h2', 'h3', 'h4', + 'h5', 'h6', 'blockquote', 'ul', 'ol', 'tr', 'th', 'td', + 'hr', 'li', 'u', 'embed', 's', 'table', 'thead', 'tbody', + 'caption', 'small', 'q', 'sup', 'sub'] + common_attrs = ["style", "class", "name"] + nonend_tags = ["img", "hr", "br", "embed"] + tags_own_attrs = { + "img": ["src", "width", "height", "alt", "align"], + "a": ["href", "target", "rel", "title"], + "embed": ["src", "width", "height", "type", "allowfullscreen", "loop", "play", "wmode", "menu"], + "table": ["border", "cellpadding", "cellspacing"], + } + + def __init__(self, allows=[]): + HTMLParser.__init__(self) + self.allow_tags = allows if allows else self.allow_tags + self.result = [] + self.start = [] + self.data = [] + + def getHtml(self): + """ + Get the safe html code + """ + for i in range(0, len(self.result)): + tmp = self.result[i].rstrip('\n') + tmp = tmp.lstrip('\n') + if tmp: + self.data.append(tmp) + return ''.join(self.data) + + def handle_startendtag(self, tag, attrs): + self.handle_starttag(tag, attrs) + + def handle_starttag(self, tag, attrs): + if tag not in self.allow_tags: + return + end_diagonal = ' /' if tag in self.nonend_tags else '' + if not end_diagonal: + self.start.append(tag) + attdict = {} + for attr in attrs: + attdict[attr[0]] = attr[1] + + attdict = self._wash_attr(attdict, tag) + if hasattr(self, "node_%s" % tag): + attdict = getattr(self, "node_%s" % tag)(attdict) + else: + attdict = self.node_default(attdict) + + attrs = [] + for (key, value) in attdict.items(): + attrs.append('%s="%s"' % (key, self._htmlspecialchars(value))) + attrs = (' ' + ' '.join(attrs)) if attrs else '' + self.result.append('<' + tag + attrs + end_diagonal + '>') + + def handle_endtag(self, tag): + if self.start and tag == self.start[len(self.start) - 1]: + self.result.append('') + self.start.pop() + + def handle_data(self, data): + self.result.append(self._htmlspecialchars(data)) + + def handle_entityref(self, name): + if name.isalpha(): + self.result.append("&%s;" % name) + + def handle_charref(self, name): + if name.isdigit(): + self.result.append("&#%s;" % name) + + def node_default(self, attrs): + attrs = self._common_attr(attrs) + return attrs + + def node_a(self, attrs): + attrs = self._common_attr(attrs) + attrs = self._get_link(attrs, "href") + attrs = self._set_attr_default(attrs, "target", "_blank") + attrs = self._limit_attr(attrs, { + "target": ["_blank", "_self"] + }) + return attrs + + def node_embed(self, attrs): + attrs = self._common_attr(attrs) + attrs = self._get_link(attrs, "src") + attrs = self._limit_attr(attrs, { + "type": ["application/x-shockwave-flash"], + "wmode": ["transparent", "window", "opaque"], + "play": ["true", "false"], + "loop": ["true", "false"], + "menu": ["true", "false"], + "allowfullscreen": ["true", "false"] + }) + attrs["allowscriptaccess"] = "never" + attrs["allownetworking"] = "none" + return attrs + + def _true_url(self, url): + prog = re.compile(r"^(http|https|ftp)://.+", re.I | re.S) + if prog.match(url): + return url + else: + return "http://%s" % url + + def _true_style(self, style): + if style: + style = re.sub(r"(\\|&#|/\*|\*/)", "_", style) + style = re.sub(r"e.*x.*p.*r.*e.*s.*s.*i.*o.*n", "_", style) + return style + + def _get_style(self, attrs): + if "style" in attrs: + attrs["style"] = self._true_style(attrs.get("style")) + return attrs + + def _get_link(self, attrs, name): + if name in attrs: + attrs[name] = self._true_url(attrs[name]) + return attrs + + def _wash_attr(self, attrs, tag): + if tag in self.tags_own_attrs: + other = self.tags_own_attrs.get(tag) + else: + other = [] + if attrs: + for (key, value) in attrs.items(): + if key not in self.common_attrs + other: + del attrs[key] + return attrs + + def _common_attr(self, attrs): + attrs = self._get_style(attrs) + return attrs + + def _set_attr_default(self, attrs, name, default=''): + if name not in attrs: + attrs[name] = default + return attrs + + def _limit_attr(self, attrs, limit={}): + for (key, value) in limit.items(): + if key in attrs and attrs[key] not in value: + del attrs[key] + return attrs + + def _htmlspecialchars(self, html): + return html.replace("<", "<") \ + .replace(">", ">") \ + .replace('"', """) \ + .replace("'", "'") + + +if "__main__" == __name__: + parser = XssHtml() + parser.feed("""

+
hehe
+

>M + MM

+ """) + parser.close() + print(parser.getHtml()) From 5c334a4ed3fff3399cce66eace02f83e34ce99d0 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 22 Sep 2015 18:52:15 +0800 Subject: [PATCH 04/22] add oj text logo --- oj/__init__.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/oj/__init__.py b/oj/__init__.py index e69de29b..c19c0794 100644 --- a/oj/__init__.py +++ b/oj/__init__.py @@ -0,0 +1,9 @@ +""" + ___ _ _ __ _ _ _ + /___\ _ __ | |(_) _ __ ___ \ \ _ _ __| | __ _ ___ | |__ _ _ __ _ __| | _ _ + // //| '_ \ | || || '_ \ / _ \ \ \| | | | / _` | / _` | / _ \ | '_ \ | | | | / _` | / _` || | | | +/ \_// | | | || || || | | || __/ /\_/ /| |_| || (_| || (_| || __/ | |_) || |_| | | (_| || (_| || |_| | +\___/ |_| |_||_||_||_| |_| \___| \___/ \__,_| \__,_| \__, | \___| |_.__/ \__, | \__, | \__,_| \__,_| + |___/ |___/ |_| +https://github.com/QingdaoU/OnlineJudge +""" \ No newline at end of file From 4ca2b458b37503eb3e0d0a600026bedd4fdd8cae Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:25:13 +0800 Subject: [PATCH 05/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=97=B6=E5=80=99=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- submission/views.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/submission/views.py b/submission/views.py index 05dbd1ed..e73d3359 100644 --- a/submission/views.py +++ b/submission/views.py @@ -10,8 +10,8 @@ from rest_framework.views import APIView from judge.judger_controller.tasks import judge from judge.judger_controller.settings import redis_config -from account.decorators import login_required -from account.models import SUPER_ADMIN, User +from account.decorators import login_required, super_admin_required +from account.models import SUPER_ADMIN, User, REGULAR_USER from problem.models import Problem from contest.models import ContestProblem, Contest @@ -104,14 +104,17 @@ def _get_submission(submission_id, user): """ submission = Submission.objects.get(id=submission_id) # 超级管理员或者提交者自己或者是一个分享的提交 - if user.admin_type == SUPER_ADMIN or submission.user_id == user.id or submission.shared: - return submission + if user.admin_type == SUPER_ADMIN or submission.user_id == user.id: + return {"submission": submission, "can_share": True} if submission.contest_id: contest = Contest.objects.get(id=submission.contest_id) # 比赛提交的话,比赛创建者也可见 if contest.created_by == user: - return submission - raise Submission.DoesNotExist + return {"submission": submission, "can_share": True} + if submission.shared: + return {"submission": submission, "can_share": False} + else: + raise Submission.DoesNotExist @login_required @@ -120,7 +123,8 @@ def my_submission(request, submission_id): 单个题目的提交详情页 """ try: - submission = _get_submission(submission_id, request.user) + result = _get_submission(submission_id, request.user) + submission = request["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") @@ -143,8 +147,10 @@ def my_submission(request, submission_id): info = submission.info else: info = None + user = User.objects.get(id=submission.user_id) return render(request, "oj/problem/my_submission.html", - {"submission": submission, "problem": problem, "info": info}) + {"submission": submission, "problem": problem, "info": info, + "user": user, "can_share": result["can_share"]}) class SubmissionAdminAPIView(APIView): @@ -222,9 +228,12 @@ class SubmissionShareAPIView(APIView): if serializer.is_valid(): submission_id = serializer.data["submission_id"] try: - submission = _get_submission(submission_id, request.user) + result = _get_submission(submission_id, request.user) except Submission.DoesNotExist: return error_response(u"提交不存在") + if not request["can_share"]: + return error_page(request, u"提交不存在") + submission = result["submission"] submission.shared = not submission.shared submission.save() return success_response(submission.shared) @@ -233,6 +242,7 @@ class SubmissionShareAPIView(APIView): class SubmissionRejudgeAdminAPIView(APIView): + @super_admin_required def post(self, request): serializer = SubmissionRejudgeSerializer(data=request.data) if serializer.is_valid(): From aa4ed9b9b2d557e11af2336270c9bec9ebe65f96 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:30:00 +0800 Subject: [PATCH 06/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNone=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=20xss=20filter=20=E5=B7=A5=E4=BD=9C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/models.py b/utils/models.py index f9844903..99e39448 100644 --- a/utils/models.py +++ b/utils/models.py @@ -8,6 +8,8 @@ class RichTextField(models.TextField): __metaclass__ = models.SubfieldBase def get_prep_value(self, value): + if not value: + return value parser = XssHtml() parser.feed(value) parser.close() From 6335a3a1eaa162a24bacc285afaec621ecf9431c Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:31:15 +0800 Subject: [PATCH 07/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20None=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=20xss=20filter=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/models.py b/utils/models.py index f9844903..99e39448 100644 --- a/utils/models.py +++ b/utils/models.py @@ -8,6 +8,8 @@ class RichTextField(models.TextField): __metaclass__ = models.SubfieldBase def get_prep_value(self, value): + if not value: + return value parser = XssHtml() parser.feed(value) parser.close() From eec6c0e37a5b9b4f680ba67322af2373e3e05946 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:33:14 +0800 Subject: [PATCH 08/22] fix typo --- submission/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submission/views.py b/submission/views.py index e73d3359..25ee23a1 100644 --- a/submission/views.py +++ b/submission/views.py @@ -124,7 +124,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"提交不存在") From 7b2a871f475ce51b2759e662b2bb9fc4a85705c0 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:34:54 +0800 Subject: [PATCH 09/22] fix typo --- submission/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submission/views.py b/submission/views.py index 25ee23a1..4464cd1d 100644 --- a/submission/views.py +++ b/submission/views.py @@ -231,7 +231,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 From e95bf8e16283f6ca39591bdb8d844c0286f68255 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:37:29 +0800 Subject: [PATCH 10/22] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E4=B8=AD=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E8=83=BD=E5=90=A6=E5=88=86=E4=BA=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/problem/my_submission.html | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/template/src/oj/problem/my_submission.html b/template/src/oj/problem/my_submission.html index cd87dc00..5c7ba4e6 100644 --- a/template/src/oj/problem/my_submission.html +++ b/template/src/oj/problem/my_submission.html @@ -65,17 +65,18 @@
- -
- {% if submission.shared %} - - {% else %} - - {% endif %} - -
+ {% if can_share %} +
+ {% if submission.shared %} + + {% else %} + + {% endif %} + +
+ {% endif %} @@ -102,7 +103,7 @@ url: "/api/submission/share/", method: "post", data: {submission_id: location.href.split("/")[4]}, - success: function(data){ + success: function (data) { location.reload(); } }) From 8fbc774dee9e1d54dd32ced6cc31f18f9ce3b11e Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 23 Sep 2015 21:41:37 +0800 Subject: [PATCH 11/22] =?UTF-8?q?=E4=B8=8D=E6=98=AF=E6=9C=AC=E4=BA=BA?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E4=BA=A4=E5=B0=B1=E5=A2=9E=E5=8A=A0=E4=BD=9C?= =?UTF-8?q?=E8=80=85=E7=94=A8=E6=88=B7=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/problem/my_submission.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/template/src/oj/problem/my_submission.html b/template/src/oj/problem/my_submission.html index 5c7ba4e6..c458cd59 100644 --- a/template/src/oj/problem/my_submission.html +++ b/template/src/oj/problem/my_submission.html @@ -40,6 +40,9 @@ {{ submission.result|translate_result }} + {% ifnotequal request.user.id submission.user_id %} +

作者:{{ user.username }}

+ {% endifnotequal %} {% ifequal submission.result 0 %}

时间 : {{ submission.accepted_answer_time }}ms 语言 : {{ submission.language|translate_language }} @@ -74,7 +77,7 @@ {% endif %} +{{ request.build_absolute_uri }} {% endif %} From 7eea999277196ddc2a7340d7e095573f97e0e44f Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 14:55:20 +0800 Subject: [PATCH 12/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20ContestRank=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/models.py | 6 ++- account/views.py | 46 +++++++++++++++++++--- contest/models.py | 66 ++++++++++++++++++++++++++++++- contest_submission/views.py | 5 +++ mq/scripts/mq.py | 77 +++++++++++-------------------------- submission/views.py | 6 +-- utils/models.py | 15 +++++++- 7 files changed, 155 insertions(+), 66 deletions(-) diff --git a/account/models.py b/account/models.py index 32453336..1ba842dc 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 utils.models 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/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/models.py b/contest/models.py index 5786958c..1897eb76 100644 --- a/contest/models.py +++ b/contest/models.py @@ -5,7 +5,8 @@ from django.utils.timezone import now from account.models import User from problem.models import AbstractProblem from group.models import Group -from utils.models import RichTextField +from utils.models import RichTextField, JsonField +from judge.judger.result import result GROUP_CONTEST = 0 @@ -104,3 +105,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 submission.problem_id in self.problem_info: + info = self.submission_info[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_time"] * 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[submission.problem_id] = info + self.save() diff --git a/contest_submission/views.py b/contest_submission/views.py index 59ee7fad..8611ff48 100644 --- a/contest_submission/views.py +++ b/contest_submission/views.py @@ -49,6 +49,11 @@ 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 + 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/mq/scripts/mq.py b/mq/scripts/mq.py index b26a46fd..caae75ee 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,16 @@ 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) logger.debug("Start message queue") MessageQueue().listen_task() diff --git a/submission/views.py b/submission/views.py index 4464cd1d..7a4d7ea1 100644 --- a/submission/views.py +++ b/submission/views.py @@ -53,9 +53,9 @@ 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 + 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"]) diff --git a/utils/models.py b/utils/models.py index 99e39448..671718c7 100644 --- a/utils/models.py +++ b/utils/models.py @@ -1,4 +1,5 @@ # coding=utf-8 +import json from django.db import models from utils.xss_filter import XssHtml @@ -9,8 +10,18 @@ class RichTextField(models.TextField): def get_prep_value(self, value): if not value: - return value + value = "" parser = XssHtml() parser.feed(value) parser.close() - return parser.getHtml() \ No newline at end of file + return parser.getHtml() + + +class JsonField(models.TextField): + __metaclass__ = models.SubfieldBase + + def get_prep_value(self, value): + return json.dumps(value) + + def to_python(self, value): + return json.loads(value) \ No newline at end of file From 44d094f19ca6eca596fa37c36c9c1e7d90e18f7f Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 15:28:55 +0800 Subject: [PATCH 13/22] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=20ac=20=E7=8A=B6=E6=80=81=E7=9A=84=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 12 ------------ mq/scripts/mq.py | 5 +++++ problem/views.py | 6 +----- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/contest/views.py b/contest/views.py index 8e0a2ac4..ccf2c953 100644 --- a/contest/views.py +++ b/contest/views.py @@ -334,18 +334,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}}) diff --git a/mq/scripts/mq.py b/mq/scripts/mq.py index caae75ee..47231da1 100644 --- a/mq/scripts/mq.py +++ b/mq/scripts/mq.py @@ -82,5 +82,10 @@ class MessageQueue(object): contest_problem.total_accepted_number += 1 contest_problem.save() + 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}) From 58df0d423d41800e6b5697e0dddca57ba1f40ac6 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 15:31:16 +0800 Subject: [PATCH 14/22] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20model=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=9A=84=20migrations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/migrations/0006_auto_20150924_1530.py | 20 +++++++++++++ contest/migrations/0011_contestrank.py | 29 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 account/migrations/0006_auto_20150924_1530.py create mode 100644 contest/migrations/0011_contestrank.py 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/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)), + ], + ), + ] From 507ef39e5ddbf3b599ba67cb3a2114a48f1ebf23 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 15:47:05 +0800 Subject: [PATCH 15/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20jsonField=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/models.py | 4 ++-- contest/models.py | 5 +++-- dockerfiles/oj_web_server/requirements.txt | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/account/models.py b/account/models.py index 1ba842dc..6c849ddb 100644 --- a/account/models.py +++ b/account/models.py @@ -2,7 +2,7 @@ from django.db import models from django.contrib.auth.models import AbstractBaseUser -from utils.models import JsonField +from jsonfield import JSONField class AdminGroup(models.Model): @@ -33,7 +33,7 @@ class User(AbstractBaseUser): # 0代表不是管理员 1是普通管理员 2是超级管理员 admin_type = models.IntegerField(default=0) # JSON字典用来表示该用户的问题的解决状态 1为ac,2为正在进行 - problems_status = JsonField(default={}) + problems_status = JSONField(default={}) # 找回密码用的token # reset_password_token = models.CharField(max_length=40, blank=True, null=True) diff --git a/contest/models.py b/contest/models.py index 1897eb76..3ef70308 100644 --- a/contest/models.py +++ b/contest/models.py @@ -5,7 +5,8 @@ from django.utils.timezone import now from account.models import User from problem.models import AbstractProblem from group.models import Group -from utils.models import RichTextField, JsonField +from utils.models import RichTextField +from jsonfield import JSONField from judge.judger.result import result @@ -117,7 +118,7 @@ class ContestRank(models.Model): 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={}) + submission_info = JSONField(default={}) def update_rank(self, submission): if not submission.contest_id or submission.contest_id != self.contest_id: 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 From 82e8ce3ea8e5f00118904189323882e59a23b344 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 15:56:47 +0800 Subject: [PATCH 16/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=20ac=20=E6=A0=87=E5=BF=97=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/serializers.py | 6 ++++++ utils/models.py | 12 +----------- utils/templatetags/problem.py | 4 ++++ 3 files changed, 11 insertions(+), 11 deletions(-) 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/utils/models.py b/utils/models.py index 671718c7..81858412 100644 --- a/utils/models.py +++ b/utils/models.py @@ -14,14 +14,4 @@ class RichTextField(models.TextField): parser = XssHtml() parser.feed(value) parser.close() - return parser.getHtml() - - -class JsonField(models.TextField): - __metaclass__ = models.SubfieldBase - - def get_prep_value(self, value): - return json.dumps(value) - - def to_python(self, value): - return json.loads(value) \ No newline at end of file + return parser.getHtml() \ No newline at end of file diff --git a/utils/templatetags/problem.py b/utils/templatetags/problem.py index 31d290df..1ccfa157 100644 --- a/utils/templatetags/problem.py +++ b/utils/templatetags/problem.py @@ -9,6 +9,9 @@ def get_problem_accepted_radio(problem): def get_problem_status(problems_status, problem_id): + # 用户没登陆 或者 user.problem_status 中没有这个字段都会到导致这里的problem_status 为 "" + if not problems_status: + return "" if str(problem_id) in problems_status: if problems_status[str(problem_id)] == 1: @@ -16,6 +19,7 @@ def get_problem_status(problems_status, problem_id): return "glyphicon glyphicon-minus dealing-flag" return "" + from django import template register = template.Library() From bd121c0adcfac2d1210bbd365e08647c6e8174a9 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 16:01:08 +0800 Subject: [PATCH 17/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=A2=98=E7=9B=AE=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=EF=BC=8Cproblems=5Fstatus=20=E4=B8=AD=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84=20key=20=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest_submission/views.py | 2 ++ submission/views.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/contest_submission/views.py b/contest_submission/views.py index 8611ff48..2d35e79b 100644 --- a/contest_submission/views.py +++ b/contest_submission/views.py @@ -51,6 +51,8 @@ class ContestSubmissionAPIView(APIView): 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() diff --git a/submission/views.py b/submission/views.py index 7a4d7ea1..06f14039 100644 --- a/submission/views.py +++ b/submission/views.py @@ -54,6 +54,8 @@ class SubmissionAPIView(APIView): return error_response(u"提交判题任务失败") # 修改用户解题状态 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() From c0d239ff66ce3ab3afc448e09701229bc3326774 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 16:14:32 +0800 Subject: [PATCH 18/22] =?UTF-8?q?=E6=99=95=E4=BA=86=EF=BC=8C=E6=B2=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81=E5=B0=B1=E5=9C=A8=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E4=B8=8A=E8=B0=83=E8=AF=95=E3=80=82=E3=80=82?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/oj/contest/contest_problems_list.html | 15 +-------------- template/src/oj/problem/problem_list.html | 2 +- 2 files changed, 2 insertions(+), 15 deletions(-) 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/problem/problem_list.html b/template/src/oj/problem/problem_list.html index 842c807d..80319700 100644 --- a/template/src/oj/problem/problem_list.html +++ b/template/src/oj/problem/problem_list.html @@ -31,7 +31,7 @@ {% for item in problems %} - + {{ item.id }} {{ item.title }} From e518c1faa1399d22a7e6eb7f5ebf3639e0c6756a Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 17:57:51 +0800 Subject: [PATCH 19/22] fix typo --- contest/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contest/models.py b/contest/models.py index 3ef70308..7e672041 100644 --- a/contest/models.py +++ b/contest/models.py @@ -125,7 +125,7 @@ class ContestRank(models.Model): raise ValueError("Error submission type") # 这道题以前提交过 - if submission.problem_id in self.problem_info: + if submission.problem_id in self.submission_info: info = self.submission_info[submission.problem_id] # 如果这道题目已经 ac 了就跳过 if info["is_ac"]: From aa0f27fc47b6c46c6c13c89f362d79cff98fc819 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 18:19:48 +0800 Subject: [PATCH 20/22] =?UTF-8?q?JSONField=20=E4=BC=9A=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8A=8A=E5=AD=97=E5=85=B8=E7=9A=84=20key=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E6=95=B0=E5=AD=97=EF=BC=8C=E5=88=A4=E6=96=AD=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=87=8D=E5=A4=8D=E5=88=9B?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contest/models.py b/contest/models.py index 7e672041..e9e95564 100644 --- a/contest/models.py +++ b/contest/models.py @@ -125,8 +125,8 @@ class ContestRank(models.Model): raise ValueError("Error submission type") # 这道题以前提交过 - if submission.problem_id in self.submission_info: - info = self.submission_info[submission.problem_id] + if str(submission.problem_id) in self.submission_info: + info = self.submission_info[str(submission.problem_id)] # 如果这道题目已经 ac 了就跳过 if info["is_ac"]: return @@ -167,5 +167,5 @@ class ContestRank(models.Model): else: info["is_ac"] = False info["error_number"] = 1 - self.submission_info[submission.problem_id] = info + self.submission_info[str(submission.problem_id)] = info self.save() From 8cd2bdbaf0b1f4fec0d28925caf0fc733bc08a66 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 24 Sep 2015 21:25:13 +0800 Subject: [PATCH 21/22] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=AF=94=E8=B5=9B=E6=8E=92=E5=90=8D=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/models.py | 2 +- contest/views.py | 81 +++-------------------- template/src/oj/contest/contest_rank.html | 23 +++---- utils/models.py | 6 +- utils/shortcuts.py | 2 +- utils/templatetags/contest.py | 41 ++++++++++++ 6 files changed, 66 insertions(+), 89 deletions(-) diff --git a/contest/models.py b/contest/models.py index e9e95564..1e4f45aa 100644 --- a/contest/models.py +++ b/contest/models.py @@ -141,7 +141,7 @@ class ContestRank(models.Model): info["ac_time"] = (submission.create_time - self.contest.start_time).total_seconds() # 之前已经提交过,但是是错误的,这次提交是正确的。错误的题目不计入罚时 - self.total_time += (info["ac_time"] + info["error_time"] * 20 * 60) + 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 diff --git a/contest/views.py b/contest/views.py index ccf2c953..3d5ff512 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, @@ -379,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 = [] - - return render(request, "oj/contest/contest_rank.html", - {"contest": contest, "contest_problems": contest_problems, - "result": result, + rank = ContestRank.objects.filter(contest_id=contest_id).order_by("-total_ac_number", "total_time") + return render(request, "oj/contest/contest_rank_new.html", + {"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): @@ -468,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/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 %}