Example #1
0
File: views.py Project: YLAsce/oj
def addProblemSubmit(request):
    logger.info(str(request).replace("\n","\t"))
    try:
        u = User.getSessionUser(request.session)
        if not u:
            raise Err(request, 'not login')
        p = request.POST
        if request.method == 'POST':
            form = addProblemForm(request.POST)
        if not form.is_valid():
            raise Exception(u'数据输入有误')
        course_id = int(p['course_id'])
        if course_id == None:
            raise Exception(u'课程非法')
        cs = Course.getById(course_id)
        if not cs:
            raise Exception(u'课程号非法')
        if not Problem.canAddCourseProblem(cs, u):
            raise Err(request, 'no priv')

        prb = Problem.addProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'],
                p['prob_memory'], p['prob_codelength'], p['prob_desc'], p['is_spj'],
                0, p['course_id'], "")

        data_count=0
        case_info=""

        #If file not exist, Create an empty file.
        open(Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+"0.in", 'a').close()
        open(Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+"0.out", 'a').close()

        if p['change_data'] == "1":
            dataInList=request.FILES.getlist('data_in')
            dataOutList=request.FILES.getlist('data_out')
            dataScrList=request.POST.getlist('data_scr')
            if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList):
                raise Exception(u'上传文件有误')
            for idx, nowData in enumerate(dataInList):
                path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".in"
                if default_storage.exists(path):
                    default_storage.delete(path)
                default_storage.save(path, nowData)
            for idx, nowData in enumerate(dataOutList):
                path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".out"
                if default_storage.exists(path):
                    default_storage.delete(path)
                default_storage.save(path, nowData)
            dict={}
            for idx, nowScr in enumerate(dataScrList):
                dict[str(idx)]=nowScr
            case_info=json.dumps(dict)

            data_count=len(dataInList)

        dict={}
        dict['desc']=p['prob_desc']
        dict['input_desc']=p['prob_input_desc']
        dict['output_desc']=p['prob_output_desc']
        dict['input_sample']=p['prob_input_sample']
        dict['output_sample']=p['prob_output_sample']
        prob_desc=json.dumps(dict)

        prb.updateProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'],
            p['prob_memory'], p['prob_codelength'], prob_desc, p['is_spj'],
            data_count, p['course_id'], case_info)

        return redirect("/problem/p/"+str(prb.pid)+"/")
    except Exception as e:
        logger.error(str(e).replace("\n","\t"))
        return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
Example #2
0
File: views.py Project: YLAsce/oj
def addProblemSubmit(request):
    logger.info(str(request).replace("\n", "\t"))
    try:
        u = User.getSessionUser(request.session)
        if not u:
            raise Err(request, 'not login')
        p = request.POST
        if request.method == 'POST':
            form = addProblemForm(request.POST)
        if not form.is_valid():
            raise Exception(u'数据输入有误')
        course_id = int(p['course_id'])
        if course_id == None:
            raise Exception(u'课程非法')
        cs = Course.getById(course_id)
        if not cs:
            raise Exception(u'课程号非法')
        if not Problem.canAddCourseProblem(cs, u):
            raise Err(request, 'no priv')

        prb = Problem.addProblem(u.uid, p['prob_priv'], p['prob_title'],
                                 p['prob_time'], p['prob_memory'],
                                 p['prob_codelength'], p['prob_desc'],
                                 p['is_spj'], 0, p['course_id'], "")

        data_count = 0
        case_info = ""

        #If file not exist, Create an empty file.
        open(Const.PROBLEM_DATA_PATH + str(prb.pid) + "/" + "0.in",
             'a').close()
        open(Const.PROBLEM_DATA_PATH + str(prb.pid) + "/" + "0.out",
             'a').close()

        if p['change_data'] == "1":
            dataInList = request.FILES.getlist('data_in')
            dataOutList = request.FILES.getlist('data_out')
            dataScrList = request.POST.getlist('data_scr')
            if len(dataInList) != len(dataOutList) or len(dataInList) != len(
                    dataScrList):
                raise Exception(u'上传文件有误')
            for idx, nowData in enumerate(dataInList):
                path = Const.PROBLEM_DATA_PATH + str(
                    prb.pid) + "/" + str(idx) + ".in"
                if default_storage.exists(path):
                    default_storage.delete(path)
                default_storage.save(path, nowData)
            for idx, nowData in enumerate(dataOutList):
                path = Const.PROBLEM_DATA_PATH + str(
                    prb.pid) + "/" + str(idx) + ".out"
                if default_storage.exists(path):
                    default_storage.delete(path)
                default_storage.save(path, nowData)
            dict = {}
            for idx, nowScr in enumerate(dataScrList):
                dict[str(idx)] = nowScr
            case_info = json.dumps(dict)

            data_count = len(dataInList)

        dict = {}
        dict['desc'] = p['prob_desc']
        dict['input_desc'] = p['prob_input_desc']
        dict['output_desc'] = p['prob_output_desc']
        dict['input_sample'] = p['prob_input_sample']
        dict['output_sample'] = p['prob_output_sample']
        prob_desc = json.dumps(dict)

        prb.updateProblem(u.uid, p['prob_priv'], p['prob_title'],
                          p['prob_time'], p['prob_memory'],
                          p['prob_codelength'], prob_desc, p['is_spj'],
                          data_count, p['course_id'], case_info)

        return redirect("/problem/p/" + str(prb.pid) + "/")
    except Exception as e:
        logger.error(str(e).replace("\n", "\t"))
        return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
