山東大學(xué)-VirtualJudge-總結(jié)6

最近的幾天時(shí)間內(nèi)根悼,主要是進(jìn)一步開發(fā)SDUVJcontest的其他功能中,并在最終完成了contest的基本的開發(fā)士败,并實(shí)現(xiàn)了基本的代碼提交界面控汉,接下來需要進(jìn)一步完善其功能并改善性能。

Contest的實(shí)現(xiàn)代碼####

views.py中完成了了 對(duì)contest前端請(qǐng)求進(jìn)行處理的函數(shù)

def contest(req):
    search = req.GET.get('search')
    if search:
        query = Contest.objects.filter(Q(name__icontains=search) | Q(uid__username__icontains=search))
    else:
        query = Contest.objects.all()
    pg = req.GET.get('pg')
    if not pg:
        pg = 1
    pg = int(pg)

    max_cnt = query.count()
    start = max(pg - PAGE_NUMBER_EVERY_PAGE, 1)
    end = min(pg + PAGE_NUMBER_EVERY_PAGE, max_cnt)

    lst = query[(pg - 1) * LIST_NUMBER_EVERY_PAGE:pg * LIST_NUMBER_EVERY_PAGE]

    return ren2res('contest.html', req, {'page': range(start, end + 1), 'list': lst})


@login_required
def contest_detail(req, cid):
    contest = Contest.objects.get(id=cid)
    # time = datetime.datetime.now(pytz.timezone(pytz.country_timezones('cn')[0]))
    # time = datetime.datetime.now()
    time = timezone.now()
    if time > contest.start_time:
        start = True
    else:
        start = False
    if contest.private:
        #print(problems)
        # print('contest.accounts')
        # print(contest.accounts.all())
        if req.user.info not in contest.accounts.all():
            return ren2res("contest/contest.html", req, {'contest': contest, 'err': "You do not have access to this contest."})
    if start:
        problems = contest.get_problem_list()
        length = len(problems)
        problems_status = [0 for i in range(length)]

        for i in range(length):
            problems[i].append(len(Status.objects.filter(user = req.user).filter(pro = problems[i][2]).filter(result = 'Accepted')))#changes
        return ren2res("contest/contest.html", req, {'contest': contest, 'problems': problems, 'problem': problems[0][2]})
    else:
        return ren2res("contest/contest.html", req, {'contest': contest, 'err': "Just wait."})


@login_required
def contest_get_problem(req, cid):
    if req.is_ajax():
        contest = Contest.objects.get(id=cid)
        pid = req.GET.get('pid')
        t = loader.get_template('contest/contest_problem.html')
        problem = Problem.objects.get(proid=pid)
        if contest.private:
            if req.user.info not in contest.accounts.all():
                problem = []
        content_html = t.render(Context({'problem': problem, 'user' : req.user}))
        return HttpResponse(content_html)


@login_required
def contest_status(req, cid):#has understood
    if req.is_ajax():
        contest = Contest.objects.get(id=cid)
        t = loader.get_template('contest/contest_status.html')
        status_list = Status.objects.filter(cid=cid).order_by('-time')#need change
        if contest.private:
            if req.user.info not in contest.accounts.all():
                status_list = []
        pg = req.GET.get('pg')
        if not pg:
            pg = 1
        pg = int(pg)

        max_cnt = status_list.count() // 20 + 1
        start = max(pg - PAGE_NUMBER_EVERY_PAGE, 1)
        end = min(pg + PAGE_NUMBER_EVERY_PAGE, max_cnt)

        lst = status_list[(pg - 1) * LIST_NUMBER_EVERY_PAGE:pg * LIST_NUMBER_EVERY_PAGE]

        content_html = t.render(Context({'status_list': lst, 'page': range(start, end + 1), 'contest_id': cid, 'user': req.user}))
        return HttpResponse(content_html)
    else:
        raise Http404


@login_required
def contest_submit(req, cid):
    contest = Contest.objects.get(id=cid)
    time = datetime.datetime.now(pytz.timezone(pytz.country_timezones('cn')[0]))
    if time > contest.start_time + contest.duration_time:
        finish = True
    else:
        finish = False

    if contest.private:
        if req.user.info not in contest.accounts.all():
            return HttpResponseRedirect("/contest/" + cid + "/")

    if req.method == 'GET':
        return ren2res("contest/contest_submit.html", req, {'contest': contest, 'problems': contest.get_problem_list()})
    elif req.method == 'POST':
        pid = req.POST.get('pid')
        #need change start
        sub = Status(pro=Problem.objects.get(proid=pid), user=req.user, lang=req.POST.get('lang'))
        if not finish:
            sub.cid = contest.id
        else:
            sub.cid = -1
        sub.save()
        if req.POST.get('code'):
            content_file = ContentFile(req.POST.get('code'))
        elif req.FILES:
            content_file = ContentFile(req.FILES['file'].read())
        else:
            return ren2res("contest/contest_submit.html", req,
                           {'contest': contest, 'problems': contest.get_problem_list(), 'err': 'No Submit!'})
        sub.source_code.save(name=str(sub.id), content=content_file)
        sub.save()
        #judger.Judger(sub)
        result=judge_delay.delay(sub)
    if not finish:
        return HttpResponseRedirect("/contest/" + cid + "/")
    else:
        return HttpResponseRedirect("/contest/"+cid+"/status?pid=" + pid)
        #need change end

