testappproj/testapp/views.py
changeset 4 4d5422e5a45d
parent 3 34d0c21e3352
child 5 548c6261fa00
equal deleted inserted replaced
3:34d0c21e3352 4:4d5422e5a45d
    29 from models import Problem
    29 from models import Problem
    30 from django.contrib.auth.decorators import login_required 
    30 from django.contrib.auth.decorators import login_required 
    31 from models import Score 
    31 from models import Score 
    32 import time
    32 import time
    33 import sandbox
    33 import sandbox
    34 
    34 import re
    35 
    35 from django.utils.datastructures import MultiValueDictKeyError
    36 def respond(request, user,template, params=None):
    36 def respond(request, user,template, params=None):
    37   """Helper to render a response, passing standard stuff to the response.
    37   """Helper to render a response, passing standard stuff to the response.
    38 
    38 
    39   Args:
    39   Args:
    40     request: The request object.
    40     request: The request object.
    59     params['sign_in'] = 1
    59     params['sign_in'] = 1
    60   # if not template.endswith('.html'):
    60   # if not template.endswith('.html'):
    61   template += '.html'
    61   template += '.html'
    62   return shortcuts.render_to_response(template, params)
    62   return shortcuts.render_to_response(template, params)
    63 
    63 
       
    64 def import_check(user_code):
       
    65 
       
    66   no_imports=len(re.findall('import',user_code))
       
    67   
       
    68   if (no_imports >= 1):
       
    69     return True
       
    70   return False
       
    71   
       
    72 
       
    73 
       
    74 def plot_change_code(user_code):
       
    75   return user_code
       
    76 
       
    77 
    64 
    78 
    65 
    79 
    66 def check_examination_done(f):
    80 def check_examination_done(f):
    67         def wrap(request, *args, **kwargs):
    81         def wrap(request, *args, **kwargs):
    68                 #this check the session if userid key exist, if not it will redirect to login page
    82                 #this check the session if userid key exist, if not it will redirect to login page
       
    83               
       
    84                 if unicode(request.user)==unicode('amit'):
       
    85                   print "hello what the hell"
       
    86                   return f(request, *args, **kwargs)
    69                 user= Test_User.objects.get(user=request.user)
    87                 user= Test_User.objects.get(user=request.user)
    70                 
       
    71                 if user.exam_done==True:
    88                 if user.exam_done==True:
    72                         return HttpResponse("You have given the exam before")
    89                         return HttpResponse("You have given the exam before")
    73                 return f(request, *args, **kwargs)
    90                 return f(request, *args, **kwargs)
    74         return wrap
    91         return wrap
    75 
    92 
   136 
   153 
   137   return solved,errors
   154   return solved,errors
   138 
   155 
   139 def execute_test_cases(code , solution):
   156 def execute_test_cases(code , solution):
   140 
   157 
   141 
       
   142   
       
   143 
       
   144 
       
   145   solved=False
   158   solved=False
       
   159  
       
   160  
       
   161   if import_check(code):
       
   162     print "entered"
       
   163     return solved,None,True
   146   # create file-like string to capture output
   164   # create file-like string to capture output
       
   165 
       
   166 
       
   167 
   147   codeOut = StringIO.StringIO()
   168   codeOut = StringIO.StringIO()
   148   codeErr = StringIO.StringIO()
   169   codeErr = StringIO.StringIO()
   149 
   170 
   150   
   171   
   151 
   172 
   152 
   173 
   153   # capture output and errors
   174   # capture output and errors
   154   sys.stdout = codeOut
   175   sys.stdout = codeOut
   155   
   176   
   156   sys.stderr = codeErr
   177   sys.stderr = codeErr
   157   print "aaaklamnsldnlndskn"
   178  
   158   sandbox.execute(code)
   179   sandbox.execute(code)
   159 
   180 
   160 # restore stdout and stderr
   181 # restore stdout and stderr
   161   sys.stdout = sys.__stdout__
   182   sys.stdout = sys.__stdout__
   162   sys.stderr = sys.__stderr__
   183   sys.stderr = sys.__stderr__
   163 
   184 
   164 
   185 
   165   print "xs zc sdc"
   186 
   166   
   187   
   167   
   188   
   168   s = codeOut.getvalue()
   189   s = codeOut.getvalue()
   169   
   190   
   170   s=unicode(s)
   191   s=unicode(s)
   182   
   203   
   183   
   204   
   184 
   205 
   185   codeOut.close()
   206   codeOut.close()
   186   codeErr.close()
   207   codeErr.close()
   187 
   208   
   188   return solved,errors
   209   return solved,errors,False
   189 
   210 
   190 
   211 
   191 def complete(request):
   212 def complete(request):
   192   print request.user
   213   print request.user
   193   user= Test_User.objects.get(user=request.user)
   214   user= Test_User.objects.get(user=request.user)
   273    user_code += '\n\n'
   294    user_code += '\n\n'
   274 
   295 
   275    errors = ''
   296    errors = ''
   276   
   297   
   277   
   298   
   278    print "ksmdlnjdns"
   299 
   279 
   300 
   280    solved,errors = execute_test_cases(user_code,problem.solution)
   301    solved,errors,import_test= execute_test_cases(user_code,problem.solution)
   281    
   302    
   282    
   303    if import_test==True:
       
   304      
       
   305      return http.HttpResponse("DO NOT try to import or your exam and account will invalidated")
       
   306 
       
   307 
   283    if solved==True:
   308    if solved==True:
   284     
   309     
   285      #user is answering his first question
   310      #user is answering his first question
   286      try:
   311      try:
   287        print user.id
   312        print user.id
   335   
   360   
   336 
   361 
   337   
   362   
   338   
   363   
   339   if form.is_valid():
   364   if form.is_valid():
   340     
   365     try:
   341     description = form.cleaned_data['Description']
   366       description = form.cleaned_data['Description']
   342     problem_type=form.cleaned_data['Problem_type']
   367       problem_type=form.cleaned_data['Problem_type']
   343     solution=form.cleaned_data['Solution']
   368       solution=form.cleaned_data['Solution']
   344     session=form.cleaned_data['Session']
   369       session=form.cleaned_data['Session']
   345     credit=form.cleaned_data['Credit']
   370       credit=form.cleaned_data['Credit']
   346    # solution_image=form.cleaned_data['Solution_Image']
   371    # solution_image=form.cleaned_data['Solution_Image']
   347     solution_image=request.FILES['Solution_Image']
   372       solution_image=request.FILES['Solution_Image']
   348 
   373 
   349     author = user.username
   374       author = user.username
   350     created = date.today()
   375       created = date.today()
   351     modified=date.today()
   376       modified=date.today()
   352     
   377     
   353     # print request.FILES 
   378     # print request.FILES 
   354     # print "this is solution"+solution
   379     # print "this is solution"+solution
   355     # print "this is solution_image"+solution_image
   380     # print "this is solution_image"+solution_image
   356   
   381   
   357 
   382 
   358     problem=Problem(description=description,problem_type=problem_type,solution_image=solution_image,session=session,author=author,created=created,modified=modified,credit=credit)
   383       problem=Problem(description=description,problem_type=problem_type,solution_image=solution_image,session=session,author=author,created=created,modified=modified,credit=credit)
   359     
   384     
       
   385     except MultiValueDictKeyError:
       
   386       description = form.cleaned_data['Description']
       
   387       problem_type=form.cleaned_data['Problem_type']
       
   388       solution=form.cleaned_data['Solution']
       
   389       session=form.cleaned_data['Session']
       
   390       credit=form.cleaned_data['Credit']
       
   391    # solution_image=form.cleaned_data['Solution_Image']
       
   392     
       
   393 
       
   394       author = user.username
       
   395       created = date.today()
       
   396       modified=date.today()
       
   397     
       
   398       problem=Problem(description=description,problem_type=problem_type,solution=solution,session=session,author=author,created=created,modified=modified,credit=credit)  
       
   399 
       
   400 
   360     problem.save() 
   401     problem.save() 
   361     print "saved"
   402     print "saved"
   362   else:
   403   else:
   363     print "form is valid"
   404     print "form is valid"
   364     print form.errors
   405     print form.errors