testappproj/testapp/views.py
changeset 2 654c583fd78e
parent 1 0eda880b3d25
child 3 34d0c21e3352
equal deleted inserted replaced
1:0eda880b3d25 2:654c583fd78e
     1 
       
     2 # Python imports
     1 # Python imports
     3 import doctest
     2 import doctest
     4 import logging
     3 import logging
     5 import os
     4 import os
     6 import traceback
     5 import traceback
    13 from django.contrib.auth.models import User 
    12 from django.contrib.auth.models import User 
    14 
    13 
    15 from testappproj.testapp.models import *
    14 from testappproj.testapp.models import *
    16 from django.http import HttpResponse
    15 from django.http import HttpResponse
    17 
    16 
    18 # from google.appengine.api import users
       
    19 # from google.appengine.ext.webapp import template 
       
    20 
       
    21 # from google.appengine.ext import db
       
    22 # from google.appengine.ext.db import djangoforms
       
    23 
    17 
    24 
    18 
    25 # Django imports 
    19 # Django imports 
    26 #from django.conf import settings
    20 #from django.conf import settings
    27 #settings._target = None
    21 #settings._target = None
    39 from datetime import date
    33 from datetime import date
    40 from django.contrib.auth.decorators import permission_required
    34 from django.contrib.auth.decorators import permission_required
    41 from models import Problem
    35 from models import Problem
    42 from django.contrib.auth.decorators import login_required 
    36 from django.contrib.auth.decorators import login_required 
    43 from models import Score 
    37 from models import Score 
       
    38 import time
       
    39 
       
    40 
       
    41 # def handle_uploaded_file(f):
       
    42 #   print f  
       
    43 #   destination = open('some/file/name.txt', 'wb+')
       
    44 #   for chunk in f.chunks():
       
    45 #     destination.write(chunk)
       
    46 #   destination.close()
       
    47 
       
    48 
    44 
    49 
    45 def respond(request, user,template, params=None):
    50 def respond(request, user,template, params=None):
    46   """Helper to render a response, passing standard stuff to the response.
    51   """Helper to render a response, passing standard stuff to the response.
    47 
    52 
    48   Args:
    53   Args:
    68     params['sign_in'] = 1
    73     params['sign_in'] = 1
    69   # if not template.endswith('.html'):
    74   # if not template.endswith('.html'):
    70   template += '.html'
    75   template += '.html'
    71   return shortcuts.render_to_response(template, params)
    76   return shortcuts.render_to_response(template, params)
    72 
    77 
    73 def execute_test_cases(code , solution):
    78 def execute_plotting_test_cases(user_code , solution_image,problem_id , username):
    74   
    79   
    75   print code 
    80   print  user_code 
    76   print "solution"+solution
    81   print "solution"+solution_image
    77 
    82 
       
    83   image_name = username+'_'+str(problem_id)+'_'+time.time()
       
    84   
       
    85   code="""from pylab import *
       
    86   %s
       
    87   show()"""%user_code
    78 
    88 
    79   solved=False
    89   solved=False
    80   # create file-like string to capture output
    90   # create file-like string to capture output
    81   codeOut = StringIO.StringIO()
    91   codeOut = StringIO.StringIO()
    82   codeErr = StringIO.StringIO()
    92   codeErr = StringIO.StringIO()
   109      
   119      
   110   if solution.strip() == s.strip():
   120   if solution.strip() == s.strip():
   111     solved =True
   121     solved =True
   112 
   122 
   113   errors=codeErr.getvalue()
   123   errors=codeErr.getvalue()
   114   print "errors"+errors
   124   
   115   
   125   
   116 
   126 
   117   codeOut.close()
   127   codeOut.close()
   118   codeErr.close()
   128   codeErr.close()
   119 
   129 
   120   return solved,errors
   130   return solved,errors
   121 
   131 
       
   132 def execute_test_cases(code , solution):
       
   133   
       
   134   print code 
       
   135   print "solution"+solution
       
   136 
       
   137 
       
   138   solved=False
       
   139   # create file-like string to capture output
       
   140   codeOut = StringIO.StringIO()
       
   141   codeErr = StringIO.StringIO()
       
   142 
       
   143 
       
   144 
       
   145   # capture output and errors
       
   146   sys.stdout = codeOut
       
   147   sys.stderr = codeErr
       
   148 
       
   149   exec code
       
   150 
       
   151 # restore stdout and stderr
       
   152   sys.stdout = sys.__stdout__
       
   153   sys.stderr = sys.__stderr__
       
   154 
       
   155 
       
   156 
       
   157   
       
   158   
       
   159   s = codeOut.getvalue()
       
   160   
       
   161   s=unicode(s)
       
   162   
       
   163 
       
   164   print s.strip()
       
   165   print solution.strip()
       
   166   
       
   167  
       
   168      
       
   169   if solution.strip() == s.strip():
       
   170     solved =True
       
   171 
       
   172   errors=codeErr.getvalue()
       
   173   
       
   174   
       
   175 
       
   176   codeOut.close()
       
   177   codeErr.close()
       
   178 
       
   179   return solved,errors
   122 
   180 
   123 
   181 
   124 
   182 
   125 def index(request):
   183 def index(request):
   126   """ need to change user in the django.contrib way"""
   184   """ need to change user in the django.contrib way"""
   215                )
   273                )
   216     entries.append(e)
   274     entries.append(e)
   217     
   275     
   218 
   276 
   219   return respond(request, user, 'problems',{'entries' : entries }  )
   277   return respond(request, user, 'problems',{'entries' : entries }  )
       
   278 
       
   279 
       
   280 
   220 @login_required(function=None, redirect_field_name='next')
   281 @login_required(function=None, redirect_field_name='next')
   221 def code(request, problem_id):
   282 def code(request, problem_id):
   222   
   283   
   223 #  print problem_id
   284 #  print problem_id
   224   problem = Problem.objects.get(id=int(problem_id))
   285   problem = Problem.objects.get(id=int(problem_id))
   318 def new_edit(request, problem_id=None, internal=False):
   379 def new_edit(request, problem_id=None, internal=False):
   319   
   380   
   320 
   381 
   321 
   382 
   322   # internal indicates that it is being called internally by uploader
   383   # internal indicates that it is being called internally by uploader
   323   #
   384   
   324   user = request.user
   385   user = request.user
   325 #  print user.get_all_permissions()
       
   326   print user.username
       
   327   if user.is_anonymous() :
   386   if user.is_anonymous() :
   328     return http.HttpResponseForbidden('You must be an signed in to create edit a problem.')
   387     return http.HttpResponseForbidden('You must be an signed in to create edit a problem.')
   329 
   388 
   330   if problem_id is None:
   389   if problem_id is None:
   331     creating_new = True
   390     creating_new = True
   332   else:
   391   else:
   333     creating_new = False
   392     creating_new = False
   334     
   393     
   335 #  problem = None
       
   336   # if problem_id:
       
   337   #   problem = models.Problem.get(db.Key.from_path(models.Problem.kind(), int(problem_id)))
       
   338   #   if problem.author != user and not users.is_current_user_admin():
       
   339   #     return http.HttpResponseForbidden('You can only edit your own problems.')
       
   340   #   if problem is None:
       
   341   #     return http.HttpResponseNotFound('No such problem.')  
       
   342  
       
   343 #   formset = ProblemForm(data=request.POST)  
       
   344 #   instance=formset.save()
       
   345 # #  upload_form = UploadForm(data=request.POST)
       
   346 
       
   347   if request.method == 'POST':
   394   if request.method == 'POST':
   348     form = ProblemForm(request.POST)
   395     form = ProblemForm(request.POST, request.FILES)
   349   else:
   396   else:
   350     form = ProblemForm()
   397     form = ProblemForm()
   351 
   398 
   352   
   399   
   353 
   400 
   354   if not request.POST:
   401   if not request.POST:
   355     return respond(request, user, 'new_edit_problem', {'form':form, 'problem':None, 'creating':creating_new })
   402     return respond(request, user, 'new_edit_problem', {'form':form, 'problem':None, 'creating':creating_new })
   356   
   403   
   357 #  print form
   404 
   358   #errors = form.errors
   405   
   359   #print str(errors)+"errors"
       
   360 
       
   361   
       
   362   #if not errors:
       
   363   #  try:
       
   364   
   406   
   365   if form.is_valid():
   407   if form.is_valid():
   366     
   408     
   367     description = form.cleaned_data['Description']
   409     description = form.cleaned_data['Description']
   368     problem_type=form.cleaned_data['Problem_type']
   410     problem_type=form.cleaned_data['Problem_type']
   369     solution=form.cleaned_data['Solution']
   411     solution=form.cleaned_data['Solution']
   370     session=form.cleaned_data['Session']
   412     session=form.cleaned_data['Session']
   371     credit=form.cleaned_data['Credit']
   413     credit=form.cleaned_data['Credit']
   372 
   414    # solution_image=form.cleaned_data['Solution_Image']
   373     
   415     solution_image=request.FILES['Solution_Image']
       
   416 
   374     author = user.username
   417     author = user.username
   375     created = date.today()
   418     created = date.today()
   376     modified=date.today()
   419     modified=date.today()
   377   
   420     
   378     problem=Problem(description=description,problem_type=problem_type,solution=solution,session=session,author=author,created=created,modified=modified,credit=credit)
   421     # print request.FILES 
       
   422     # print "this is solution"+solution
       
   423     # print "this is solution_image"+solution_image
       
   424   
       
   425 
       
   426     problem=Problem(description=description,problem_type=problem_type,solution_image=solution_image,session=session,author=author,created=created,modified=modified,credit=credit)
   379     
   427     
   380     problem.save() 
   428     problem.save() 
   381     print "saved"
   429     print "saved"
   382   else:
   430   else:
   383     print "form is valid"
   431     print "form is valid"