def contest_time(req, cid):#don't need change
    if req.is_ajax():
        contest = Contest.objects.get(id = cid)
        startTime = contest.start_time.strftime('%Y-%m-%d %H:%M:%S UTC')

        days = contest.duration_time.days
        seconds = contest.duration_time.seconds

        durationTime = days * 3600 * 24 + seconds;

        timeData = {'start' : startTime,
                    'duration' : durationTime}

        print(timeData)
        return JsonResponse(timeData)

@login_required
def contest_rank(req, cid):
    if req.is_ajax():
        contest = Contest.objects.get(id = cid)
        if contest.private:
            if req.user.info not in contest.accounts.all():
                return JsonResponse("{}")
        rank_cache = contest.rank
        # print("rank_cache:")
        # print(rank_cache)
        status_list = Status.objects.filter(cid = cid).filter(id__gt = contest.last_submit_id).order_by("time")
        # print("status_list")
        # print(status_list)
        rank_dict = json.loads(rank_cache)
        # print("rank_dict")
        # print(rank_dict)
        statsinfo = {}
        pos = 0
        problem_list = contest.get_problem_list()
        length = len(problem_list)

        
        if contest.last_submit_id==0:
            rank_dict["statsinfo"] = [{} for i in range(length)]
            for item in problem_list:
                rank_dict["statsinfo"][pos] = {"probid" : chr(pos + 65) ,"acNum" : 0, "tryNum" : 0}
                statsinfo[item[2].title] = {"pos" : pos}
                pos += 1
        else:
            for item in problem_list:
                statsinfo[item[2].title] = {"pos" : pos}
                pos += 1

        for item in status_list:
            if item.uid.is_staff :
                continue
            name = item.uid.username
            contest.last_submit_id = max(contest.last_submit_id, item.id)
            if name not in rank_dict.keys():
                rank_dict[name] = {"name" : name, "solved":0, "penalty":0, "probs" : [{"failNum" : 0, "acNum" : 0, "acTime" : 0} for i in range(length)]}

            pos = statsinfo[item.pid.title]["pos"]

            if item.status == 3: #Waiting
                break

            if item.status == 0: #Accepted
                rank_dict["statsinfo"][pos]["acNum"] += 1
            rank_dict["statsinfo"][pos]["tryNum"] += 1

            if rank_dict[name]["probs"][pos]["acNum"] == 0:
                if item.status == 0:
                    rank_dict[name]["probs"][pos]["acNum"] += 1
                    rank_dict[name]["probs"][pos]["acTime"] = dateToInt(item.time - contest.start_time, 1)
                    rank_dict[name]["penalty"] += 20 * rank_dict[name]["probs"][pos]["failNum"] + dateToInt(item.time - contest.start_time, 0)
                    rank_dict[name]["solved"] += 1
                else:
                    rank_dict[name]["probs"][pos]["failNum"] += 1
        contest.rank = json.dumps(rank_dict)
        # print("contest.rank")
        # print(contest.rank)
        contest.save()
        return JsonResponse(rank_dict)

同時(shí)钟病,contest的界面如下,已經(jīng)可以實(shí)現(xiàn)在contest中顯示問題的詳細(xì)說明界面:

contest主界面
problem詳細(xì)說明

contest.submit的界面如下:

contest提交問題
代碼提交時(shí)問題的選擇
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末萧恕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子肠阱,更是在濱河造成了極大的恐慌票唆,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,348評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屹徘,死亡現(xiàn)場離奇詭異走趋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)噪伊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,122評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門簿煌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鉴吹,你說我怎么就攤上這事姨伟。” “怎么了豆励?”我有些...
    開封第一講書人閱讀 156,936評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵授滓,是天一觀的道長。 經(jīng)常有香客問我肆糕,道長般堆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,427評(píng)論 1 283
  • 正文 為了忘掉前任诚啃,我火速辦了婚禮淮摔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘始赎。我一直安慰自己和橙,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,467評(píng)論 6 385
  • 文/花漫 我一把揭開白布造垛。 她就那樣靜靜地躺著魔招,像睡著了一般。 火紅的嫁衣襯著肌膚如雪五辽。 梳的紋絲不亂的頭發(fā)上办斑,一...
    開封第一講書人閱讀 49,785評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼乡翅。 笑死鳞疲,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蠕蚜。 我是一名探鬼主播尚洽,決...
    沈念sama閱讀 38,931評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼靶累!你這毒婦竟也來了腺毫?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,696評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤挣柬,失蹤者是張志新(化名)和其女友劉穎潮酒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凛忿,經(jīng)...
    沈念sama閱讀 44,141評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡澈灼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,483評(píng)論 2 327
  • 正文 我和宋清朗相戀三年竞川,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了店溢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,625評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡委乌,死狀恐怖床牧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情遭贸,我是刑警寧澤戈咳,帶...
    沈念sama閱讀 34,291評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站壕吹,受9級(jí)特大地震影響著蛙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜耳贬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,892評(píng)論 3 312
  • 文/蒙蒙 一踏堡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咒劲,春花似錦顷蟆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蛔屹,卻和暖如春削樊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背兔毒。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工嫉父, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留沛硅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓绕辖,卻偏偏與公主長得像摇肌,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子仪际,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,492評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容