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. |
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 |
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 |