Example #3
0
File: views.py Project: YLAsce/oj
def addProblem(request, course_id): # modified
    logger.info(str(request).replace("\n","\t"))
    try:
        u = User.getSessionUser(request.session)
        if not u:
            raise Err(request, 'not login')
        if course_id == None:
            return redirect('/problem/addProblem/')
        try:
            course_id = int(course_id)
            cs = Course.getById(course_id)
        except:
            raise Exception(u'课程号非法')

        if not Problem.canAddCourseProblem(cs, u):
            raise Err(request, 'no priv')

        if request.method == 'POST':
            form = addProblemForm(request.POST)
            if form.is_valid():
                prb = Problem.addProblem(u.uid,
                        form.cleaned_data['prob_priv'],
                        form.cleaned_data['prob_title'],
                        form.cleaned_data['prob_time'],
                        form.cleaned_data['prob_memory'],
                        form.cleaned_data['prob_codelength'],
                        form.cleaned_data['prob_desc'],
                        form.cleaned_data['is_spj'],
                        0, course_id, "")

                data_count=0
                case_info=""
                if form.cleaned_data['change_data'] == "1":
                    dataInList=request.FILES.getlist('data_in')
                    dataOutList=request.FILES.getlist('data_out')
                    dataScrList=request.POST.getlist('data_scr')
                    if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList):
                        raise Exception(u'上传文件有误')
                    for idx, nowData in enumerate(dataInList):
                        path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".in"
                        if default_storage.exists(path):
                            default_storage.delete(path)
                        default_storage.save(path, nowData)
                    for idx, nowData in enumerate(dataOutList):
                        path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".out"
                        if default_storage.exists(path):
                            default_storage.delete(path)
                        default_storage.save(path, nowData)
                    dict={}
                    for idx, nowScr in enumerate(dataScrList):
                        dict[str(idx)]=nowScr
                    case_info=json.dumps(dict)

                    data_count=len(dataInList)

                dict={}
                dict['desc']=form.cleaned_data['prob_desc']
                dict['input_desc']=form.cleaned_data['prob_input_desc']
                dict['output_desc']=form.cleaned_data['prob_output_desc']
                dict['input_sample']=form.cleaned_data['prob_input_sample']
                dict['output_sample']=form.cleaned_data['prob_output_sample']
                prob_desc=json.dumps(dict)

                prb.updateProblem(u.uid,
                        form.cleaned_data['prob_priv'],
                        form.cleaned_data['prob_title'],
                        form.cleaned_data['prob_time'],
                        form.cleaned_data['prob_memory'],
                        form.cleaned_data['prob_codelength'],
                        prob_desc,
                        form.cleaned_data['is_spj'],
                        data_count, course_id, case_info)

                return redirect("/problem/p/"+str(prb.pid)+"/")
            else:
                raise Err(request, 'problem info illegal')
        else:
            form = addProblemForm()
            return render(request,'newtpl/problem/modifyProblem.html',{'form': form,'course': cs, 'tpl':{'sp':True}})
    except Exception as e:
        logger.error(str(e).replace("\n","\t"))
        return render(request, Err.ERROR_PAGE)
