From 029e953d4ca617dbead2dde1981264f3399a20ad Mon Sep 17 00:00:00 2001 From: raylu Date: Fri, 13 May 2011 01:42:47 -0400 Subject: Grading (but no meaningful database manipulation yet) --- graders/lab1.py | 29 +++++++++++++++++++++++++++++ pyc/grader/models.py | 2 +- pyc/grader/views.py | 22 ++++++++++++++++++++-- pyc/templates/submit.html | 10 ++++++++++ requirements.txt | 1 + 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100755 graders/lab1.py diff --git a/graders/lab1.py b/graders/lab1.py new file mode 100755 index 0000000..86e595f --- /dev/null +++ b/graders/lab1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +from sandbox import Sandbox, SandboxConfig +import sys +from os import path + +importname = sys.argv[1] +if importname.endswith('.py'): + importname = importname[:len(importname)-3] +else: + print 'Filename did not end with .py for some reason...' + sys.exit(2) + +sys.path.insert(0, path.expanduser('~/submissions')) +sys.dont_write_bytecode = True + +def run(): + exec("import %s as submission" % importname) + if submission.test() == 1: + return 100 + else: + return 0 + +config = SandboxConfig() +config.timeout = 5 +config.allowModule(importname, 'test') +sandbox = Sandbox(config) +score = sandbox.call(run) +print score diff --git a/pyc/grader/models.py b/pyc/grader/models.py index ba19e03..e11f371 100644 --- a/pyc/grader/models.py +++ b/pyc/grader/models.py @@ -10,7 +10,7 @@ class Submission(models.Model): from django.contrib.auth.models import User def upload_filename(instance, filename): from time import time - return "%d-%d.%d" % (instance.lab.id, instance.user.id, int(time())) + return "s%d_%d_%d.py" % (instance.lab.id, instance.user.id, int(time())) lab = models.ForeignKey(Lab) user = models.ForeignKey(User) time = models.DateTimeField(auto_now_add=True) diff --git a/pyc/grader/views.py b/pyc/grader/views.py index 4f81309..df7e0b9 100644 --- a/pyc/grader/views.py +++ b/pyc/grader/views.py @@ -21,20 +21,38 @@ from django.contrib.auth.decorators import login_required @login_required def submit(request, lab_id): from django.http import HttpResponseRedirect + from django.db import IntegrityError if request.method == 'POST': form = SubmissionForm(request.POST, request.FILES) if form.is_valid(): labobj = Lab.objects.get(pk=lab_id) rfile = request.FILES['file'] submission = Submission(lab=labobj, user=request.user, file=rfile) - submission.save() - return HttpResponseRedirect("/lab/%s/" % lab_id) + try: + submission.save() + except IntegrityError: + pass + score, err = grade(submission.file.name, lab_id) + return render_to_response('submit.html', { + 'err' : err, + 'score' : score, + 'form' : form, + }, context_instance=RequestContext(request)) else: form = SubmissionForm() return render_to_response('submit.html', { 'form' : form, }, context_instance=RequestContext(request)) +def grade(file, id): + import subprocess + from os import path + graderfile = "lab%s.py" % id + graderdir = path.dirname(__file__) + '/../../graders/' + grader = graderdir + graderfile + p = subprocess.Popen([grader, file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + return p.communicate() + from django import forms class SubmissionForm(forms.Form): file = forms.FileField() diff --git a/pyc/templates/submit.html b/pyc/templates/submit.html index e8746d5..a8a05fa 100644 --- a/pyc/templates/submit.html +++ b/pyc/templates/submit.html @@ -1,5 +1,15 @@ {% extends 'base.html' %} {% block body %} + +{% if score %} +

You scored {{ score }}

+{% endif %} +{% if err %} +
+{{ err }}
+
+{% endif %} +

{{ form }} diff --git a/requirements.txt b/requirements.txt index d3e4ba5..5b83f02 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ django +pysandbox -- cgit v1.2.3