Ejemplo n.º 1
0
def editProgram(request, programID):
    program = get_object_or_404(Program, pk=programID)
    is_moderator = isCourseModerator(program.assignment.course, request.user)
    if not is_moderator:
        return HttpResponseForbidden("Forbidden 403")

    if request.method == 'POST':
        # form is initialized by model then overwritten by request data and files.
        lang_category = language_category(program.assignment.program_language)
        if (lang_category == 0
            ):  #Compilation needed. Execution not needed. C and C++
            form = ProgramFormCNotE(request.POST,
                                    request.FILES,
                                    initial=model_to_dict(program))
        elif (lang_category == 1):  #Compilation and execution needed.
            form = ProgramFormCandE(request.POST,
                                    request.FILES,
                                    initial=model_to_dict(program))
        elif (lang_category == 2):  #Execution needed. Python and bash
            form = ProgramFormE(request.POST,
                                request.FILES,
                                initial=model_to_dict(program))
        form.assignment = program.assignment
        form.program_model = program
        if form.is_valid():
            # check if new file is uploaded
            if 'program_files' in form.changed_data:  # program_files are changed."
                if program.program_files:  # delete older file if any.
                    program.program_files.delete(save=False)
                if not form.cleaned_data[
                        'program_files']:  # if file is being cleared.
                    form.cleaned_data.pop('program_files')

            if 'makefile' in form.changed_data:
                if program.makefile:
                    program.makefile.delete(save=False)
                if not form.cleaned_data['makefile']:
                    form.cleaned_data.pop('makefile')

            for key in form.cleaned_data.keys():
                setattr(program, key, form.cleaned_data[key])

            program.delete_error_message()
            program.is_sane = True
            for afield in [
                    'program_files', 'compiler_command', 'makefile',
                    'execution_command'
            ]:
                if afield in form.changed_data:
                    program.compile_now = True
                    program.execute_now = True
                    break
            program.save()

            # Mark all assignment results to stale if either program_files or compiler_command or execution_command have changed
            changed_fields = ['program_files']
            if program.compiler_command:
                changed_fields.append('compiler_command')
            if program.execution_command:
                changed_fields.append('execution_command')
            if set(changed_fields) - set(form.changed_data):
                all_submissions = Upload.objects.filter(
                    assignment=program.assignment)
                AssignmentResults.objects.filter(
                    submission__in=all_submissions).update(is_stale=True)

            return HttpResponseRedirect(
                reverse('assignments_detailsprogram',
                        kwargs={'programID': programID}))
    else:
        lang_category = language_category(program.assignment.program_language)
        if (lang_category == 0
            ):  #Compilation needed. Execution not needed. C and C++
            form = ProgramFormCNotE(initial=model_to_dict(program))
        elif (lang_category == 1):  #Compilation and execution needed.
            form = ProgramFormCandE(initial=model_to_dict(program))
        elif (lang_category == 2):  #Execution needed. Python and bash
            form = ProgramFormE(initial=model_to_dict(program))

    return render_to_response('assignments/editProgram.html', {
        'form': form,
        'program': program
    },
                              context_instance=RequestContext(request))
Ejemplo n.º 2
0
def createProgram(request, assignmentID):
    assignment = get_object_or_404(Assignment, pk=assignmentID)
    course = assignment.course
    # Only creator of course can create new program in assignment.

    is_moderator = isCourseModerator(course, request.user)
    if not is_moderator:
        return HttpResponseForbidden("Forbidden 403")

    if request.method == 'POST':
        lang_category = language_category(assignment.program_language)
        if (lang_category == 0
            ):  #Compilation needed. Execution not needed. C and C++
            form = ProgramFormCNotE(request.POST, request.FILES)
        elif (lang_category == 1):  #Compilation and execution needed.
            form = ProgramFormCandE(request.POST, request.FILES)
        elif (lang_category == 2):  #Execution needed. Python and bash
            form = ProgramFormE(request.POST, request.FILES)
        form.assignment = assignment  # files submitted by student
        if form.is_valid():
            newProgram = Program(**form.cleaned_data)
            newProgram.assignment = assignment
            newProgram.is_sane = True
            newProgram.compile_now = True
            newProgram.execute_now = True
            newProgram.language = assignment.program_language
            newProgram.save()
            link = reverse('assignments_createtestcase',
                           kwargs={'programID': newProgram.id})
            messages.success(
                request,
                'Section Created! Now <a href="{0}">ADD</a> testcase for this program.'
                .format(link),
                extra_tags='safe')
            all_submissions = Upload.objects.filter(assignment=assignment)
            AssignmentResults.objects.filter(
                submission__in=all_submissions).update(is_stale=True)
            return HttpResponseRedirect(
                reverse('assignments_details',
                        kwargs={'assignmentID': assignmentID}))
    else:
        objs = Program.objects.filter(assignment=assignment)
        initial = {}
        lang_category = language_category(assignment.program_language)
        if objs:
            if lang_category == 0:
                comp_command = pickle.loads(objs[0].compiler_command)
                initial['compiler_command'] = pickle.dumps(
                    [comp_command[0], '', ''])
            elif lang_category == 1:
                comp_command = pickle.loads(objs[0].compiler_command)
                initial['compiler_command'] = pickle.dumps(
                    [comp_command[0], '', ''])
                exe_command = pickle.loads(objs[0].execution_command)
                initial['execution_command'] = pickle.dumps(
                    [exe_command[0], '', ''])
            elif lang_category == 2:
                exe_command = pickle.loads(objs[0].execution_command)
                initial['execution_command'] = pickle.dumps(
                    [exe_command[0], '', ''])
        else:
            if lang_category == 0:
                comp_command = get_compiler_name(assignment.program_language)
                initial['compiler_command'] = pickle.dumps(
                    [comp_command, '', ''])
            elif lang_category == 1:
                comp_command = get_compiler_name(assignment.program_language)
                initial['compiler_command'] = pickle.dumps(
                    [comp_command, '', ''])
                exe_command = get_interpreter_name(assignment.program_language)
                initial['execution_command'] = pickle.dumps(
                    [exe_command, '', ''])
            elif lang_category == 2:
                exe_command = get_interpreter_name(assignment.program_language)
                initial['execution_command'] = pickle.dumps(
                    [exe_command, '', ''])
        if lang_category == 0:  #Compilation needed. Execution not needed. C and C++
            form = ProgramFormCNotE(initial=initial)
        elif lang_category == 1:  #Compilation and execution needed.
            form = ProgramFormCandE(initial=initial)
        elif lang_category == 2:  #Execution needed. Python and bash
            form = ProgramFormE(initial=initial)

    course = assignment.course
    return render_to_response('assignments/createProgram.html', {
        'form': form,
        'assignment': assignment,
        'course': course,
        'is_moderator': is_moderator
    },
                              context_instance=RequestContext(request))