checking if somebody is importing a libarary in code
authoramit@thunder
Mon, 14 Jun 2010 22:02:14 +0530
changeset 4 4d5422e5a45d
parent 3 34d0c21e3352
child 5 548c6261fa00
checking if somebody is importing a libarary in code
testappproj/.database.sqlite3
testappproj/testapp/forms.py
testappproj/testapp/forms.pyc
testappproj/testapp/views.py
testappproj/testapp/views.pyc
testappproj/urls.py
testappproj/urls.pyc
Binary file testappproj/.database.sqlite3 has changed
--- a/testappproj/testapp/forms.py	Mon Jun 14 01:00:59 2010 +0530
+++ b/testappproj/testapp/forms.py	Mon Jun 14 22:02:14 2010 +0530
@@ -1,3 +1,4 @@
+from recaptcha.client import captcha
 from django import forms
 from registration.forms import RegistrationForm
 from django.utils.translation import ugettext_lazy as _
@@ -5,6 +6,9 @@
 from registration.models import RegistrationProfile
 #from google.appengine.ext.db import djangoforms
 import models
+from django.conf import settings
+from django.utils.safestring import mark_safe
+
 
 
 attrs_dict = { 'class': 'required' }
@@ -26,6 +30,47 @@
   )
 
 
+# class ReCaptcha(forms.Widget):
+#     input_type = None # Subclasses must define this.
+
+#     def render(self, name, value, attrs=None):
+#         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+#         html = u"<script>var RecaptchaOptions = {theme : '%s'};</script>" % (
+#             final_attrs.get('theme', 'white'))
+#         html += captcha.displayhtml(settings.RECAPTCHA_PUBLIC)
+#         return mark_safe(html)
+
+#     def value_from_datadict(self, data, files, name):
+#         return {
+#             'recaptcha_challenge_field': data.get('recaptcha_challenge_field', None),
+#             'recaptcha_response_field': data.get('recaptcha_response_field', None),
+#         }
+
+# class ReCaptchaField(forms.FileField):
+#     widget = ReCaptcha
+#     default_error_messages = {
+#         'invalid-site-public-key': u"Invalid public key",
+#         'invalid-site-private-key': u"Invalid private key",
+#         'invalid-request-cookie': u"Invalid cookie",
+#         'incorrect-captcha-sol': u"Invalid entry, please try again.",
+#         'verify-params-incorrect': u"The parameters to verify were incorrect, make sure you are passing all the required parameters.",
+#         'invalid-referrer': u"Invalid referrer domain",
+#         'recaptcha-not-reachable': u"Could not contact reCAPTCHA server",
+#     }
+
+#     def clean(self, data, initial):
+#         if initial is None or initial == '':
+#             raise Exception("ReCaptchaField requires the client's IP be set to the initial value")
+#         ip = initial
+#         resp = captcha.submit(data.get("recaptcha_challenge_field", None),
+#                               data.get("recaptcha_response_field", None),
+#                               settings.RECAPTCHA_PRIVATE, ip)
+#         if not resp.is_valid:
+#             raise forms.ValidationError(self.default_error_messages.get(
+#                     resp.error_code, "Unknown error: %s" % (resp.error_code)))
+
+
+
 class ProblemForm(forms.Form):
   Description = forms.CharField(widget=forms.Textarea(attrs={'rows': '10', 'cols': '80'}),required=True,help_text="To be used as the question")
   Problem_type = forms.ChoiceField(choices=PROBLEM_CHOICES,required=True)
@@ -44,7 +89,8 @@
 
   Fullname = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
   Address =  forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
-  
+#  captcha = ReCaptchaField()
+
   class Meta:
         exclude = ('special_user','exam_done')
 
Binary file testappproj/testapp/forms.pyc has changed
--- a/testappproj/testapp/views.py	Mon Jun 14 01:00:59 2010 +0530
+++ b/testappproj/testapp/views.py	Mon Jun 14 22:02:14 2010 +0530
@@ -31,8 +31,8 @@
 from models import Score 
 import time
 import sandbox
