Exemplo n.º 1
0
def OLDaddSubmission(request,
                     contest_id=None,
                     problem_index=None):  # problem_id=None,
    logger.info(str(request).replace("\n\r", "\t"))
    """
    @view: add submission to some contest
    """
    template_tags = {
        'show_cid': True,
        'show_problem_index': True
    }  # 'is_contest': False, 'show_pid': True,
    u = None

    try:
        u = User.getSessionUser(request.session)
        if not u:
            return render(
                request,
                'error.html',
                {'errmsg': Const.NOT_LOGGED_IN},
            )

        if request.method == 'POST':
            form = addSubmissionForm(request.POST)
            if form.is_valid():

                sub_name = str(datetime.now())
                for i in ['-', ':', '.', ' ']:
                    sub_name = sub_name.replace(i, '_')
                code_file_path = "{0}/{1}_{2}".format(
                    Const.SUBMISSION_TMP_PATH, u.uid, sub_name)
                code_length = 0
                if 'code_file' in request.FILES:
                    #return render( request, 'error.html', { 'errmsg': u'请选择要上传的文件!', 'user': u }, )
                    default_storage.save(code_file_path,
                                         request.FILES['code_file'])
                # code_file = request.FILES['code_file']
                # dest = open( code_file_path, 'wb+')
                # for chunk in code_file.chunks():
                #     code_length += len(chunk)
                #     dest.write(chunk.encode('utf')) # chinese encoding
                # dest.write(chunk)
                else:
                    if not form.cleaned_data['code']:
                        return render(
                            request,
                            'error.html',
                            {
                                'errmsg': u'请填写要提交的代码!',
                                'user': u
                            },
                        )
                    default_storage.save(
                        code_file_path, ContentFile(form.cleaned_data['code']))

                code_length = default_storage.size(code_file_path)
                #dest = open( code_file_path, 'wb+')
                #code_length = len(request.POST['code'])
                #for chunk in request.POST['code']:
                #    dest.write(chunk.encode('utf')) # chinese encoding
                #dest.close()
                # before we tran the very file object, we should turn it off first!!!!(bugs here! but solved)

                no_judge = False
                if not contest_id:
                    return render(
                        request,
                        'error.html',
                        {
                            'errmsg': u'该选择要提交题目的考试!',
                            'user': u
                        },
                    )
                else:
                    c = Contest.getById(contest_id)
                    if not c:
                        return render(
                            request,
                            'error.html',
                            {
                                'errmsg': u'编号为{0}的考试不存在!'.format(contest_id),
                                'user': u
                            },
                        )

                    p_index = ContestProblem.getByContestAndProblemIndex(
                        contest_id, form.cleaned_data['problem_index'])
                    if not p_index:
                        return render(
                            request,
                            'error.html',
                            {
                                'errmsg':
                                u'序号为{0}的题目不存在于考试{1}中!'.format(
                                    form.cleaned_data['problem_index'],
                                    contest_id),
                                'user':
                                u
                            },
                        )
                    no_judge = c.board_type == 2
                    p = p_index.problem

                if not Submission.canSubmitCode(u, p.pid, contest_id):
                    return render(
                        request,
                        'error.html',
                        {
                            'errmsg': u'您没有提交本题的权限!',
                            'user': u
                        },
                    )

                Submission.addSubmission(u, p_index, p.data_count,
                                         code_file_path,
                                         form.cleaned_data['language'],
                                         code_length, no_judge)

                return redirect('Submission:contest_status',
                                contest_id=contest_id)  # could it be okay?
            # the usage of url.name in urls.py
        # not POST method
        else:
            form = addSubmissionForm()
            if not contest_id:
                return render(
                    request,
                    'error.html',
                    {
                        'errmsg': u'该选择要提交题目的考试!',
                        'user': u
                    },
                )
            else:
                c = Contest.getById(contest_id)
                if not c:
                    return render(
                        request,
                        'error.html',
                        {
                            'errmsg': u'编号为{0}的考试不存在!'.format(contest_id),
                            'user': u
                        },
                    )

        if problem_index:
            form.fields['problem_index'].initial = problem_index
        else:
            form.fields['problem_index'].initial = ''

        return render(
            request,
            'Submission/Submit.html',
            {
                'form': form,
                'show_tags': template_tags,
                'user': u,
                'contest': c,
                'problem_index': problem_index
            },
        )  # 'test_info': form.cleaned_data['language']})

    except Exception as e:
        logger.error(str(e).replace("\n\r", "\t"))
        return render(
            request,
            'error.html',
            {
                'errmsg': unicode(e),
                'user': u
            },
        )
