Commit 49a000a3 by Piotr Mitros

Folding stuff in capa_module from views

parent 9e18b261
......@@ -33,8 +33,6 @@ class LoncapaModule(XModule):
return render_to_string('problem.html',
{'problem':content, 'id':self.filename})
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None):
XModule.__init__(self, xml, item_id, ajax_url, track_url, state)
dom=parseString(xml)
......@@ -48,4 +46,11 @@ class LoncapaModule(XModule):
# Temporary
def check_problem(self, get):
pass
answer=dict()
# input_resistor_1 ==> resistor_1
for key in get:
answer['_'.join(key.split('_')[1:])]=get[key]
js=json.dumps(self.lcp.grade_answers(answer))
return js
......@@ -49,6 +49,7 @@ class StudentModule(models.Model):
module_id = models.CharField(max_length=255) # Filename for homeworks, etc.
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
xml = models.TextField(blank=True)
class Meta:
unique_together = (('student', 'module_id', 'module_type'),)
......
......@@ -128,7 +128,8 @@ def render_x_module(request, xml_module):
problem=capa_module.LoncapaModule(xml_module.toxml(), module_id)
smod=StudentModule(student=request.user,
module_id=module_id,
state=problem.get_state())
state=problem.get_state(),
xml=problem.xml)
smod.save()
elif len(s) == 1:
# If so, render it
......@@ -140,56 +141,32 @@ def render_x_module(request, xml_module):
return {'content':problem.get_html()}
def modx_dispatch(request, dispatch=None, id=None):
s = StudentModule.objects.filter(student=request.user, module_id=id)
def modx_dispatch(request, module=None, dispatch=None, id=None):
s = StudentModule.objects.filter(module_type=module, student=request.user, module_id=id)
if len(s) == 0:
raise Http404
s=s[0]
dispatch=dispatch.split('?')[0]
if dispatch=='problem_check':
return check_problem(request)
problem=capa_module.LoncapaModule(s.xml, s.module_id, state=s.state)
html = problem.check_problem(request.GET)
s.state=problem.get_state()
s.grade=problem.get_score()['score']
s.save()
return HttpResponse(html) #check_problem(s, request.GET)
elif dispatch=='problem_reset':
return reset_problem(request,id)
else:
print "AAA"
raise Http404
def reset_problem(request,id):
s = StudentModule.objects.filter(student=request.user, module_id=id)
s[0].delete()
return HttpResponse(json.dumps({}), mimetype="application/json")
def check_problem(request):
answer=dict()
# input_resistor_1 ==> resistor_1
for key in request.GET:
answer['_'.join(key.split('_')[1:])]=request.GET[key]
## THE NEXT TWO LINES ARE SUBTLE, AND CAN EASILY INTRODUCE SECURITY ISSUES
#
# The filename is grabbed from the user. The user could inject arbitrary
# filenames and potentially compromise our system. The second line prevents
# this, since we confirm filename is a valid module_id in the database.
# Small changes to the code or to the database could break this.
#
# We should probably add an explicit check to make sure the filename is in
# the XML file to make this less fragile.
filename=answer.keys()[0].split('_')[0]
s = StudentModule.objects.filter(student=request.user, module_id=filename)
if len(s) == 1:
s=s[0]
problem=capa_problem.LoncapaProblem(settings.DATA_DIR+filename+'.xml',
id=filename,
state=s.state)
js=json.dumps(problem.grade_answers(answer))
s.state=problem.get_state()
s.grade=problem.get_score()['score']
s.save()
else:
raise Exception("Database is inconsistent (3).")
return HttpResponse(js, mimetype="application/json")
module_types={'video':video_module,
'html':html_module,
'tab':tab_module,
......
......@@ -26,7 +26,8 @@ class XModule:
def handle_ajax(self, json):
return
def __init__(self, xml, item_id, ajax_url, track_url, state=None):
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None):
''' In most cases, you must pass state or xml'''
self.xml=xml
self.item_id=item_id
self.ajax_url=ajax_url
......
......@@ -10,7 +10,7 @@ urlpatterns = patterns('',
url(r'^courseware/(?P<course>[^/]*)/(?P<chapter>[^/]*)/$', 'courseware.views.index'),
url(r'^courseware/(?P<course>[^/]*)/$', 'courseware.views.index'),
# url(r'^courseware/modx/(?P<id>[^/]*)/problem_check$', 'courseware.views.check_problem'),
url(r'^courseware/modx/(?P<id>[^/]*)/(?P<dispatch>[^/]*)$', 'courseware.views.modx_dispatch'), #reset_problem'),
url(r'^modx/(?P<module>[^/]*)/(?P<id>[^/]*)/(?P<dispatch>[^/]*)$', 'courseware.views.modx_dispatch'), #reset_problem'),
url(r'^courseware/$', 'courseware.views.index'),
url(r'^profile$', 'courseware.views.profile'),
# url(r'^admin/', include('django.contrib.admin.urls')),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment