From 0bf84d1c4047ecfd74c526ffd5777d256a3e4b6a Mon Sep 17 00:00:00 2001 From: hohoTT <609029365@qq.com> Date: Sat, 22 Aug 2015 16:08:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=89=8D=E5=8F=B0?= =?UTF-8?q?=E6=AF=94=E8=B5=9B=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 49 +++++++++++- oj/urls.py | 3 + template/oj/contest/contest_list.html | 106 ++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 template/oj/contest/contest_list.html diff --git a/contest/views.py b/contest/views.py index d00bee5..0f0cf70 100644 --- a/contest/views.py +++ b/contest/views.py @@ -1,15 +1,18 @@ # coding=utf-8 import json +import datetime from django.shortcuts import render from django.db import IntegrityError from django.utils import dateparse from django.db.models import Q +from django.core.paginator import Paginator from rest_framework.views import APIView from utils.shortcuts import (serializer_invalid_response, error_response, success_response, paginate, rand_str, error_page) from account.models import REGULAR_USER, ADMIN, SUPER_ADMIN from group.models import Group +from announcement.models import Announcement from .models import Contest, ContestProblem from .serializers import (CreateContestSerializer, ContestSerializer, EditContestSerializer, @@ -17,7 +20,11 @@ from .serializers import (CreateContestSerializer, ContestSerializer, EditContes def contest_page(request, contest_id): - pass + try: + contest = Contest.objects.get(id=contest_id, visible=True) + except Contest.DoesNotExist: + return error_page(request, u"比赛不存在") + return render(request, "oj/contest/problems.html", {"contest": contest}) class ContestAdminAPIView(APIView): @@ -220,4 +227,42 @@ class ContestProblemAdminAPIView(APIView): contest_problem = contest_problem.filter(Q(title__contains=keyword) | Q(description__contains=keyword)) - return paginate(request, contest_problem, ContestProblemSerializer) \ No newline at end of file + return paginate(request, contest_problem, ContestProblemSerializer) + + +def contest_list_page(request, page=1): + # 正常情况 + contests = Contest.objects.filter(visible=True) + + # 搜索的情况 + keyword = request.GET.get("keyword", None) + if keyword: + contests = contests.filter(title__contains=keyword) + + paginator = Paginator(contests, 20) + try: + current_page = paginator.page(int(page)) + except Exception: + return error_page(request, u"不存在的页码") + + previous_page = next_page = None + + try: + previous_page = current_page.previous_page_number() + except Exception: + pass + + try: + next_page = current_page.next_page_number() + except Exception: + pass + + # 右侧的公告列表 + announcements = Announcement.objects.filter(is_global=True, visible=True).order_by("-create_time") + # 系统当前时间 + now = datetime.datetime.now() + return render(request, "oj/contest/contest_list.html", + {"problems": current_page, "page": int(page), + "previous_page": previous_page, "next_page": next_page, + "keyword": keyword, "announcements": announcements, + "now": now}) diff --git a/oj/urls.py b/oj/urls.py index 6dc7606..106b966 100644 --- a/oj/urls.py +++ b/oj/urls.py @@ -40,12 +40,15 @@ urlpatterns = [ url(r'^api/admin/contest/$', ContestAdminAPIView.as_view(), name="contest_admin_api"), url(r'^api/admin/user/$', UserAdminAPIView.as_view(), name="user_admin_api"), url(r'^problem/(?P\d+)/$', "problem.views.problem_page", name="problem_page"), + url(r'^contest/(?P\d+)/$', "contest.views.contest_page", name="contest_page"), url(r'^announcement/(?P\d+)/$', "announcement.views.announcement_page", name="announcement_page"), url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"), name="add_contest_page"), url(r'^problems/$', "problem.views.problem_list_page", name="problem_list_page"), url(r'^problems/(?P\d+)/$', "problem.views.problem_list_page", name="problem_list_page"), + url(r'^contests/$', "contest.views.contest_list_page", name="contest_list_page"), + url(r'^contests/(?P\d+)/$', "contest.views.contest_list_page", name="contest_list_page"), url(r'^admin/template/(?P\w+)/(?P\w+).html$', AdminTemplateView.as_view(), name="admin_template"), url(r'^api/admin/group/$', GroupAdminAPIView.as_view(), name="group_admin_api"), diff --git a/template/oj/contest/contest_list.html b/template/oj/contest/contest_list.html new file mode 100644 index 0000000..3233a6e --- /dev/null +++ b/template/oj/contest/contest_list.html @@ -0,0 +1,106 @@ +{% extends "oj_base.html" %} +{% block body %} + {% load problem %} +
+
+
+
+
+
+
+ + +
+
+
+
+
+ + + + + + + + + + + + + + {% for item in contests %} + + + + + + {% ifequal item.mode 0 %} + + {% endifequal %} + {% ifequal item.mode 1 %} + + {% endifequal %} + {% ifequal item.mode 2 %} + + {% endifequal %} + + {% ifequal item.contest_type 0 %} + + {% endifequal %} + {% ifequal item.contest_type 1 %} + + {% endifequal %} + {% ifequal item.contest_type 2 %} + + {% endifequal %} + + {% if now < item_start_time %} + + {% elif item.start_time <= now and now <= item_end_time %} + + {% else %} + + {% endif %} + + {% endfor %} + +
#比赛名称开始时间比赛模式比赛类型状态
{{ item.id }}{{ item.title }}{{ item.start_time }}acm模式AC数量模式AC总分排名模式小组赛公开赛公开赛(密码保护)比赛还未开始比赛正在进行比赛已结束
+ +
+
+ +
+
+
+

+ + 公告 +