Example #4
0
File: views.py Project: YLAsce/oj
def addProblem(request, course_id):  # modified
    logger.info(str(request).replace("\n", "\t"))
    try:
        u = User.getSessionUser(request.session)
        if not u:
            raise Err(request, 'not login')
        if course_id == None:
            return redirect('/problem/addProblem/')
        try:
            course_id = int(course_id)
            cs = Course.getById(course_id)
        except:
            raise Exception(u'课程号非法')

        if not Problem.canAddCourseProblem(cs, u):
            raise Err(request, 'no priv')

        if request.method == 'POST':
            form = addProblemForm(request.POST)
            if form.is_valid():
                prb = Problem.addProblem(u.uid, form.cleaned_data['prob_priv'],
                                         form.cleaned_data['prob_title'],
                                         form.cleaned_data['prob_time'],
                                         form.cleaned_data['prob_memory'],
                                         form.cleaned_data['prob_codelength'],
                                         form.cleaned_data['prob_desc'],
                                         form.cleaned_data['is_spj'], 0,
                                         course_id, "")

                data_count = 0
                case_info = ""
                if form.cleaned_data['change_data'] == "1":
                    dataInList = request.FILES.getlist('data_in')
                    dataOutList = request.FILES.getlist('data_out')
                    dataScrList = request.POST.getlist('data_scr')
                    if len(dataInList) != len(dataOutList) or len(
                            dataInList) != len(dataScrList):
                        raise Exception(u'上传文件有误')
                    for idx, nowData in enumerate(dataInList):
                        path = Const.PROBLEM_DATA_PATH + str(
                            prb.pid) + "/" + str(idx) + ".in"
                        if default_storage.exists(path):
                            default_storage.delete(path)
                        default_storage.save(path, nowData)
                    for idx, nowData in enumerate(dataOutList):
                        path = Const.PROBLEM_DATA_PATH + str(
                            prb.pid) + "/" + str(idx) + ".out"
                        if default_storage.exists(path):
                            default_storage.delete(path)
                        default_storage.save(path, nowData)
                    dict = {}
                    for idx, nowScr in enumerate(dataScrList):
                        dict[str(idx)] = nowScr
                    case_info = json.dumps(dict)

                    data_count = len(dataInList)

                dict = {}
                dict['desc'] = form.cleaned_data['prob_desc']
                dict['input_desc'] = form.cleaned_data['prob_input_desc']
                dict['output_desc'] = form.cleaned_data['prob_output_desc']
                dict['input_sample'] = form.cleaned_data['prob_input_sample']
                dict['output_sample'] = form.cleaned_data['prob_output_sample']
                prob_desc = json.dumps(dict)

                prb.updateProblem(u.uid, form.cleaned_data['prob_priv'],
                                  form.cleaned_data['prob_title'],
                                  form.cleaned_data['prob_time'],
                                  form.cleaned_data['prob_memory'],
                                  form.cleaned_data['prob_codelength'],
                                  prob_desc, form.cleaned_data['is_spj'],
                                  data_count, course_id, case_info)

                return redirect("/problem/p/" + str(prb.pid) + "/")
            else:
                raise Err(request, 'problem info illegal')
        else:
            form = addProblemForm()
            return render(request, 'newtpl/problem/modifyProblem.html', {
                'form': form,
                'course': cs,
                'tpl': {
                    'sp': True
                }
            })
    except Exception as e:
        logger.error(str(e).replace("\n", "\t"))
        return render(request, Err.ERROR_PAGE)