started the basic framework evaluating results
authoramit@thunder
Fri, 21 May 2010 02:40:17 +0530
changeset 1 0eda880b3d25
parent 0 0b061d58aea3
child 2 654c583fd78e
started the basic framework evaluating results
testappproj/.database.sqlite3
testappproj/__init__.pyc
testappproj/settings.pyc
testappproj/templates/code.html
testappproj/testapp/__init__.pyc
testappproj/testapp/forms.py
testappproj/testapp/forms.pyc
testappproj/testapp/helpers/__init__.pyc
testappproj/testapp/helpers/bulkuploader.pyc
testappproj/testapp/management/__init__.pyc
testappproj/testapp/management/commands/__init__.pyc
testappproj/testapp/management/commands/seed_db.pyc
testappproj/testapp/models.py
testappproj/testapp/models.pyc
testappproj/testapp/views.py
testappproj/testapp/views.pyc
testappproj/urls.pyc
Binary file testappproj/.database.sqlite3 has changed
Binary file testappproj/__init__.pyc has changed
Binary file testappproj/settings.pyc has changed
--- a/testappproj/templates/code.html	Mon May 17 22:33:59 2010 +0530
+++ b/testappproj/templates/code.html	Fri May 21 02:40:17 2010 +0530
@@ -23,7 +23,7 @@
 {% ifequal pu.classname "ProblemUser" %}{{ pu.solution }}{% else %}{{ problem.skeleton }}{% endifequal %}
 
     </textarea></p>
-    <input type="hidden" name="problem_id" value="{{ problem.key.id }}" />
+    <input type="hidden" name="problem_id" value="{{ problem.id }}" />
     <p><input type="submit" name="submit" value="Run" /></p>
   </form>
 
Binary file testappproj/testapp/__init__.pyc has changed
--- a/testappproj/testapp/forms.py	Mon May 17 22:33:59 2010 +0530
+++ b/testappproj/testapp/forms.py	Fri May 21 02:40:17 2010 +0530
@@ -32,7 +32,7 @@
   Solution = forms.CharField(widget=forms.Textarea(attrs={'rows': '10', 'cols': '80'}),help_text="comma seperated in case of multiple solutions")
   Solution_Image = forms.ImageField(required=False)
   Session = forms.ChoiceField(choices=SESSION_CHOICES, required=True)
-
+  Credit=forms.IntegerField(required=True)
 
   class Meta:
    model = models.Problem
Binary file testappproj/testapp/forms.pyc has changed
Binary file testappproj/testapp/helpers/__init__.pyc has changed
Binary file testappproj/testapp/helpers/bulkuploader.pyc has changed
Binary file testappproj/testapp/management/__init__.pyc has changed
Binary file testappproj/testapp/management/commands/__init__.pyc has changed
Binary file testappproj/testapp/management/commands/seed_db.pyc has changed
--- a/testappproj/testapp/models.py	Mon May 17 22:33:59 2010 +0530
+++ b/testappproj/testapp/models.py	Fri May 21 02:40:17 2010 +0530
@@ -46,7 +46,7 @@
   author = models.CharField(max_length=255)
   created = models.DateTimeField()
   modified = models.DateTimeField()
-  
+  credit=models.IntegerField()
 
 
   #if  user.has_perm('testapp.add_bar') and  user.has_perm('foo.change_bar') and user.has_perm('foo.delete_bar')
@@ -57,7 +57,9 @@
   fullname=models.CharField(max_length=255)
 
   
-
+class Score(models.Model):
+  user = models.ForeignKey(User, unique=True)
+  total_credits=models.IntegerField(default=0,blank=False)
   
 
 
Binary file testappproj/testapp/models.pyc has changed
--- a/testappproj/testapp/views.py	Mon May 17 22:33:59 2010 +0530
+++ b/testappproj/testapp/views.py	Fri May 21 02:40:17 2010 +0530
@@ -1,10 +1,12 @@
+
 # Python imports
 import doctest
 import logging
 import os
 import traceback
 import random
-
+import StringIO
+import sys
 
 # # AppEngine imports
 
@@ -37,7 +39,8 @@
 from datetime import date
 from django.contrib.auth.decorators import permission_required
 from models import Problem