+
+ {% for item in announcements %} + {{ forloop.counter }}.  {{ item.title }} +
+ {% endfor %} +
+
+
+
+
+{% endblock %} + +{% block js_block %} + +{% endblock %} \ No newline at end of file From 48d48a0f3040115b75c9fc3d9f0126c7d25b2896 Mon Sep 17 00:00:00 2001 From: hohoTT <609029365@qq.com> Date: Sat, 22 Aug 2015 20:42:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=AF=94?= =?UTF-8?q?=E8=B5=9B=E5=88=97=E8=A1=A8=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 12 +++-- .../oj/announcement/_announcement_panel.html | 12 +++++ template/oj/contest/contest_list.html | 51 +++++-------------- template/oj/problem/problem_list.html | 13 +---- utils/templatetags/contest.py | 29 +++++++++++ 5 files changed, 64 insertions(+), 53 deletions(-) create mode 100644 template/oj/announcement/_announcement_panel.html create mode 100644 utils/templatetags/contest.py diff --git a/contest/views.py b/contest/views.py index 0f0cf70..cabfff9 100644 --- a/contest/views.py +++ b/contest/views.py @@ -1,6 +1,7 @@ # coding=utf-8 import json import datetime +from django.utils.timezone import localtime from django.shortcuts import render from django.db import IntegrityError from django.utils import dateparse @@ -209,7 +210,7 @@ class ContestProblemAdminAPIView(APIView): """ 比赛题目分页json api接口 --- - response_serializer: ProblemSerializer + response_serializer: ContestProblemSerializer """ contest_problem_id = request.GET.get("contest_problem_id", None) if contest_problem_id: @@ -239,6 +240,11 @@ def contest_list_page(request, page=1): if keyword: contests = contests.filter(title__contains=keyword) + # 筛选我能参加的比赛 + join = request.GET.get("join", None) + if join: + contests = Contest.objects.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())) + paginator = Paginator(contests, 20) try: current_page = paginator.page(int(page)) @@ -262,7 +268,7 @@ def contest_list_page(request, page=1): # 系统当前时间 now = datetime.datetime.now() return render(request, "oj/contest/contest_list.html", - {"problems": current_page, "page": int(page), + {"contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword, "announcements": announcements, - "now": now}) + "join": join, "now": now}) diff --git a/template/oj/announcement/_announcement_panel.html b/template/oj/announcement/_announcement_panel.html new file mode 100644 index 0000000..f17a9ce --- /dev/null +++ b/template/oj/announcement/_announcement_panel.html @@ -0,0 +1,12 @@ +
+
+

+ + 公告 +

+
+ {% for item in announcements %} +

{{ forloop.counter }}.  {{ item.title }}

+ {% endfor %} +
+
\ No newline at end of file diff --git a/template/oj/contest/contest_list.html b/template/oj/contest/contest_list.html index 3233a6e..1689cee 100644 --- a/template/oj/contest/contest_list.html +++ b/template/oj/contest/contest_list.html @@ -1,6 +1,6 @@ {% extends "oj_base.html" %} {% block body %} - {% load problem %} + {% load contest %}
@@ -18,11 +18,9 @@ - - @@ -30,19 +28,9 @@ {% for item in contests %} - - {% ifequal item.mode 0 %} - - {% endifequal %} - {% ifequal item.mode 1 %} - - {% endifequal %} - {% ifequal item.mode 2 %} - - {% endifequal %} {% ifequal item.contest_type 0 %} @@ -54,27 +42,26 @@ {% endifequal %} - {% if now < item_start_time %} - - {% elif item.start_time <= now and now <= item_end_time %} - - {% else %} - - {% endif %} + {% endfor %}
# 比赛名称 开始时间比赛模式 比赛类型 状态
{{ item.id }} {{ item.title }} {{ item.start_time }}acm模式AC数量模式AC总分排名模式小组赛公开赛(密码保护)比赛还未开始比赛正在进行比赛已结束{{ item|contest_status }}
+
+ +
-
-
-

- - 公告 -

-
- {% for item in announcements %} - {{ forloop.counter }}.  {{ item.title }} -
- {% endfor %} -
-
+ {% include "oj/announcement/_announcement_panel.html" %}
{% endblock %} + {% block js_block %} - {% endblock %} \ No newline at end of file diff --git a/template/oj/problem/problem_list.html b/template/oj/problem/problem_list.html index 073cfe7..21833a7 100644 --- a/template/oj/problem/problem_list.html +++ b/template/oj/problem/problem_list.html @@ -55,18 +55,7 @@
-
-
-

- - 公告 -

-
- {% for item in announcements %} - {{ forloop.counter }}.  {{ item.title }} - {% endfor %} -
-
+ {% include "oj/announcement/_announcement_panel.html" %}

diff --git a/utils/templatetags/contest.py b/utils/templatetags/contest.py new file mode 100644 index 0000000..818d7f6 --- /dev/null +++ b/utils/templatetags/contest.py @@ -0,0 +1,29 @@ +# coding=utf-8 +import datetime +from django.utils.timezone import localtime + + +def get_contest_status(contest): + now = datetime.datetime.now() + if localtime(contest.start_time).replace(tzinfo=None) > now: + return "没有开始" + if localtime(contest.end_time).replace(tzinfo=None) < now: + return "已经结束" + return "正在进行" + + +def get_contest_status_color(contest): + now = datetime.datetime.now() + if localtime(contest.start_time).replace(tzinfo=None) > now: + return "info" + if localtime(contest.end_time).replace(tzinfo=None) < now: + return "warning" + return "success" + + +from django import template + +register = template.Library() +register.filter("contest_status", get_contest_status) +register.filter("contest_status_color", get_contest_status_color) +