Exemplo n.º 2
0
Arquivo: views.py Projeto: YLAsce/oj
def OLDaddSubmission(request, contest_id=None, problem_index=None): # problem_id=None, 
    logger.info(str(request).replace("\n\r","\t"))
    """
    @view: add submission to some contest
    """
    template_tags = { 'show_cid': True, 'show_problem_index': True} # 'is_contest': False, 'show_pid': True, 
    u = None

    try:
        u = User.getSessionUser( request.session)
        if not u:
            return render( request, 'error.html', { 'errmsg': Const.NOT_LOGGED_IN}, )

        if request.method == 'POST':
            form = addSubmissionForm( request.POST)
            if form.is_valid():

                sub_name = str( datetime.now())
                for i in ['-',':','.',' ']:
                    sub_name = sub_name.replace(i,'_')
                code_file_path = "{0}/{1}_{2}".format(Const.SUBMISSION_TMP_PATH, u.uid, sub_name)
                code_length = 0
                if 'code_file' in request.FILES:
                        #return render( request, 'error.html', { 'errmsg': u'请选择要上传的文件!', 'user': u }, )
                    default_storage.save( code_file_path, request.FILES['code_file'])
                   # code_file = request.FILES['code_file']
                   # dest = open( code_file_path, 'wb+')
                   # for chunk in code_file.chunks():
                   #     code_length += len(chunk)
                   #     dest.write(chunk.encode('utf')) # chinese encoding
                        # dest.write(chunk)
                else:
                    if not form.cleaned_data['code']:
                        return render( request, 'error.html', { 'errmsg': u'请填写要提交的代码!', 'user': u }, )
                    default_storage.save( code_file_path, ContentFile( form.cleaned_data['code']))

                code_length = default_storage.size( code_file_path)
                    #dest = open( code_file_path, 'wb+')
                    #code_length = len(request.POST['code'])
                    #for chunk in request.POST['code']:
                    #    dest.write(chunk.encode('utf')) # chinese encoding
                    #dest.close()
                # before we tran the very file object, we should turn it off first!!!!(bugs here! but solved)

                if not contest_id:
                    return render( request, 'error.html', { 'errmsg': u'该选择要提交题目的考试!', 'user': u }, )
                else:
                    c = Contest.getById( contest_id )
                    if not c:
                        return render( request, 'error.html', { 'errmsg': u'编号为{0}的考试不存在!'.format( contest_id), 'user': u }, )

                    p_index = ContestProblem.getByContestAndProblemIndex( contest_id, form.cleaned_data['problem_index'])
                    if not p_index:
                        return render( request, 'error.html', { 'errmsg': u'序号为{0}的题目不存在于考试{1}中!'.format(form.cleaned_data['problem_index'], contest_id), 'user': u }, )
                p = p_index.problem

                if not Submission.canSubmitCode( u, p.pid, contest_id):
                    return render( request, 'error.html', { 'errmsg': u'您没有提交本题的权限!', 'user': u}, )

                Submission.addSubmission( u, p_index, p.data_count, code_file_path, form.cleaned_data['language'], code_length)

                return redirect('Submission:contest_status',contest_id=contest_id) # could it be okay?
            # the usage of url.name in urls.py
        # not POST method
        else:
            form = addSubmissionForm()
            if not contest_id:
                return render( request, 'error.html', { 'errmsg': u'该选择要提交题目的考试!', 'user': u }, )
            else:
                c = Contest.getById( contest_id )
                if not c:
                    return render( request, 'error.html', { 'errmsg': u'编号为{0}的考试不存在!'.format( contest_id), 'user': u }, )

        if problem_index:
            form.fields['problem_index'].initial = problem_index
        else:
            form.fields['problem_index'].initial = ''

        return render( request, 'Submission/Submit.html', { 'form': form, 'show_tags': template_tags, 'user': u, 'contest': c, 'problem_index': problem_index}, )# 'test_info': form.cleaned_data['language']})

    except Exception as e:
        logger.error(str(e).replace("\n\r","\t"))
        return render( request, 'error.html', { 'errmsg': unicode(e), 'user': u}, )
