def my_submission(request, submission_id): """ 单个题目的提交详情页 """ try: result = _get_submission(submission_id, request.user) submission = result["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") try: if submission.contest_id: problem = ContestProblem.objects.get(id=submission.problem_id, visible=True) else: problem = Problem.objects.get(id=submission.problem_id, visible=True) except Exception: return error_page(request, u"提交不存在") if submission.info: try: info = json.loads(submission.info) except Exception: info = submission.info else: info = None user = User.objects.get(id=submission.user_id) return render(request, "oj/submission/my_submission.html", {"submission": submission, "problem": problem, "info": info, "user": user, "can_share": result["can_share"], "website_base_url": settings.WEBSITE_INFO["url"]})
def my_submission(request, submission_id): """ 单个题目的提交详情页 """ try: result = _get_submission(submission_id, request.user) submission = result["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") try: if submission.contest_id: problem = ContestProblem.objects.get(id=submission.problem_id, visible=True) else: problem = Problem.objects.get(id=submission.problem_id, visible=True) except Exception: return error_page(request, u"提交不存在") if submission.result in [judge_result["compile_error"], judge_result["system_error"], judge_result["waiting"]]: info = submission.info else: info = json.loads(submission.info) if "test_case" in info[0]: info = sorted(info, key=lambda x: x["test_case"]) user = User.objects.get(id=submission.user_id) return render(request, "oj/submission/my_submission.html", {"submission": submission, "problem": problem, "info": info, "user": user, "can_share": result["can_share"], "website_base_url": settings.WEBSITE_INFO["url"]})
def my_submission(request, submission_id): """ 单个题目的提交详情页 """ try: result = _get_submission(submission_id, request.user) submission = result["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") if submission.contest_id: try: problem = ContestProblem.objects.get(id=submission.problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"提交不存在") else: try: problem = Problem.objects.get(id=submission.problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"提交不存在") if submission.info: try: info = json.loads(submission.info) except Exception: 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, "user": user, "can_share": result["can_share"]})
def my_submission(request, submission_id): """ 单个题目的提交详情页 """ try: result = _get_submission(submission_id, request.user) submission = result["submission"] except Submission.DoesNotExist: return error_page(request, u"提交不存在") show_score = settings.SHOW_SCORE score=0.0 try: if submission.contest_id: problem = ContestProblem.objects.get(id=submission.problem_id, visible=True) else: problem = Problem.objects.get(id=submission.problem_id, visible=True) except Exception: return error_page(request, u"提交不存在") if submission.result in [judge_result["compile_error"], judge_result["system_error"], judge_result["waiting"]]: info = submission.info show_score=False else: score=submission.accepted_answer_info info = json.loads(submission.info) if "test_case" in info[0]: info = sorted(info, key=lambda x: x["test_case"]) user = User.objects.get(id=submission.user_id) return render(request, "oj/submission/my_submission.html", {"submission": submission, "problem": problem, "info": info,"score":score,"show_score":show_score, "user": user, "can_share": result["can_share"], "website_base_url": settings.WEBSITE_INFO["url"],"show_score": settings.SHOW_SCORE})
def _check_user_announcement_permission(*args, **kwargs): """ 这个函数检测当前用户能否查看这个公告 """ # CBV 的情况,第一个参数是self,第二个参数是request if len(args) == 2: request = args[-1] else: request = args[0] if "announcement_id" not in kwargs: return error_page(request, u"参数错误") announcement_id = kwargs["announcement_id"] try: announcement = Announcement.objects.get(id=announcement_id, visible=True) except Announcement.DoesNotExist: return error_page(request, u"公告不存在") # 如果公告是只有部分小组可见的 if not announcement.is_global: # 用户必须是登录状态的 if not request.user.is_authenticated(): return HttpResponseRedirect("/login/") if not announcement.groups.filter(id__in=request.user.group_set.all()).exists(): return error_page(request, u"公告不存在") return func(*args, **kwargs)
def problem_list_page(request, page=1): """ 前台的问题列表 """ # 正常情况 problems = Problem.objects.filter(visible=True) # 搜索的情况 keyword = request.GET.get("keyword", "").strip() if keyword: problems = problems.filter( Q(title__contains=keyword) | Q(description__contains=keyword)) difficulty_order = request.GET.get("order_by", None) if difficulty_order: if difficulty_order[0] == "-": problems = problems.order_by("-difficulty") difficulty_order = "difficulty" else: problems = problems.order_by("difficulty") difficulty_order = "-difficulty" else: difficulty_order = "difficulty" # 按照标签筛选 tag_text = request.GET.get("tag", None) if tag_text: try: tag = ProblemTag.objects.get(name=tag_text) except ProblemTag.DoesNotExist: return error_page(request, u"标签不存在") problems = tag.problem_set.all().filter(visible=True) try: page = int(page) except: return error_page(request, u"不存在的页码") current_page, previous_page, next_page, page_range, error, one_page = do_pages( problems, 40, page) if error: return error_page(request, u"不存在的页码") # 右侧标签列表 按照关联的题目的数量排序 排除题目数量为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, "tags": tags, "difficulty_order": difficulty_order, "page_range": page_range, "one_page": one_page })
def user_index_page(request, username): try: user = User.objects.get(username=username) except User.DoesNotExist: return error_page(request, u"用户不存在") blog_link = "" if user.userprofile.blog: blog_link = user.userprofile.blog.replace("http://", "").replace("https://", "") messages = Message.objects.filter(leave_to=user).order_by("-created_time") page = request.GET.get('page', 1) try: page = int(page) except: return error_page(request, u"不存在的页码") current_page, previous_page, next_page, page_range, error, one_page = do_pages(messages, 5, page) if error: return error_page(request, u"不存在的页码") return render(request, "oj/account/user_index.html", {"user": user, "blog_link": blog_link, "messages": current_page, "previous_page": previous_page, "next_page": next_page, "page_range": page_range, "one_page": one_page})
def discussion_page(request, problem_id): """ 题目讨论区 """ try: problem = Problem.objects.get(id=problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"题目不存在") problem_discussion_list = ProblemDiscussion.objects.filter( problem=problem).order_by("-created_time") page = request.GET.get('page', 1) try: page = int(page) except: return error_page(request, u"不存在的页码") current_page, previous_page, next_page, page_range, error, one_page = do_pages( problem_discussion_list, 10, page) if error: return error_page(request, u"不存在的页码") return render( request, "oj/problem/problem_discussion_list.html", { "problem_discussion_list": current_page, "problem": problem, "previous_page": previous_page, "next_page": next_page, "page_range": page_range, "one_page": one_page })
def problem_list_page(request, page=1): """ 前台的问题列表 """ # 正常情况 problems = Problem.objects.filter(visible=True) # 搜索的情况 keyword = request.GET.get("keyword", "").strip() if keyword: problems = problems.filter(Q(title__contains=keyword) | Q(description__contains=keyword)) difficulty_order = request.GET.get("order_by", None) if difficulty_order: if difficulty_order[0] == "-": problems = problems.order_by("-difficulty") difficulty_order = "difficulty" else: problems = problems.order_by("difficulty") difficulty_order = "-difficulty" else: difficulty_order = "difficulty" # 按照标签筛选 tag_text = request.GET.get("tag", None) if tag_text: try: tag = ProblemTag.objects.get(name=tag_text) except ProblemTag.DoesNotExist: return error_page(request, u"标签不存在") problems = tag.problem_set.all().filter(visible=True) paginator = Paginator(problems, 40) 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 # 右侧标签列表 按照关联的题目的数量排序 排除题目数量为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, "tags": tags, "difficulty_order": difficulty_order})
def reset_password_page(request, token): try: user = User.objects.get(reset_password_token=token) except User.DoesNotExist: return error_page(request, u"链接已失效") if (now() - user.reset_password_token_create_time).total_seconds() > 30 * 60: return error_page(request, u"链接已过期") return render(request, "oj/account/reset_password.html", {"user": user})
def contest_problem_submissions_list_page(request, contest_id, page=1): """ 单个比赛中的所有提交(包含自己和别人,自己可查提交结果,其他人不可查) """ try: contest = Contest.objects.get(id=contest_id) except Contest.DoesNotExist: return error_page(request, u"比赛不存在") # 以下是本场比赛中所有的提交 submissions = Submission.objects.filter(contest_id=contest_id). \ values("id", "contest_id", "problem_id", "result", "create_time", "accepted_answer_time", "language", "user_id").order_by("-create_time") language = request.GET.get("language", None) filter = None if language: submissions = submissions.filter(language=int(language)) filter = {"name": "language", "content": language} result = request.GET.get("result", None) if result: submissions = submissions.filter(result=int(result)) filter = {"name": "result", "content": result} paginator = Paginator(submissions, 20) # 为查询题目标题创建新字典 title = {} contest_problems = ContestProblem.objects.filter(contest=contest) for item in contest_problems: title[item.id] = item.title for item in submissions: item['title'] = title[item['problem_id']] 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 for item in current_page: # 自己提交的 管理员和创建比赛的可以看到所有的提交链接 if item["user_id"] == request.user.id or request.user.admin_type == SUPER_ADMIN or \ request.user == contest.created_by: item["show_link"] = True else: item["show_link"] = False return render(request, "oj/contest/submissions_list.html", {"submissions": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "contest": contest, "filter":filter})
def group_list_page(request, page=1): groups = Group.objects.filter(visible=True, join_group_setting__lte=2) # 搜索的情况 keyword = request.GET.get("keyword", None) if keyword: groups = groups.filter(Q(name__contains=keyword) | Q(description__contains=keyword)) paginator = Paginator(groups, 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 return render(request, "oj/group/group_list.html", { "groups": current_page.object_list, "contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword })
def application_page(request, request_id): try: application = JoinGroupRequest.objects.get(user=request.user, pk=request_id) except JoinGroupRequest.DoesNotExist: return error_page(request, u"申请不存在") return render(request, "oj/group/my_application.html", {"application": application})
def contest_problem_page(request, contest_id, contest_problem_id): """ 单个比赛题目的详情页 """ contest = Contest.objects.get(id=contest_id) try: contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛题目不存在") warning = u"您已经提交过本题的正确答案,重复提交可能造成时间累计。" show_warning = False try: submission = ContestSubmission.objects.get(user=request.user, contest=contest, problem=contest_problem) show_warning = submission.ac except ContestSubmission.DoesNotExist: pass # 已经结束 if contest.status == CONTEST_ENDED: show_warning = True warning = u"比赛已经结束" elif contest.status == CONTEST_NOT_START: show_warning = True warning = u"比赛没有开始,您是管理员,可以提交和测试题目,但是目前的提交不会计入排名。" show_submit_code_area = False if contest.status == CONTEST_UNDERWAY: show_submit_code_area = True if request.user.admin_type == SUPER_ADMIN or request.user == contest.created_by: show_submit_code_area = True return render(request, "oj/contest/contest_problem.html", {"contest_problem": contest_problem, "contest": contest, "samples": json.loads(contest_problem.samples), "show_warning": show_warning, "warning": warning, "show_submit_code_area": show_submit_code_area})
def user_rank_page(request, page=1): ranks = UserProfile.objects.filter(submission_number__gt=0).order_by( "-accepted_problem_number", "submission_number") paginator = Paginator(ranks, 20) try: ranks = paginator.page(int(page)) except Exception: return error_page(request, u"不存在的页码") previous_page = next_page = None try: previous_page = ranks.previous_page_number() except Exception: pass try: next_page = ranks.next_page_number() except Exception: pass return render( request, "utils/rank.html", { "ranks": ranks, "page": page, "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, })
def group_list_page(request, page=1): groups = Group.objects.filter(visible=True, join_group_setting__lte=2) # 搜索的情况 keyword = request.GET.get("keyword", None) if keyword: groups = groups.filter( Q(name__contains=keyword) | Q(description__contains=keyword)) paginator = Paginator(groups, 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 next_page = None try: next_page = current_page.next_page_number() except Exception: pass return render( request, "oj/group/group_list.html", { "groups": groups, "contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword })
def application_list_page(request, group_id): try: group = Group.objects.get(id=group_id, visible=True) except Group.DoesNotExist: return error_page(request, u"小组不存在") applications = JoinGroupRequest.objects.filter(user=request.user, group=group) return render(request, "oj/group/my_application_list.html", {"group": group, "applications": applications})
def user_rank_page(request, page=1): ranks = UserProfile.objects.filter(submission_number__gt=0).order_by("-accepted_problem_number", "submission_number") try: page = int(page) except: return error_page(request, u"不存在的页码") current_page, previous_page, next_page, page_range, error, one_page = do_pages(ranks, 20, page) if error: return error_page(request, u"不存在的页码") return render(request, "utils/rank.html", {"ranks": current_page, "page": page, "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "page_range": page_range, "one_page": one_page})
def get(self, request): callback = request.GET.get("callback", None) if not callback or callback != settings.SSO["callback"]: return error_page(request, u"参数错误") token = rand_str() request.user.auth_token = token request.user.save() return render(request, "oj/account/sso.html", {"redirect_url": callback + "?token=" + token, "callback": callback})
def contest_problem_my_submissions_list_page(request, contest_id, contest_problem_id): """ 我比赛单个题目的所有提交列表 """ try: Contest.objects.get(id=contest_id) except Contest.DoesNotExist: return error_page(request, u"比赛不存在") try: contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛问题不存在") submissions = Submission.objects.filter(user_id=request.user.id, problem_id=contest_problem.id).\ order_by("-create_time").\ values("id", "result", "create_time", "accepted_answer_time", "language") return render(request, "oj/submission/problem_my_submissions_list.html", {"submissions": submissions, "problem": contest_problem})
def contest_problem_my_submissions_list_page(request, contest_id, contest_problem_id): """ 我比赛单个题目的所有提交列表 """ try: Contest.objects.get(id=contest_id) except Contest.DoesNotExist: return error_page(request, u"比赛不存在") try: contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛问题不存在") submissions = Submission.objects.filter(user_id=request.user.id, problem_id=contest_problem.id). \ order_by("-create_time"). \ values("id", "result", "create_time", "accepted_answer_time", "language") return render(request, "oj/submission/problem_my_submissions_list.html", {"submissions": submissions, "problem": contest_problem})
def check(*args, **kwargs): request = args[-1] if request.user.is_authenticated() and request.user.admin_type: return func(*args, **kwargs) if request.is_ajax(): return error_response(u"需要管理员权限") else: return error_page(request, u"需要管理员权限,如果没有登录,请先登录")
def my_submission_list_page(request, page=1): """ 我的所有提交的列表页 """ # 显示所有人的提交 这是管理员的调试功能 show_all = request.GET.get("show_all", False) == "true" and request.user.admin_type == SUPER_ADMIN if show_all: submissions = Submission.objects.filter(contest_id__isnull=True) else: submissions = Submission.objects.filter(user_id=request.user.id, contest_id__isnull=True) submissions = submissions.values("id", "user_id", "problem_id", "result", "create_time", "accepted_answer_time", "language").order_by("-create_time") language = request.GET.get("language", None) filter = None if language: submissions = submissions.filter(language=int(language)) filter = {"name": "language", "content": language} result = request.GET.get("result", None) if result: submissions = submissions.filter(result=int(result)) filter = {"name": "result", "content": result} # 因为提交页面经常会有重复的题目和用户,缓存一下查询结果 cache_result = {"problem": {}, "user": {}} for item in submissions: problem_id = item["problem_id"] if problem_id not in cache_result["problem"]: problem = Problem.objects.get(id=problem_id) cache_result["problem"][problem_id] = problem.title item["title"] = cache_result["problem"][problem_id] if show_all: user_id = item["user_id"] if user_id not in cache_result["user"]: user = User.objects.get(id=user_id) cache_result["user"][user_id] = user item["user"] = cache_result["user"][user_id] paginator = Paginator(submissions, 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 return render(request, "oj/submission/my_submissions_list.html", {"submissions": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "filter": filter, "show_all": show_all})
def announcement_page(request, announcement_id): """ 公告的详情页面 """ try: announcement = Announcement.objects.get(id=announcement_id, visible=True) except Announcement.DoesNotExist: return error_page(request, u"公告不存在") return render(request, "oj/announcement/announcement.html", {"announcement": announcement})
def auth_page(request): if not request.user.is_authenticated(): return render(request, "oj/account/oauth.html") callback = request.GET.get("callback", None) if not callback: return error_page(request, u"参数错误") token = rand_str() request.user.auth_token = token return render(request, "oj/account/oauth.html", {"callback": callback, "token": token})
def problem_page(request, problem_id): """ 前台题目详情页 """ try: problem = Problem.objects.get(id=problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"题目不存在") return render(request, "oj/problem/problem.html", {"problem": problem, "samples": json.loads(problem.samples)})
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 = get_cache_redis() cache_key = str(contest_id) + "_rank_cache" rank = r.get(cache_key) if not rank: rank = ( ContestRank.objects.filter(contest_id=contest_id) .select_related("user") .order_by("-total_ac_number", "total_time") .values( "id", "user__id", "user__username", "user__real_name", "contest_id", "submission_info", "total_submission_number", "total_ac_number", "total_time", ) ) r.set(cache_key, json.dumps([dict(item) for item in rank])) else: rank = json.loads(rank) try: paging_rank = paginate_data(request, rank, None) if request.GET.get("paging", None): rank = paging_rank["results"] else: rank = paging_rank except Exception as e: return error_page(request, e.message) if request.GET.get("paging", None): paging_info = paging_rank paging_info["offset"] = paging_rank["page_size"] * (int(paging_rank["current_page"]) - 1) else: paging_info = {"previous_page": None, "next_page": None, "count": 0, "total_page": 0, "offset": 0} return render( request, "oj/contest/contest_rank.html", { "rank": rank, "contest": contest, "contest_problems": contest_problems, "paging_info": paging_info, "auto_refresh": request.GET.get("auto_refresh", None) == "true", "show_real_name": request.GET.get("show_real_name", None) == "true", }, )
def announcement_page(request, announcement_id): try: announcement = Announcement.objects.get(id=announcement_id, visible=True) except Announcement.DoesNotExist: return error_page(request, u"公告不存在") # 公开的公告 if announcement.is_global == 0: return render(request, "oj/announcement/announcement.html", {"announcement": announcement}) else: if not request.user.is_authenticated(): return error_page(request, u"公告不存在") # 判断是不是在组里面 if request.user.admin_type == SUPER_ADMIN or request.user == announcement.created_by: return render(request, "oj/announcement/announcement.html", {"announcement": announcement}) else: if request.user.groups.filter(id__in=[item.id for item in announcement.groups.all()]).exists(): return render(request, "oj/announcement/announcement.html", {"announcement": announcement}) else: return error_page(request, u"公告不存在")
def application_list_page(request, group_id): try: group = Group.objects.get(id=group_id, visible=True) except Group.DoesNotExist: return error_page(request, u"小组不存在") applications = JoinGroupRequest.objects.filter(user=request.user, group=group) return render(request, "oj/group/my_application_list.html", { "group": group, "applications": applications })
def check(*args, **kwargs): if len(args) == 2: request = args[-1] else: request = args[0] if request.user.is_authenticated() and request.user.admin_type == SUPER_ADMIN: return func(*args, **kwargs) if request.is_ajax(): return error_response(u"需要超级管理员权限") else: return error_page(request, u"需要超级管理员权限")
def group_page(request, group_id): try: group = Group.objects.get(id=group_id, visible=True) except Group.DoesNotExist: return error_page(request, u"小组不存在") joined = True try: UserGroupRelation.objects.get(user=request.user, group=group) except UserGroupRelation.DoesNotExist: joined = False return render(request, "oj/group/group.html", {"group": group, "joined": joined})
def contest_list_page(request, page=1): """ 所有比赛的列表页 """ # 正常情况 contests = Contest.objects.filter(visible=True).order_by("-create_time") # 搜索的情况 keyword = request.GET.get("keyword", "").strip() if keyword: contests = contests.filter( Q(title__contains=keyword) | Q(description__contains=keyword)) # 筛选我能参加的比赛 join = request.GET.get("join", None) if request.user.is_authenticated() and join: contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())). \ filter(end_time__gt=datetime.datetime.now(), start_time__lt=datetime.datetime.now()) try: page = int(page) except: return error_page(request, u"不存在的页码") current_page, previous_page, next_page, page_range, error, one_page = do_pages( contests, 10, page) if error: return error_page(request, u"不存在的页码") return render( request, "oj/contest/contest_list.html", { "contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword, "join": join, "page_range": page_range, "one_page": one_page, "start_id": page * 10 - 10 })
def user_index_page(request, username): try: user = User.objects.get(username=username) except User.DoesNotExist: return error_page(request, u"用户不存在") blog_link = "" if user.userprofile.blog: blog_link = user.userprofile.blog.replace("http://", "").replace("https://", "") return render(request, "oj/account/user_index.html", {"user": user, "blog_link": blog_link})
def contest_problem_page(request, contest_id, contest_problem_id): """ 单个比赛题目的详情页 """ contest = Contest.objects.get(id=contest_id) try: problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛题目不存在") warning = u"您已经提交过本题的正确答案,重复提交可能造成时间累计。" show_warning = False try: rank = ContestRank.objects.get(user=request.user, contest=contest) # 提示已经 ac 过这道题了 show_warning = rank.submission_info.get(str(problem.id), {"is_ac": False})["is_ac"] except ContestRank.DoesNotExist: pass # 已经结束 if contest.status == CONTEST_ENDED: show_warning = True warning = u"比赛已经结束" elif contest.status == CONTEST_NOT_START: show_warning = True warning = u"比赛没有开始,您是管理员,可以提交和测试题目,但是目前的提交不会计入排名。" show_submit_code_area = False if contest.status == CONTEST_UNDERWAY or \ contest.status == CONTEST_ENDED or \ request.user.admin_type == SUPER_ADMIN or \ request.user == contest.created_by: show_submit_code_area = True else: contest_set = Contest.objects.filter( groups__in=request.user.managed_groups.all()) if contest in contest_set: show_submit_code_area = True return render( request, "oj/problem/contest_problem.html", { "problem": problem, "contest": contest, "samples": json.loads(problem.samples), "show_warning": show_warning, "warning": warning, "show_submit_code_area": show_submit_code_area, "fuckingProblem": problem.input_description == u"None", "fuckingProblem2": problem.output_description == u"None", })
def contest_rank_page(request, contest_id): contest = Contest.objects.get(id=contest_id) contest_problems = ContestProblem.objects.filter( contest=contest, visible=True).order_by("sort_index") r = get_cache_redis() cache_key = str(contest_id) + "_rank_cache" rank = r.get(cache_key) if not rank: rank = ContestRank.objects.filter(contest_id=contest_id). \ select_related("user"). \ order_by("-total_ac_number", "total_time"). \ values("id", "user__id", "user__username", "user__real_name", "contest_id", "submission_info", "total_submission_number", "total_ac_number", "total_time") r.set(cache_key, json.dumps([dict(item) for item in rank])) else: rank = json.loads(rank) try: paging_rank = paginate_data(request, rank, None) if request.GET.get("paging", None): rank = paging_rank["results"] else: rank = paging_rank except Exception as e: return error_page(request, e.message) if request.GET.get("paging", None): paging_info = paging_rank paging_info["offset"] = paging_rank["page_size"] * ( int(paging_rank["current_page"]) - 1) else: paging_info = { "previous_page": None, "next_page": None, "count": 0, "total_page": 0, "offset": 0 } return render( request, "oj/contest/contest_rank.html", { "rank": rank, "contest": contest, "contest_problems": contest_problems, "paging_info": paging_info, "auto_refresh": request.GET.get("auto_refresh", None) == "true", "show_real_name": request.GET.get("show_real_name", None) == "true", })
def contest_list_page(request, page=1): """ 所有比赛的列表页 """ # 正常情况 contests = None if request.user.id == None or request.user.admin_type != SUPER_ADMIN: contests = Contest.objects.filter( visible=True).order_by("-create_time") else: contests = Contest.objects.all().order_by("-create_time") # 搜索的情况 keyword = request.GET.get("keyword", "").strip() if keyword: contests = contests.filter( Q(title__contains=keyword) | Q(description__contains=keyword)) # 筛选我能参加的比赛 join = request.GET.get("join", None) if request.user.is_authenticated() and join: contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())). \ filter(end_time__gt=datetime.datetime.now(), start_time__lt=datetime.datetime.now()) 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 return render( request, "oj/contest/contest_list.html", { "contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword, "join": join })
def group_page(request, group_id): try: group = Group.objects.get(id=group_id, visible=True) except Group.DoesNotExist: return error_page(request, u"小组不存在") joined = True try: UserGroupRelation.objects.get(user=request.user, group=group) except UserGroupRelation.DoesNotExist: joined = False return render(request, "oj/group/group.html", { "group": group, "joined": joined })
def contest_problem_page(request, contest_id, contest_problem_id): """ 单个比赛题目的详情页 """ contest = Contest.objects.get(id=contest_id) try: problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛题目不存在") warning = u"您已经提交过本题的正确答案,重复提交可能造成时间累计。" show_warning = False try: rank = ContestRank.objects.get(user=request.user, contest=contest) # 提示已经 ac 过这道题了 show_warning = rank.submission_info.get(str(problem.id), {"is_ac": False})["is_ac"] except ContestRank.DoesNotExist: pass # 已经结束 if contest.status == CONTEST_ENDED: show_warning = True warning = u"比赛已经结束" elif contest.status == CONTEST_NOT_START: show_warning = True warning = u"比赛没有开始,您是管理员,可以提交和测试题目,但是目前的提交不会计入排名。" show_submit_code_area = False if ( contest.status == CONTEST_UNDERWAY or request.user.admin_type == SUPER_ADMIN or request.user == contest.created_by ): show_submit_code_area = True else: contest_set = Contest.objects.filter(groups__in=request.user.managed_groups.all()) if contest in contest_set: show_submit_code_area = True return render( request, "oj/problem/contest_problem.html", { "problem": problem, "contest": contest, "samples": json.loads(problem.samples), "show_warning": show_warning, "warning": warning, "show_submit_code_area": show_submit_code_area, }, )
def problem_my_submissions_list_page(request, problem_id): """ 我单个题目所有提交的列表页 """ try: problem = Problem.objects.get(id=problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"问题不存在") submissions = Submission.objects.filter(user_id=request.user.id, problem_id=problem.id, contest_id__isnull=True). \ order_by("-create_time"). \ values("id", "result", "create_time", "accepted_answer_time", "language") return render(request, "oj/submission/problem_my_submissions_list.html", {"submissions": submissions, "problem": problem})
def contest_list_page(request, page=1): """ 所有比赛的列表页 """ # 正常情况 contests = Contest.objects.filter(visible=True).order_by("-create_time") # 搜索的情况 keyword = request.GET.get("keyword", None) if keyword: contests = contests.filter(Q(title__contains=keyword) | Q(description__contains=keyword)) # 筛选我能参加的比赛 join = request.GET.get("join", None) if request.user.is_authenticated and join: contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())).filter( end_time__gt=datetime.datetime.now(), start_time__lt=datetime.datetime.now() ) 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 return render( request, "oj/contest/contest_list.html", { "contests": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "keyword": keyword, "join": join, }, )
def post(self, request): serializer = SubmissionhareSerializer(data=request.data) if serializer.is_valid(): submission_id = serializer.data["submission_id"] try: result = _get_submission(submission_id, request.user) except Submission.DoesNotExist: return error_response(u"提交不存在") if not result["can_share"]: return error_page(request, u"提交不存在") submission = result["submission"] submission.shared = not submission.shared submission.save() return success_response(submission.shared) else: return serializer_invalid_response(serializer)
def my_submission_list_page(request, page=1): """ 我的所有提交的列表页 """ submissions = Submission.objects.filter(user_id=request.user.id, contest_id__isnull=True). \ values("id", "problem_id", "result", "create_time", "accepted_answer_time", "language").order_by("-create_time") language = request.GET.get("language", None) filter = None if language: submissions = submissions.filter(language=int(language)) filter = {"name": "language", "content": language} result = request.GET.get("result", None) if result: submissions = submissions.filter(result=int(result)) filter = {"name": "result", "content": result} # 为 submission 查询题目 因为提交页面经常会有重复的题目,缓存一下查询结果 cache_result = {} for item in submissions: problem_id = item["problem_id"] if problem_id not in cache_result: problem = Problem.objects.get(id=problem_id) cache_result[problem_id] = problem.title item["title"] = cache_result[problem_id] paginator = Paginator(submissions, 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") return render(request, "oj/submission/my_submissions_list.html", {"submissions": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "announcements": announcements, "filter": filter})
def get(sell, request): """ openapi 获取题目内容 """ problem_id = request.GET.get("problem_id", None) appkey = request.GET.get("appkey", None) if not (problem_id and appkey): return error_response(u"参数错误") try: User.objects.get(openapi_appkey=appkey) except User.DoesNotExist: return error_response(u"appkey无效") try: problem = Problem.objects.get(id=problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"题目不存在") return success_response(OpenAPIProblemSerializer(problem).data)
def __call__(self, *args, **kwargs): if len(args) == 2: self.request = args[1] else: self.request = args[0] if self.check_permission(): if self.request.user.is_forbidden is True: if self.request.is_ajax(): return error_response(u"您已被禁用,请联系管理员") else: return error_page(self.request, u"您已被禁用,请联系管理员") return self.func(*args, **kwargs) else: if self.request.is_ajax(): return error_response(u"请先登录") else: return HttpResponseRedirect("/login/?__from=" + urllib.quote(self.request.path))
def user_rank_page(request, page=1): ranks = UserProfile.objects.filter(submission_number__gt=0).order_by("-accepted_problem_number", "-submission_number") paginator = Paginator(ranks, 20) try: ranks = paginator.page(int(page)) except Exception: return error_page(request, u"不存在的页码") previous_page = next_page = None try: previous_page = ranks.previous_page_number() except Exception: pass try: next_page = ranks.next_page_number() except Exception: pass return render(request, "utils/rank.html", {"ranks": ranks, "page": page, "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20,})
def problem_list_page(request, page=1): """ 前台的问题列表 """ # 正常情况 problems = Problem.objects.filter(visible=True) # 搜索的情况 keyword = request.GET.get("keyword", "").strip() if keyword: problems = problems.filter(Q(title__contains=keyword) | Q(description__contains=keyword)) difficulty_order = request.GET.get("order_by", None) if difficulty_order: if difficulty_order[0] == "-": problems = problems.order_by("-difficulty") difficulty_order = "difficulty" else: problems = problems.order_by("difficulty") difficulty_order = "-difficulty" else: difficulty_order = "difficulty" # 按照标签筛选 tag_text = request.GET.get("tag", None) if tag_text: try: tag = ProblemTag.objects.get(name=tag_text) except ProblemTag.DoesNotExist: return error_page(request, u"标签不存在") problems = tag.problem_set.all().filter(visible=True) paginator = Paginator(problems, 40) try: current_page = paginator.page(int(page)) except Exception: return error_page(request, u"不存在的页码") previous_page = next_page = None pageid_from = max(1, current_page.number - 5) pageid_to = min(current_page.number + 6, paginator.num_pages + 1) pagelist = range(pageid_from, pageid_to) try: previous_page = current_page.previous_page_number() except Exception: pass try: next_page = current_page.next_page_number() except Exception: pass # 右侧标签列表 按照关联的题目的数量排序 排除题目数量为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, "first_page":1, "last_page": paginator.num_pages, "keyword": keyword, "tag": tag_text, "tags": tags, "difficulty_order": difficulty_order, "pagelist": pagelist})
def contest_problem_submissions_list_page(request, contest_id, page=1): """ 单个比赛中的所有提交(包含自己和别人,自己可查提交结果,其他人不可查) """ try: contest = Contest.objects.get(id=contest_id) except Contest.DoesNotExist: return error_page(request, u"比赛不存在") submissions = Submission.objects.filter(contest_id=contest_id). \ values("id", "contest_id", "problem_id", "result", "create_time", "accepted_answer_time", "language", "user_id").order_by("-create_time") # 如果比赛已经开始,就不再显示之前测试题目的提交 if contest.status != CONTEST_NOT_START: submissions = submissions.filter(create_time__gte=contest.start_time) user_id = request.GET.get("user_id", None) if user_id: submissions = submissions.filter(user_id=request.GET.get("user_id")) problem_id = request.GET.get("problem_id", None) if problem_id: submissions = submissions.filter(problem_id=problem_id) # 封榜的时候只能看到自己的提交 if not contest.real_time_rank: if not (request.user.admin_type == SUPER_ADMIN or request.user == contest.created_by): submissions = submissions.filter(user_id=request.user.id) language = request.GET.get("language", None) filter = None if language: submissions = submissions.filter(language=int(language)) filter = {"name": "language", "content": language} result = request.GET.get("result", None) if result: submissions = submissions.filter(result=int(result)) filter = {"name": "result", "content": result} paginator = Paginator(submissions, 20) try: submissions = paginator.page(int(page)) except Exception: return error_page(request, u"不存在的页码") # 为查询题目标题创建新字典 title = {} contest_problems = ContestProblem.objects.filter(contest=contest) for item in contest_problems: title[item.id] = item.title for item in submissions: item['title'] = title[item['problem_id']] previous_page = next_page = None try: previous_page = submissions.previous_page_number() except Exception: pass try: next_page = submissions.next_page_number() except Exception: pass for item in submissions: # 自己提交的 管理员和创建比赛的可以看到所有的提交链接 if item["user_id"] == request.user.id or request.user.admin_type == SUPER_ADMIN or \ request.user == contest.created_by: item["show_link"] = True else: item["show_link"] = False return render( request, "oj/contest/submissions_list.html", { "submissions": submissions, "page": int(page), "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "contest": contest, "filter": filter, "user_id": user_id, "problem_id": problem_id })
def _check_user_contest_permission(*args, **kwargs): """ 这个函数检查当前的这个比赛对于 request 的用户来说能不能参加 需要比较:比赛的开始和结束时间、比赛是否有密码、比赛是不是限定指定小组参加 如果是有密码或者限定指定小组参加的话,即使比赛已经结束,那么也是可以看到所有的题目和结果的 否则不能看到这个比赛的题目结果排名等等 """ # CBV 的情况,第一个参数是self,第二个参数是request if len(args) == 2: request = args[-1] else: request = args[0] if not request.user.is_authenticated(): if request.is_ajax(): return error_response(u"请先登录") else: return HttpResponseRedirect( "/login/?__from=" + urllib.quote(request.build_absolute_uri())) # kwargs 就包含了 url 里面的参数 if "contest_id" in kwargs: contest_id = kwargs["contest_id"] elif "contest_id" in request.data: contest_id = request.data["contest_id"] elif "contest_id" in request.GET: contest_id = request.GET["contest_id"] else: if request.is_ajax(): return error_response(u"参数错误") else: return error_page(request, u"参数错误") try: contest = Contest.objects.get(id=contest_id) except Contest.DoesNotExist: if request.is_ajax(): return error_response(u"比赛不存在") else: return error_page(request, u"比赛不存在") if request.user.admin_type == SUPER_ADMIN or request.user == contest.created_by: return func(*args, **kwargs) if request.user.admin_type == ADMIN: contest_set = Contest.objects.filter( groups__in=request.user.managed_groups.all()) if contest in contest_set: return func(*args, **kwargs) # 管理员可见隐藏的比赛,已经先判断了身份 if not contest.visible: if request.is_ajax(): return error_response(u"比赛不存在") else: return error_page(request, u"比赛不存在") # 有密码的公开赛 if contest.contest_type == PASSWORD_PROTECTED_CONTEST: # 没有输入过密码 if contest.id not in request.session.get("contests", []): if request.is_ajax(): return error_response(u"请先输入密码") else: return render( request, "oj/contest/no_contest_permission.html", { "reason": "password_protect", "show_tab": False, "contest": contest }) # 指定小组参加的 if contest.contest_type == GROUP_CONTEST: if not contest.groups.filter( id__in=request.user.group_set.all()).exists(): if request.is_ajax(): return error_response(u"只有指定小组的可以参加这场比赛") else: return render( request, "oj/contest/no_contest_permission.html", { "reason": "group_limited", "show_tab": False, "contest": contest }) if contest.contest_type == PASSWORD_PROTECTED_GROUP_CONTEST: if not contest.groups.filter( id__in=request.user.group_set.all()).exists(): if contest.id not in request.session.get("contests", []): if request.is_ajax(): return error_response(u"请先输入密码") else: return render( request, "oj/contest/no_contest_permission.html", { "reason": "password_protect", "show_tab": False, "contest": contest }) # 比赛没有开始 if contest.status == CONTEST_NOT_START: if request.is_ajax(): return error_response(u"比赛还没有开始") else: return render( request, "oj/contest/no_contest_permission.html", { "reason": "contest_not_start", "show_tab": False, "contest": contest }) # 比赛已经结束了,只拦截 ajax 的答案提交 if contest.status == CONTEST_ENDED and request.path == reverse( "contest_submission_api") and request.is_ajax(): return error_response(u"比赛已经结束") return func(*args, **kwargs)
def submission_list_page(request, page=1): """ 所有提交的列表页 """ submission_filter = {"my": None, "user_id": None} # 是否显示所有人的提交 show_all = False # url中如果存在user_id参数,说明只显示这个人的提交,忽略其他参数 user_id = request.GET.get("user_id", None) if user_id and request.user.admin_type == SUPER_ADMIN: submission_filter["user_id"] = user_id submissions = Submission.objects.filter(user_id=user_id, contest_id__isnull=True) else: # 兼容部分版本,设置中没有这一项 try: show_all = settings.SHOW_ALL_SUBMISSIONS_LIST except Exception: pass # url中my=true可以只显示自己的 if request.GET.get("my", None) == "true": submission_filter["my"] = "true" show_all = False if show_all: submissions = Submission.objects.filter(contest_id__isnull=True) else: submissions = Submission.objects.filter(user_id=request.user.id, contest_id__isnull=True) submissions = submissions.values("id", "user_id", "problem_id", "result", "create_time", "accepted_answer_time", "language").order_by("-create_time") language = request.GET.get("language", None) if language: submissions = submissions.filter(language=int(language)) submission_filter["language"] = language result = request.GET.get("result", None) if result: submissions = submissions.filter(result=int(result)) submission_filter["result"] = result paginator = Paginator(submissions, 20) try: submissions = paginator.page(int(page)) except Exception: return error_page(request, u"不存在的页码") # 因为提交页面经常会有重复的题目和用户,缓存一下查询结果 cache_result = {"problem": {}, "user": {}} for item in submissions: problem_id = item["problem_id"] if problem_id not in cache_result["problem"]: problem = Problem.objects.get(id=problem_id) cache_result["problem"][problem_id] = problem.title item["title"] = cache_result["problem"][problem_id] user_id = item["user_id"] if user_id not in cache_result["user"]: user = User.objects.get(id=user_id) cache_result["user"][user_id] = user item["user"] = cache_result["user"][user_id] if item["user_id"] == request.user.id or request.user.admin_type == SUPER_ADMIN: item["show_link"] = True else: item["show_link"] = False previous_page = next_page = None try: previous_page = submissions.previous_page_number() except Exception: pass try: next_page = submissions.next_page_number() except Exception: pass return render(request, "oj/submission/submissions_list.html", {"submissions": submissions, "page": int(page), "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, "query": build_query_string(submission_filter), "submission_filter": submission_filter, "show_all": show_all})