def addContest(request, ccId): try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') try: cc = CourseClass.getById(ccId) except: raise Err(request, 'no resource') try: Contest.canAddContest(cc, u) except: raise Err(request, 'no priv') recentProblem = Problem.problemListByAuthor(u) if request.method == 'POST': form = contestForm(request.POST) pIdList = request.POST.getlist('problem_id') #pIdList = Problem.problemList(u) pTitleList = request.POST.getlist('problem_title_custom') pCnt = len(pIdList) if form.is_valid(): for i in xrange(pCnt): p = Problem.getById(pIdList[i]) if not p.canViewProblem(u): raise Err(request, 'no problem priv') pInfos = [(pIdList[i], pTitleList[i], chr(65+i)) for i in xrange(pCnt)] cTitle = form.cleaned_data['title'] cDesc = form.cleaned_data['desc'] cStartDate = form.cleaned_data['start_date'] cStartTime = form.cleaned_data['start_time'] cLength = form.cleaned_data['length'] cBoardStop = form.cleaned_data['board_stop'] cType = form.cleaned_data['contest_type'] cBoardType = form.cleaned_data['board_type'] permitLang = reduce(add, [Const.LANG_MASK[lang] for lang in form.cleaned_data['lang_limit']]) c = Contest.addContest(u, cc, cTitle, pInfos, datetime.combine(cStartDate, cStartTime), cDesc, cLength, cBoardStop, cType, cBoardType, permitLang) return redirect('Contest:show_contest', c.cid) else: problemList = [{'pid': pIdList[x], 'title': pTitleList[x], 'origTitle':Problem.getById(pIdList[x]).prob_title} for x in xrange(pCnt)] return render(request, 'newtpl/contest/addContest.html', {'cc':cc, 'form': form, 'recent_problem': recentProblem, 'problem_list': problemList, 'tpl':{'has_priv': True, 'sp': True, }}) else: form = contestForm() return render(request, 'newtpl/contest/addContest.html', {'cc':cc, 'form': form, 'recent_problem': recentProblem, 'tpl':{'has_priv': True, 'sp': True, }}) except Exception as e: messages.info(request, unicode(e)) return render(request, Err.ERROR_PAGE)
def updateContest(request, cId): try: u = User.getSessionUser(request.session) if not u: #raise Exception(Const.NOT_LOGGED_IN) raise Err(request, err='not login') try: c = Contest.getById(cId) except: raise Err(request, 'no resource') cc = c.course_class try: c.canBeManaged(u) except: raise Err(request, err='no priv') started = c.isStarted(5) if started: recentProblem = None else: recentProblem = Problem.problemListByAuthor(u) problemList = [{'pid': cp.problem.pid, 'title': cp.problem_title, 'origTitle':cp.problem.prob_title} for cp in c.getContestProblem()] if request.method == 'POST': POST = request.POST.copy() if started: POST['start_date'] = c.start_time.date() POST['start_time'] = c.start_time.time() POST['started'] = started form = contestForm(POST) if started: form.fields['start_date'].widget.attrs['disabled'] = True form.fields['start_time'].widget.attrs['disabled'] = True if started: pIdList = [cp['pid'] for cp in problemList] pTitleList = [cp['title'] for cp in problemList] pass else: #pIdList = Problem.problemList(u) pIdList = request.POST.getlist('problem_id') pTitleList = request.POST.getlist('problem_title_custom') pCnt = len(pIdList) if form.is_valid(): for i in xrange(pCnt): p = Problem.getById(pIdList[i]) if not p.canViewProblem(u): raise Err(request, 'no problem priv') pInfos = [(pIdList[i], pTitleList[i], chr(65+i)) for i in xrange(pCnt)] cTitle = form.cleaned_data['title'] cDesc = form.cleaned_data['desc'] cStartDate = form.cleaned_data['start_date'] cStartTime = form.cleaned_data['start_time'] cLength = form.cleaned_data['length'] cBoardStop = form.cleaned_data['board_stop'] cType = form.cleaned_data['contest_type'] cBoardType = form.cleaned_data['board_type'] permitLang = reduce(add, [Const.LANG_MASK[lang] for lang in form.cleaned_data['lang_limit']]) c.updateContest(cTitle, pInfos, datetime.combine(cStartDate, cStartTime), cDesc, cLength, cBoardStop, cType, cBoardType, permitLang) return redirect('Contest:show_contest', cId) else: problemList = [{'pid': pIdList[x], 'title': pTitleList[x], 'origTitle':Problem.getById(pIdList[x]).prob_title} for x in xrange(pCnt)] return render(request, 'newtpl/contest/updateContest.html', {'c': c, 'cc':cc, 'form': form, 'started': started, 'hehe':pCnt, 'recent_problem':recentProblem, 'problem_list': problemList, 'tpl':{'has_priv': True, 'sp': True,}}) else: form = contestForm( initial={ 'title': c.contest_title, 'desc':c.contest_description, 'start_date': c.start_time.date(), 'start_time': c.start_time.time(), 'length': c.length, 'board_stop': c.board_stop, 'contest_type': c.contest_type, 'board_type': c.board_type, 'lang_limit': c.permittedLangs(), 'started': started, } ) if started: form.fields['start_date'].widget.attrs['disabled'] = True form.fields['start_time'].widget.attrs['disabled'] = True return render(request, 'newtpl/contest/updateContest.html', {'c':c, 'cc':cc, 'form': form, 'started': started, 'recent_problem':recentProblem, 'problem_list': problemList, 'tpl':{'has_priv': True, 'sp': True, 'nav_act':'contest',}}) #return render(request, Const.ERROR_PAGE, {'errmsg': unicode(e), }) except Exception as e: messages.info(request, unicode(e)) return render(request, Const.ERROR_PAGE, {'errmsg': unicode(e), })
def updateContest(request, cId): try: u = User.getSessionUser(request.session) if not u: #raise Exception(Const.NOT_LOGGED_IN) raise Err(request, err='not login') try: c = Contest.getById(cId) except: raise Err(request, 'no resource') cc = c.course_class try: c.canBeManaged(u) except: raise Err(request, err='no priv') started = c.isStarted(5) if started: recentProblem = None else: recentProblem = Problem.problemListByAuthor(u) problemList = [{ 'pid': cp.problem.pid, 'title': cp.problem_title, 'origTitle': cp.problem.prob_title } for cp in c.getContestProblem()] if request.method == 'POST': POST = request.POST.copy() if started: POST['start_date'] = c.start_time.date() POST['start_time'] = c.start_time.time() POST['started'] = started form = contestForm(POST) if started: form.fields['start_date'].widget.attrs['disabled'] = True form.fields['start_time'].widget.attrs['disabled'] = True if started: pIdList = [cp['pid'] for cp in problemList] pTitleList = [cp['title'] for cp in problemList] pass else: pIdList = request.POST.getlist('problem_id') pTitleList = request.POST.getlist('problem_title_custom') pCnt = len(pIdList) if form.is_valid(): for i in xrange(pCnt): p = Problem.getById(pIdList[i]) if not p.canManageProblem(u): raise Err(request, 'no problem priv') pInfos = [(pIdList[i], pTitleList[i], chr(65 + i)) for i in xrange(pCnt)] cTitle = form.cleaned_data['title'] cDesc = form.cleaned_data['desc'] cStartDate = form.cleaned_data['start_date'] cStartTime = form.cleaned_data['start_time'] cLength = form.cleaned_data['length'] cBoardStop = form.cleaned_data['board_stop'] cType = form.cleaned_data['contest_type'] cBoardType = form.cleaned_data['board_type'] permitLang = reduce(add, [ Const.LANG_MASK[lang] for lang in form.cleaned_data['lang_limit'] ]) c.updateContest(cTitle, pInfos, datetime.combine(cStartDate, cStartTime), cDesc, cLength, cBoardStop, cType, cBoardType, permitLang) return redirect('Contest:show_contest', cId) else: problemList = [{ 'pid': pIdList[x], 'title': pTitleList[x], 'origTitle': Problem.getById(pIdList[x]).prob_title } for x in xrange(pCnt)] return render( request, 'newtpl/contest/updateContest.html', { 'c': c, 'cc': cc, 'form': form, 'started': started, 'hehe': pCnt, 'recent_problem': recentProblem, 'problem_list': problemList, 'tpl': { 'has_priv': True, 'sp': True, } }) else: form = contestForm( initial={ 'title': c.contest_title, 'desc': c.contest_description, 'start_date': c.start_time.date(), 'start_time': c.start_time.time(), 'length': c.length, 'board_stop': c.board_stop, 'contest_type': c.contest_type, 'board_type': c.board_type, 'lang_limit': c.permittedLangs(), 'started': started, }) if started: form.fields['start_date'].widget.attrs['disabled'] = True form.fields['start_time'].widget.attrs['disabled'] = True return render( request, 'newtpl/contest/updateContest.html', { 'c': c, 'cc': cc, 'form': form, 'started': started, 'recent_problem': recentProblem, 'problem_list': problemList, 'tpl': { 'has_priv': True, 'sp': True, 'nav_act': 'contest', } }) #return render(request, Const.ERROR_PAGE, {'errmsg': unicode(e), }) except Exception as e: return render(request, Const.ERROR_PAGE, { 'errmsg': unicode(e), })
def addContest(request, ccId): try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') try: cc = CourseClass.getById(ccId) except: raise Err(request, 'no resource') try: Contest.canAddContest(cc, u) except: raise Err(request, 'no priv') recentProblem = Problem.problemListByAuthor(u) if request.method == 'POST': form = contestForm(request.POST) pIdList = request.POST.getlist('problem_id') pTitleList = request.POST.getlist('problem_title_custom') pCnt = len(pIdList) if form.is_valid(): for i in xrange(pCnt): p = Problem.getById(pIdList[i]) if not p.canManageProblem(u): raise Err(request, 'no problem priv') pInfos = [(pIdList[i], pTitleList[i], chr(65 + i)) for i in xrange(pCnt)] cTitle = form.cleaned_data['title'] cDesc = form.cleaned_data['desc'] cStartDate = form.cleaned_data['start_date'] cStartTime = form.cleaned_data['start_time'] cLength = form.cleaned_data['length'] cBoardStop = form.cleaned_data['board_stop'] cType = form.cleaned_data['contest_type'] cBoardType = form.cleaned_data['board_type'] permitLang = reduce(add, [ Const.LANG_MASK[lang] for lang in form.cleaned_data['lang_limit'] ]) c = Contest.addContest( u, cc, cTitle, pInfos, datetime.combine(cStartDate, cStartTime), cDesc, cLength, cBoardStop, cType, cBoardType, permitLang) return redirect('Contest:show_contest', c.cid) else: problemList = [{ 'pid': pIdList[x], 'title': pTitleList[x], 'origTitle': Problem.getById(pIdList[x]).prob_title } for x in xrange(pCnt)] return render( request, 'newtpl/contest/addContest.html', { 'cc': cc, 'form': form, 'recent_problem': recentProblem, 'problem_list': problemList, 'tpl': { 'has_priv': True, 'sp': True, } }) else: form = contestForm() return render( request, 'newtpl/contest/addContest.html', { 'cc': cc, 'form': form, 'recent_problem': recentProblem, 'tpl': { 'has_priv': True, 'sp': True, } }) except Exception as e: return render(request, Err.ERROR_PAGE)