Exemplo n.º 3
0
def addSubmission(request, contest_id=None, problem_index=None):
    logger.info(str(request).replace("\n\r", "\t"))
    """
    view used to add submission
    """
    try:
        contest_id = int(contest_id)

        u = User.getSessionUser(request.session)
        if not u:
            raise Err(request, err='not login')

        if not contest_id:
            raise Err(
                request,
                err='request err',
                log_format=('contest id', 'UNDEFINED'),
                user_format=(u'考试编号', u'你吃了么!!!'),
            )

        elif not problem_index:
            raise Err(
                request,
                err='request err',
                log_format=('problem index', 'UNDEFINED'),
                user_format=(u'题目序号', u'哪里去了!!'),
            )
        else:
            try:
                c = Contest.getById(contest_id)
            except:
                raise Err(
                    request,
                    err='no contest',
                    log_format=('{0}'.format(cid), ''),
                    user_format=(u'{0}'.format(cid), u'别做坏事!'),
                )

            try:
                p_index = ContestProblem.getBy(c=c, idx=problem_index)
            except:
                raise Err(
                    request,
                    err='no contest problem',
                    log_format=('{0}'.format(problem_index), ''),
                    user_format=(u'{0}'.format(problem_index), u'别乱写好么!!'),
                )

            p = p_index.problem

            if not Submission.canSubmitCode(u, p_index):
                raise Err(
                    request,
                    err='submit err',
                    log_format=(
                        'no priv to submit',
                        'contest ended or no priv to attend the contest'),
                    user_format=(u'您没有提交该题的权限', u'考试已结束或者您没有参加本场考试的权限'))

        lang_list = []
        if c.lang_limit & 1 == 1:
            lang_list.append(('gcc', 'GNU C'))
        if (c.lang_limit >> 1) & 1 == 1:
            lang_list.append(('g++', 'GNU C++'))
        if (c.lang_limit >> 2) & 1 == 1:
            lang_list.append(('java', 'JAVA'))

        if request.method == 'POST':
            form = addSubmissionForm(lang_list, request.POST)

            if form.is_valid():
                sub_name = str(datetime.now())
                time_str = sub_name.split('.')
                time_str.pop()

                for i in ['-', ':', '.', ' ']:
                    sub_name = sub_name.replace(i, '_')

                import os
                code_file_path = os.path.join(
                    Const.SUBMISSION_TMP_PATH,
                    "{0:010d}_{1}".format(u.uid, sub_name))
                code_length = 0

                # head_details: details of the submission added at the head of the code file
                head_details = ''
                head_details += '/*\n'
                head_details += 'USER_ID: ' + str(u) + '\n'
                head_details += 'PROBLEM: ' + str(p.pid) + '\n'
                head_details += 'SUBMISSION_TIME: ' + time_str[0] + '\n'
                head_details += '*/\n'

                if 'code_file' in request.FILES:
                    default_storage.save(code_file_path,
                                         request.FILES['code_file'])
                else:
                    if form.cleaned_data['code']:
                        default_storage.save(
                            code_file_path,
                            ContentFile(head_details +
                                        form.cleaned_data['code']))
                    else:
                        raise Err(
                            request,
                            err='request err',
                            log_format=('code', 'no input'),
                            user_format=(u'代码呢!', u'不写代码交什么交!!'),
                        )

                code_length = default_storage.size(code_file_path)
                sub_lang = form.cleaned_data['language']

                if sub_lang not in map(lambda x: x[0], lang_list):
                    raise Err(
                        request,
                        err='illegal language',
                        log_format=('{0}'.format(sub_lang), 'blabla'),
                        user_format=(u'{0}'.format(sub_lang), u'别瞎搞成不!!'),
                    )

                x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
                if x_forwarded_for:
                    ip = x_forwarded_for.split(',')[0]
                else:
                    ip = request.META.get('REMOTE_ADDR')

                additional = {'submit_ip': ip}
                Submission.addSubmission(u, p_index, code_file_path, sub_lang,
                                         code_length, additional,
                                         c.board_type == 2)

                return redirect('Submission:contest_status',
                                contest_id=contest_id)  # could it be okay?
            else:
                raise Exception(u'form invalid')
            # the usage of url.name in urls.py
        # not POST method
        else:
            form = addSubmissionForm(lang_list)

        return render(
            request,
            'newtpl/submission/submit.html',
            {
                'form': form,
                'tpl': {
                    'sp': True
                },
                'contest': c,
                'cp': p_index
            },
        )  # 'test_info': form.cleaned_data['language']})

    except Exception as e:
        logger.error(str(e).replace("\n\r", "\t"))
        return render(
            request,
            Err.ERROR_PAGE,
            {
                'errmsg': unicode(e),
            },
        )
