testappproj/testapp/views.py
changeset 5 548c6261fa00
parent 4 4d5422e5a45d
child 6 d0f107095b1a
equal deleted inserted replaced
4:4d5422e5a45d 5:548c6261fa00
     5 import traceback
     5 import traceback
     6 import random
     6 import random
     7 import StringIO
     7 import StringIO
     8 import sys
     8 import sys
     9 import json
     9 import json
       
    10 import time
       
    11 import sandbox
       
    12 import re
       
    13 from matplotlib.pyplot import imread
       
    14 from numpy import alltrue
    10 
    15 
    11 
    16 
    12 from django.contrib.auth.models import User 
    17 from django.contrib.auth.models import User 
    13 
    18 
    14 from testappproj.testapp.models import *
    19 from testappproj.testapp.models import *
    27 from datetime import date
    32 from datetime import date
    28 from django.contrib.auth.decorators import permission_required
    33 from django.contrib.auth.decorators import permission_required
    29 from models import Problem
    34 from models import Problem
    30 from django.contrib.auth.decorators import login_required 
    35 from django.contrib.auth.decorators import login_required 
    31 from models import Score 
    36 from models import Score 
    32 import time
       
    33 import sandbox
       
    34 import re
       
    35 from django.utils.datastructures import MultiValueDictKeyError
    37 from django.utils.datastructures import MultiValueDictKeyError
       
    38 
       
    39 
    36 def respond(request, user,template, params=None):
    40 def respond(request, user,template, params=None):
    37   """Helper to render a response, passing standard stuff to the response.
    41   """Helper to render a response, passing standard stuff to the response.
    38 
    42 
    39   Args:
    43   Args:
    40     request: The request object.
    44     request: The request object.
    70   return False
    74   return False
    71   
    75   
    72 
    76 
    73 
    77 
    74 def plot_change_code(user_code):
    78 def plot_change_code(user_code):
       
    79   user_code=re.sub("show\(\)",'',user_code)
       
    80 
    75   return user_code
    81   return user_code
    76 
    82 
    77 
    83 
    78 
    84 
    79 
    85 
    98 
   104 
    99 
   105 
   100 
   106 
   101 
   107 
   102 def execute_plotting_test_cases(user_code , solution_image,problem_id , username):
   108 def execute_plotting_test_cases(user_code , solution_image,problem_id , username):
   103   
   109    
   104   print  user_code 
   110 
   105   print "solution"+solution_image
   111   solved=False
   106 
   112 
   107   image_name = username+'_'+str(problem_id)+'_'+time.time()
   113   if import_check(user_code):
   108   
   114     print "entered"
       
   115     return solved,None,True
       
   116 
       
   117  
       
   118   user_image_name = str(username)+'_'+str(problem_id)+'_'+str(int(time.time()))+'.png'
       
   119    
       
   120   
       
   121   user_code=plot_change_code(user_code)
       
   122 
       
   123 
       
   124 
   109   code="""from pylab import *
   125   code="""from pylab import *
   110   %s
   126 import pylab
   111   show()"""%user_code
   127 %s
   112 
   128 figure_conf=pylab.gcf()
   113   solved=False
   129 figure_conf.set_size_inches(10,10)
       
   130 figure_conf.set_dpi(80)
       
   131 figure_conf.savefig('%s')"""%(user_code,user_image_name)
       
   132 
       
   133   
       
   134   print code
   114   # create file-like string to capture output
   135   # create file-like string to capture output
   115   codeOut = StringIO.StringIO()
   136   codeOut = StringIO.StringIO()
   116   codeErr = StringIO.StringIO()
   137   codeErr = StringIO.StringIO()
   117 
   138 
   118 
   139   print str(codeOut)
   119   
   140   
   120   # capture output and errors
   141   # capture output and errors
   121   sys.stdout = codeOut
   142   sys.stdout = codeOut
   122   sys.stderr = codeErr
   143   sys.stderr = codeErr
   123 
   144 
       
   145  
   124   sandbox.execute(code)
   146   sandbox.execute(code)
   125 
   147 
       
   148  
       
   149  
   126 # restore stdout and stderr
   150 # restore stdout and stderr
   127   sys.stdout = sys.__stdout__
   151   sys.stdout = sys.__stdout__
   128   sys.stderr = sys.__stderr__
   152   sys.stderr = sys.__stderr__
   129 
   153 
   130 
   154 
   131 
   155   new_solution_image='media-root/'+str((solution_image))
   132   
   156   solution_image_matrix=imread(new_solution_image)
   133   
   157   print str(solution_image_matrix)
   134   s = codeOut.getvalue()
   158   print user_image_name
   135   
   159   user_image_matrix=imread(user_image_name)
   136   s=unicode(s)
   160 
   137   
   161 
   138 
   162 
   139   print s.strip()
   163   if solution_image_matrix==user_image_matrix:
   140   print solution.strip()
   164     solved=True
   141   
   165 
   142  
   166   errors=codeErr.getvalue()  
   143      
       
   144   if solution.strip() == s.strip():
       
   145     solved =True
       
   146 
       
   147   errors=codeErr.getvalue()
       
   148   
       
   149   
       
   150 
   167 
   151   codeOut.close()
   168   codeOut.close()
   152   codeErr.close()
   169   codeErr.close()
   153 
   170 
   154   return solved,errors
   171 
       
   172   print solved
       
   173   return solved,errors,False
   155 
   174 
   156 def execute_test_cases(code , solution):
   175 def execute_test_cases(code , solution):
   157 
   176 
   158   solved=False
   177   solved=False
   159  
   178  
   198      
   217      
   199   if solution.strip() == s.strip():
   218   if solution.strip() == s.strip():
   200     solved =True
   219     solved =True
   201 
   220 
   202   errors=codeErr.getvalue()
   221   errors=codeErr.getvalue()
       
   222   
       
   223 
       
   224 
   203   
   225   
   204   
   226   
   205 
   227 
   206   codeOut.close()
   228   codeOut.close()
   207   codeErr.close()
   229   codeErr.close()
   272   return respond(request, request.user ,'code' ,{'problem' : problem } )
   294   return respond(request, request.user ,'code' ,{'problem' : problem } )
   273 
   295 
   274 def run(request):
   296 def run(request):
   275 
   297 
   276    user = request.user 
   298    user = request.user 
   277    print user
   299 
   278    problem_id = request.POST.get('problem_id')
   300    problem_id = request.POST.get('problem_id')
   279   
   301   
   280    if problem_id:
   302    if problem_id:
   281      problem = Problem.objects.get(id=int(problem_id))
   303      problem = Problem.objects.get(id=int(problem_id))
   282 
   304 
   293    user_code = user_code.replace('\r\n', '\n')
   315    user_code = user_code.replace('\r\n', '\n')
   294    user_code += '\n\n'
   316    user_code += '\n\n'
   295 
   317 
   296    errors = ''
   318    errors = ''
   297   
   319   
   298   
   320    print problem.problem_type
   299 
   321 
   300 
   322 
   301    solved,errors,import_test= execute_test_cases(user_code,problem.solution)
   323    if unicode(problem.problem_type)==unicode('S'):
       
   324      solved,errors,import_test= execute_test_cases(user_code,problem.solution)
       
   325    else :
       
   326      solved,errors,import_test= execute_plotting_test_cases(user_code,problem.solution_image,problem_id,user)
   302    
   327    
       
   328 
       
   329 
       
   330    print solved
       
   331 
   303    if import_test==True:
   332    if import_test==True:
   304      
   333      
   305      return http.HttpResponse("DO NOT try to import or your exam and account will invalidated")
   334      return http.HttpResponse("DO NOT try to import or your exam and account will invalidated")
   306 
   335 
   307 
   336 
   323        score.save()
   352        score.save()
   324   
   353   
   325 
   354 
   326 
   355 
   327    else:
   356    else:
   328      http.HttpResponse('Wrong Answer')
   357      return http.HttpResponse('Wrong Answer')
   329      
   358      
   330  
   359  
   331    return http.HttpResponse("Right Answer")
   360    return http.HttpResponse("Right Answer")
   332 
   361 
   333   
   362