Esempio n. 1
0
def upload(request):
    # upload/make a submission
    if request.method == 'POST':
        sleep(1)
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            file = request.FILES['file']
            assID = request.POST['assID']
            user = request.user
            submission = None
            try:
                submission = _upload(user, assID, file)
                check_submission(submission)
                submission.retcode = 0
                submission.message = 'success'
                submission.save()
                return json.dumps({
                    'code': 0,
                    'message': submission.message,
                    'sid': str(submission.id),
                    })
            except UploadError as err:
                html = err.html()
                if submission:
                    submission.code = err.code
                    submission.message = html
                    submission.save()
                result = json.dumps({
                    'code': err.code,
                    'message': html,
                    })
                logger.debug('code:{}\nhtml:{}'.format(err.code, html.encode('utf8')))
                return result
        return json.dumps({
            'code': 1,
            'message': form.errors,
            })
    return json.dumps({
        'code': 1,
        'message': 'method is not POST',
        })
Esempio n. 2
0
def show_submission(request, submissionID):
    # show a submission, require GRADER permission
    # accept GET only
    if request.method != 'GET': raise Http404()

    user = request.user
    try:
        if user.usertype == 'TA':
            submission = Submission.objects.get(id=submissionID)
            submissions = Submission.objects.filter(
                    assignment=submission.assignment, grader=user, finished=0, retcode=0)
            for nextSubm in submissions:
                if nextSubm != submission:
                    break
            else:
                nextSubm = None
        elif user.usertype == 'student':
            submission = Submission.objects.get(id=submissionID, user=user)
            nextSubm = None
    except Submission.DoesNotExist:
        raise Http404()

    try:
        dec = submission.decompression
    except:
        # not decompressed 
        try:
            check_submission(submission)
        except UploadError as err:
            raise Http404()
        submission.retcode = 0
        submission.message = 'success'
        submission.save()
        dec = submission.decompression
    # dec: decompression
    comps = submission.compilation_set
    spec_ = submission.assignment.spec
    rows = []
    # rows = [(prob, contents), (prob, contents), ...]
    root = pjoin(dec.path, spec_.name)
    for prob in spec_.problems:
        # name, contents
        #   contents = [(type, content), (type, content), ... ]
        subroot = pjoin(root, prob.name)
        contents = []
        if prob.type == spec.Problem.CODE:
            for fname in os.listdir(subroot):
                fullpath = pjoin(subroot, fname)
                type = detect_type(fullpath)
                if type:
                    content = read_content(fullpath)
                    contents.append((type, fname, content))
            contents.sort(key=lambda (t, f, c): -len(c))
        elif prob.type == spec.Problem.TEXT:
            fullpath = subroot
            content = read_content(fullpath)
            contents.append(('.txt', prob.name, content))
        try:
            comID = comps.get(subpath=pjoin(spec_.name, prob.name), result='success').id
        except:
            comID = None
        rows.append((prob, submission.get_score(prob.name), comID, contents))

    return make_response(request, 'show_submission.html', {
        'rows': rows,
        'submission': submission,
        'ass': submission.assignment,
        'back': request.GET.get('back', settings.HOME_URL),
        'next': submission.assignment.url() if nextSubm is None else nextSubm.url(),
        })