Exemplo n.º 4
0
Arquivo: views.py Projeto: YLAsce/oj
def addSubmission(request, contest_id=None, problem_index=None):
    logger.info(str(request).replace("\n\r","\t"))
    """
    view used to add submission
    """
    try:
        contest_id = int(contest_id)

        u = User.getSessionUser( request.session)
        if not u:
            raise Err( request, err='not login')

        if not contest_id:
            raise Err( request, err='request err', 
                    log_format=( 'contest id', 'UNDEFINED'), 
                    user_format=( u'考试编号', u'你吃了么!!!'),
                    )

        elif not problem_index:
            raise Err( request, err='request err', 
                    log_format=( 'problem index', 'UNDEFINED'), 
                    user_format=( u'题目序号', u'哪里去了!!'),
                    )
        else:
            try:
                c = Contest.getById( contest_id )
            except:
                raise Err( request, err='no contest', 
                        log_format=( '{0}'.format(cid), ''), 
                        user_format=( u'{0}'.format( cid), u'别做坏事!'),
                        )

            try:
                p_index = ContestProblem.getBy(c=c, idx=problem_index)
            except:
                raise Err( request, err='no contestproblem', 
                        log_format=( '{0}'.format( problem_index), ''), 
                        user_format=( u'{0}'.format( problem_index), u'别乱写好么!!'),
                        )

            p = p_index.problem
            
            if not Submission.canSubmitCode( u, p_index ):
                raise Err( request, err = 'submit err', 
                        log_format = ( 'no priv to submit', 'contest ended or no priv to attend the contest'),
                        user_format = ( u'您没有提交该题的权限', u'考试已结束或者您没有参加本场考试的权限')
                        )

        lang_list = []
        if c.lang_limit & 1 == 1:
            lang_list.append( ('gcc','GNU C'))
        if (c.lang_limit >> 1) & 1 == 1:
            lang_list.append( ('g++','GNU C++'))
        if (c.lang_limit >> 2) & 1 == 1:
            lang_list.append( ('java', 'JAVA'))
            
        if request.method == 'POST':
            form = addSubmissionForm(lang_list, request.POST)

            if form.is_valid():
                sub_name = str( datetime.now())
                time_str = sub_name.split('.')
                time_str.pop()

                for i in ['-',':','.',' ']:
                    sub_name = sub_name.replace(i,'_')

                import os
                code_file_path = os.path.join( Const.SUBMISSION_TMP_PATH, "{0:010d}_{1}".format( u.uid, sub_name))
                code_length = 0

                # head_details: details of the submission added at the head of the code file
                head_details = ''
                head_details += '/*\n'
                head_details += 'USER_ID: ' + str(u) + '\n'
                head_details += 'PROBLEM: ' + str(p.pid) + '\n'
                head_details += 'SUBMISSION_TIME: ' + time_str[0] + '\n'
                head_details += '*/\n'

                if 'code_file' in request.FILES:
                    default_storage.save( code_file_path, request.FILES['code_file'])
                else:
                    if form.cleaned_data['code']:
                        default_storage.save( code_file_path, ContentFile( head_details + form.cleaned_data['code']))
                    else:
                        raise Err( request, err='request err', 
                                log_format=( 'code', 'no input'), 
                                user_format=( u'代码呢!', u'不写代码交什么交!!'),
                                )

                code_length = default_storage.size( code_file_path)
                sub_lang = form.cleaned_data['language']

                if sub_lang not in map( lambda x: x[0], lang_list):
                    raise Err( request, err='illegal language', 
                            log_format=( '{0}'.format( sub_lang), 'blabla'), 
                            user_format=( u'{0}'.format( sub_lang), u'别瞎搞成不!!'),
                            )

                x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
                if x_forwarded_for:
                    ip = x_forwarded_for.split(',')[0]
                else:
                    ip = request.META.get('REMOTE_ADDR')

                additional = { 'submit_ip': ip}
                Submission.addSubmission( u, p_index, code_file_path, sub_lang, code_length, additional)

                return redirect('Submission:contest_status',contest_id=contest_id) # could it be okay?
            else:
                raise Exception(u'form invalid')
            # the usage of url.name in urls.py
        # not POST method
        else:
            form = addSubmissionForm(lang_list)

        return render( request, 'newtpl/submission/submit.html', { 'form': form, 'tpl': {'sp':True}, 'contest': c, 'cp': p_index}, )# 'test_info': form.cleaned_data['language']})

    except Exception as e:
        logger.error(str(e).replace("\n\r","\t"))
        return render( request, Err.ERROR_PAGE, { 'errmsg': unicode(e), }, )