-
+from django.contrib.auth.decorators import login_required 
+from models import Score 
 
 def respond(request, user,template, params=None):
   """Helper to render a response, passing standard stuff to the response.
@@ -67,26 +70,54 @@
   template += '.html'
   return shortcuts.render_to_response(template, params)
 
-def execute_test_cases(request, test_cases, g):
-  li = []
+def execute_test_cases(code , solution):
   
-  solved = True
-  for e in test_cases:
-    if not e.want:
-      exec e.source in g
-      continue
-    call = e.source.strip()
-    got = eval(e.source.strip(), g)
-    expected = eval(e.want, g)
-    
-    if got == expected:
-      status = 'pass'
-    else:
-      status = 'fail'
-      solved = False
-    li.append([call, expected, "%r" % got, status])
+  print code 
+  print "solution"+solution
+
+
+  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__
+
+
 
-  return li, solved
+  
+  
+  s = codeOut.getvalue()
+  
+  s=unicode(s)
+  
+
+  print s.strip()
+  print solution.strip()
+  
+ 
+     
+  if solution.strip() == s.strip():
+    solved =True
+
+  errors=codeErr.getvalue()
+  print "errors"+errors
+  
+
+  codeOut.close()
+  codeErr.close()
+
+  return solved,errors
 
 
 
@@ -140,7 +171,7 @@
 #   pu = get_pu(problem)
 #   if pu == "PU_NOT_FOUND" or pu == "USER_NOT_SIGNED_IN": return False
 #   return pu.solved
-
+@login_required(function=None, redirect_field_name='next')
 def problems(request, category=None):
   user = request.user
   
@@ -183,16 +214,17 @@
              session =get_problem.session
                )
     entries.append(e)
-
+    
 
   return respond(request, user, 'problems',{'entries' : entries }  )
-
+@login_required(function=None, redirect_field_name='next')
 def code(request, problem_id):
   
-  print problem_id
+#  print problem_id
   problem = Problem.objects.get(id=int(problem_id))
-
+  
   if problem is None:
+  
     return http.HttpResponseNotFound('No such problem.')
   
 #  pu = get_pu(problem)
@@ -200,12 +232,15 @@
 #    pu = models.ProblemUser(problem=problem, user=users.get_current_user(), solution='', solved=False)
 #    pu.put()
 #    pu = None
-
-  return respond(request, request.user ,'code' , )
+  print problem.id
+  return respond(request, request.user ,'code' ,{'problem' : problem } )
 
 def run(request):
   user = request.user 
+  print request.POST
+
   problem_id = request.POST.get('problem_id')
+  
   if problem_id:
     problem = Problem.objects.get(id=int(problem_id))
 
@@ -229,16 +264,16 @@
   errors = ''
   
   
-  try:
-    print user_code
-    compiled = compile(user_code, 'error_file', 'exec')
-    g = {}
-    exec compiled in g
+  # try:
+  #   print user_code
+  #   compiled = compile(user_code, 'error_file', 'exec')
+  #   g = {}
+  #   exec compiled in g
 
 #    s = problem.tests.replace('\r\n', '\n')
 #    s += '\n\n'
     # test_cases = doctest.DocTestParser().get_examples(s) 
-    # results, solved = execute_test_cases(request, test_cases, g)
+  solved,errors = execute_test_cases(user_code,problem.solution)
     # if solved:
     #   pu = get_pu(problem)
     #   if pu.__class__ == models.ProblemUser:
@@ -249,10 +284,33 @@
     #   if pu.__class__ == models.ProblemUser:
     #     pu.solved = False
     #     pu.put()
-  except:
-    errors = traceback.format_exc()
-    return respond(request, user, 'traceback', {'errors': errors})
-  results="solved"
+  
+  
+  
+
+  if solved==True:
+    
+    #user is answering his first question
+    try:
+   # print user.id
+      score_for_user= Score.objects.get(user=user)
+      score_for_user.total_credits+=problem.credit
+      score_for_user.save()
+    
+   # user has answered questions previously
+    except Score.DoesNotExist:
+#      print user.id 
+      score_new_user=user.id
+      score_total_credits=problem.credit
+      score=Score(user_id=user.id,total_credits=score_total_credits)
+      score.save()
+  
+
+
+  else:
+    print "oops"
+  results=solved
+      
   return respond(request, request.user, 'run', {'results': results})
   
 
@@ -310,12 +368,14 @@
     problem_type=form.cleaned_data['Problem_type']
     solution=form.cleaned_data['Solution']
     session=form.cleaned_data['Session']
+    credit=form.cleaned_data['Credit']
+
     
     author = user.username
     created = date.today()
     modified=date.today()
-
-    problem=Problem(description=description,problem_type=problem_type,solution=solution,session=session,author=author,created=created,modified=modified)
+  
+    problem=Problem(description=description,problem_type=problem_type,solution=solution,session=session,author=author,created=created,modified=modified,credit=credit)
     
     problem.save() 
     print "saved"
@@ -328,14 +388,10 @@
   #    errors['__all__'] = unicode(err)
       
 
-  # if errors:
-  #   print "new world"
-  #   if internal:
-      
-  #     print errors
-  #     return ('error',errors)
-  #   return respond(request, user, 'new_edit_problem', 
-  #                        {'form': form, 'problem':problem, 'creating':creating_new,'error':errors})
+  if form.errors:
+    print "new world"
+    return respond(request, user, 'new_edit_problem', 
+                         {'form': form, 'problem':problem, 'creating':creating_new,'error':errors})
 
   # if creating_new:
   #   # if internal:
Binary file testappproj/testapp/views.pyc has changed
Binary file testappproj/urls.pyc has changed