diff -r 0eda880b3d25 -r 654c583fd78e testappproj/testapp/views.py --- a/testappproj/testapp/views.py Fri May 21 02:40:17 2010 +0530 +++ b/testappproj/testapp/views.py Mon May 31 19:18:57 2010 +0530 @@ -1,4 +1,3 @@ - # Python imports import doctest import logging @@ -15,11 +14,6 @@ from testappproj.testapp.models import * from django.http import HttpResponse -# from google.appengine.api import users -# from google.appengine.ext.webapp import template - -# from google.appengine.ext import db -# from google.appengine.ext.db import djangoforms # Django imports @@ -41,6 +35,17 @@ from models import Problem from django.contrib.auth.decorators import login_required from models import Score +import time + + +# def handle_uploaded_file(f): +# print f +# destination = open('some/file/name.txt', 'wb+') +# for chunk in f.chunks(): +# destination.write(chunk) +# destination.close() + + def respond(request, user,template, params=None): """Helper to render a response, passing standard stuff to the response. @@ -70,6 +75,60 @@ template += '.html' return shortcuts.render_to_response(template, params) +def execute_plotting_test_cases(user_code , solution_image,problem_id , username): + + print user_code + print "solution"+solution_image + + image_name = username+'_'+str(problem_id)+'_'+time.time() + + code="""from pylab import * + %s + show()"""%user_code + + solved=False + # create file-like string to capture output + codeOut = StringIO.StringIO() + codeErr = StringIO.StringIO() + + + + # capture output and errors + sys.stdout = codeOut + sys.stderr = codeErr + + exec code + +# restore stdout and stderr + sys.stdout = sys.__stdout__ + sys.stderr = sys.__stderr__ + + + + + + s = codeOut.getvalue() + + s=unicode(s) + + + print s.strip() + print solution.strip() + + + + if solution.strip() == s.strip(): + solved =True + + errors=codeErr.getvalue() + + + + codeOut.close() + codeErr.close() + + return solved,errors + def execute_test_cases(code , solution): print code @@ -111,7 +170,7 @@ solved =True errors=codeErr.getvalue() - print "errors"+errors + codeOut.close() @@ -121,7 +180,6 @@ - def index(request): """ need to change user in the django.contrib way""" @@ -217,6 +275,9 @@ return respond(request, user, 'problems',{'entries' : entries } ) + + + @login_required(function=None, redirect_field_name='next') def code(request, problem_id): @@ -320,10 +381,8 @@ # internal indicates that it is being called internally by uploader - # + user = request.user -# print user.get_all_permissions() - print user.username if user.is_anonymous() : return http.HttpResponseForbidden('You must be an signed in to create edit a problem.') @@ -332,20 +391,8 @@ else: creating_new = False -# problem = None - # if problem_id: - # problem = models.Problem.get(db.Key.from_path(models.Problem.kind(), int(problem_id))) - # if problem.author != user and not users.is_current_user_admin(): - # return http.HttpResponseForbidden('You can only edit your own problems.') - # if problem is None: - # return http.HttpResponseNotFound('No such problem.') - -# formset = ProblemForm(data=request.POST) -# instance=formset.save() -# # upload_form = UploadForm(data=request.POST) - if request.method == 'POST': - form = ProblemForm(request.POST) + form = ProblemForm(request.POST, request.FILES) else: form = ProblemForm() @@ -354,13 +401,8 @@ if not request.POST: return respond(request, user, 'new_edit_problem', {'form':form, 'problem':None, 'creating':creating_new }) -# print form - #errors = form.errors - #print str(errors)+"errors" - #if not errors: - # try: if form.is_valid(): @@ -369,13 +411,19 @@ solution=form.cleaned_data['Solution'] session=form.cleaned_data['Session'] credit=form.cleaned_data['Credit'] + # solution_image=form.cleaned_data['Solution_Image'] + solution_image=request.FILES['Solution_Image'] - author = user.username created = date.today() modified=date.today() + + # print request.FILES + # print "this is solution"+solution + # print "this is solution_image"+solution_image - problem=Problem(description=description,problem_type=problem_type,solution=solution,session=session,author=author,created=created,modified=modified,credit=credit) + + problem=Problem(description=description,problem_type=problem_type,solution_image=solution_image,session=session,author=author,created=created,modified=modified,credit=credit) problem.save() print "saved"