-
-
+import re
+from django.utils.datastructures import MultiValueDictKeyError
 def respond(request, user,template, params=None):
   """Helper to render a response, passing standard stuff to the response.
 
@@ -61,13 +61,30 @@
   template += '.html'
   return shortcuts.render_to_response(template, params)
 
+def import_check(user_code):
+
+  no_imports=len(re.findall('import',user_code))
+  
+  if (no_imports >= 1):
+    return True
+  return False
+  
+
+
+def plot_change_code(user_code):
+  return user_code
+
+
 
 
 def check_examination_done(f):
         def wrap(request, *args, **kwargs):
                 #this check the session if userid key exist, if not it will redirect to login page
+              
+                if unicode(request.user)==unicode('amit'):
+                  print "hello what the hell"
+                  return f(request, *args, **kwargs)
                 user= Test_User.objects.get(user=request.user)
-                
                 if user.exam_done==True:
                         return HttpResponse("You have given the exam before")
                 return f(request, *args, **kwargs)
@@ -138,12 +155,16 @@
 
 def execute_test_cases(code , solution):
 
-
-  
+  solved=False
+ 
+ 
+  if import_check(code):
+    print "entered"
+    return solved,None,True
+  # create file-like string to capture output
 
 
-  solved=False
-  # create file-like string to capture output
+
   codeOut = StringIO.StringIO()
   codeErr = StringIO.StringIO()
 
@@ -154,7 +175,7 @@
   sys.stdout = codeOut
   
   sys.stderr = codeErr
-  print "aaaklamnsldnlndskn"
+ 
   sandbox.execute(code)
 
 # restore stdout and stderr
@@ -162,7 +183,7 @@
   sys.stderr = sys.__stderr__
 
 
-  print "xs zc sdc"
+
   
   
   s = codeOut.getvalue()
@@ -184,8 +205,8 @@
 
   codeOut.close()
   codeErr.close()
-
-  return solved,errors
+  
+  return solved,errors,False
 
 
 def complete(request):
@@ -275,11 +296,15 @@
    errors = ''
   
   
-   print "ksmdlnjdns"
+
 
-   solved,errors = execute_test_cases(user_code,problem.solution)
+   solved,errors,import_test= execute_test_cases(user_code,problem.solution)
    
-   
+   if import_test==True:
+     
+     return http.HttpResponse("DO NOT try to import or your exam and account will invalidated")
+
+
    if solved==True:
     
      #user is answering his first question
@@ -337,26 +362,42 @@
   
   
   if form.is_valid():
-    
-    description = form.cleaned_data['Description']
-    problem_type=form.cleaned_data['Problem_type']
-    solution=form.cleaned_data['Solution']
-    session=form.cleaned_data['Session']
-    credit=form.cleaned_data['Credit']
+    try:
+      description = form.cleaned_data['Description']
+      problem_type=form.cleaned_data['Problem_type']
+      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']
+      solution_image=request.FILES['Solution_Image']
 
-    author = user.username
-    created = date.today()
-    modified=date.today()
+      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_image=solution_image,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)
     
+    except MultiValueDictKeyError:
+      description = form.cleaned_data['Description']
+      problem_type=form.cleaned_data['Problem_type']
+      solution=form.cleaned_data['Solution']
+      session=form.cleaned_data['Session']
+      credit=form.cleaned_data['Credit']
+   # solution_image=form.cleaned_data['Solution_Image']
+    
+
+      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,credit=credit)  
+
+
     problem.save() 
     print "saved"
   else:
Binary file testappproj/testapp/views.pyc has changed
--- a/testappproj/urls.py	Mon Jun 14 01:00:59 2010 +0530
+++ b/testappproj/urls.py	Mon Jun 14 22:02:14 2010 +0530
@@ -21,6 +21,8 @@
 from registration.views import register
 from testappproj.testapp.forms import Test_UserForm
 from django.contrib import admin
+
+
 admin.autodiscover()
 
 
Binary file testappproj/urls.pyc has changed