summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exam/views.py77
-rw-r--r--settings.py7
-rw-r--r--templates/exam/intro.html2
-rw-r--r--templates/exam/login.html4
-rw-r--r--templates/exam/question.html4
-rw-r--r--templates/exam/quit.html2
6 files changed, 61 insertions, 35 deletions
diff --git a/exam/views.py b/exam/views.py
index 52c5664..0bec977 100644
--- a/exam/views.py
+++ b/exam/views.py
@@ -11,10 +11,27 @@ from django.template import RequestContext
from exam.models import Quiz, Question, QuestionPaper, Profile, Answer
from exam.forms import UserRegisterForm, UserLoginForm
from exam.xmlrpc_clients import python_server
+from settings import URL_ROOT
# The directory where user data can be saved.
OUTPUT_DIR = abspath(join(dirname(__file__), pardir, 'output'))
+
+def my_redirect(url):
+ """An overridden redirect to deal with URL_ROOT-ing. See settings.py
+ for details."""
+ return redirect(URL_ROOT + url)
+
+def my_render_to_response(template, context=None, **kwargs):
+ """Overridden render_to_response.
+ """
+ if context is None:
+ context = {'URL_ROOT': URL_ROOT}
+ else:
+ context['URL_ROOT'] = URL_ROOT
+ return render_to_response(template, context, **kwargs)
+
+
def gen_key(no_of_chars):
"""Generate a random key of the number of characters."""
allowed_chars = string.digits+string.uppercase
@@ -29,9 +46,9 @@ def index(request):
"""
user = request.user
if user.is_authenticated():
- return redirect("/exam/start/")
+ return my_redirect("/exam/start/")
- return redirect("/exam/login/")
+ return my_redirect("/exam/login/")
def user_register(request):
""" Register a new user.
@@ -39,7 +56,7 @@ def user_register(request):
user = request.user
if user.is_authenticated():
- return redirect("/exam/start/")
+ return my_redirect("/exam/start/")
if request.method == "POST":
form = UserRegisterForm(request.POST)
@@ -49,38 +66,40 @@ def user_register(request):
new_user = authenticate(username = u_name, password = pwd)
login(request, new_user)
- return redirect("/exam/start/")
+ return my_redirect("/exam/start/")
else:
- return render_to_response('exam/register.html',{'form':form},
+ return my_render_to_response('exam/register.html',
+ {'form':form},
context_instance=RequestContext(request))
else:
form = UserRegisterForm()
- return render_to_response('exam/register.html',{'form':form},
- context_instance=RequestContext(request))
+ return my_render_to_response('exam/register.html',
+ {'form':form},
+ context_instance=RequestContext(request))
def user_login(request):
"""Take the credentials of the user and log the user in."""
user = request.user
if user.is_authenticated():
- return redirect("/exam/start/")
+ return my_redirect("/exam/start/")
if request.method == "POST":
form = UserLoginForm(request.POST)
if form.is_valid():
user = form.cleaned_data
login(request, user)
- return redirect("/exam/start/")
+ return my_redirect("/exam/start/")
else:
- context = {"form": form,}
- return render_to_response('exam/login.html', context,
- context_instance=RequestContext(request))
+ context = {"form": form}
+ return my_render_to_response('exam/login.html', context,
+ context_instance=RequestContext(request))
else:
form = UserLoginForm()
context = {"form": form}
- return render_to_response('exam/login.html', context,
- context_instance=RequestContext(request))
+ return my_render_to_response('exam/login.html', context,
+ context_instance=RequestContext(request))
def start(request):
user = request.user
@@ -120,18 +139,18 @@ def start(request):
# Show the user the intro page.
context = {'user': user}
ci = RequestContext(request)
- return render_to_response('exam/intro.html', context,
- context_instance=ci)
+ return my_render_to_response('exam/intro.html', context,
+ context_instance=ci)
def question(request, q_id):
user = request.user
if not user.is_authenticated():
- return redirect('exam/login/')
+ return my_redirect('/exam/login/')
q = get_object_or_404(Question, pk=q_id)
try:
paper = QuestionPaper.objects.get(user=request.user)
except QuestionPaper.DoesNotExist:
- redirect('/exam/start')
+ my_redirect('/exam/start')
time_left = paper.time_left()
if time_left == 0:
return complete(request, reason='Your time is up!')
@@ -140,8 +159,8 @@ def question(request, q_id):
'quiz_name': quiz_name,
'time_left': time_left}
ci = RequestContext(request)
- return render_to_response('exam/question.html', context,
- context_instance=ci)
+ return my_render_to_response('exam/question.html', context,
+ context_instance=ci)
def show_question(request, q_id):
"""Show a question if possible."""
@@ -154,7 +173,7 @@ def show_question(request, q_id):
def check(request, q_id):
user = request.user
if not user.is_authenticated():
- return redirect('exam/login/')
+ return my_redirect('/exam/login/')
question = get_object_or_404(Question, pk=q_id)
paper = QuestionPaper.objects.get(user=user)
answer = request.POST.get('answer')
@@ -191,15 +210,15 @@ def check(request, q_id):
'quiz_name': paper.quiz.description,
'time_left': time_left}
- return render_to_response('exam/question.html', context,
- context_instance=ci)
+ return my_render_to_response('exam/question.html', context,
+ context_instance=ci)
else:
next_q = paper.answered_question(question.id)
return show_question(request, next_q)
def quit(request):
- return render_to_response('exam/quit.html',
- context_instance=RequestContext(request))
+ return my_render_to_response('exam/quit.html',
+ context_instance=RequestContext(request))
def complete(request, reason=None):
user = request.user
@@ -211,9 +230,9 @@ def complete(request, reason=None):
# Logout the user and quit with the message given.
logout(request)
context = {'message': message}
- return render_to_response('exam/complete.html', context)
+ return my_render_to_response('exam/complete.html', context)
else:
- return redirect('/exam/')
+ return my_redirect('/exam/')
def monitor(request):
"""Monitor the progress of the papers taken so far."""
@@ -243,5 +262,5 @@ def monitor(request):
reverse=True)
context = {'paper_list': paper_list}
- return render_to_response('exam/monitor.html', context,
- context_instance=RequestContext(request))
+ return my_render_to_response('exam/monitor.html', context,
+ context_instance=RequestContext(request))
diff --git a/settings.py b/settings.py
index e5e3e69..655608d 100644
--- a/settings.py
+++ b/settings.py
@@ -12,6 +12,13 @@ SERVER_PORTS = [8001] # range(8001, 8026)
# Timeout for the code to run in seconds. This is an integer!
SERVER_TIMEOUT = 2
+# The root of the URL, for example you might be in the situation where you
+# are not hosted as host.org/exam/ but as host.org/foo/exam/ for whatever
+# reason set this to the root you have to serve at. In the above example
+# host.org/foo/exam set URL_ROOT='/foo'
+URL_ROOT = ''
+
+
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
diff --git a/templates/exam/intro.html b/templates/exam/intro.html
index 75de639..1d3e5de 100644
--- a/templates/exam/intro.html
+++ b/templates/exam/intro.html
@@ -45,7 +45,7 @@ carefully.
<p> We hope you enjoy taking this exam.</p>
-<form action="/exam/start/" method="post" align="center">
+<form action="{{URL_ROOT}}/exam/start/" method="post" align="center">
{% csrf_token %}
<input type="submit" name="start" value="Start Exam!">
</form>
diff --git a/templates/exam/login.html b/templates/exam/login.html
index 8f0ef6c..8e6352e 100644
--- a/templates/exam/login.html
+++ b/templates/exam/login.html
@@ -15,6 +15,6 @@ Please login to proceed.</p>
<input type="submit" value="Login" />
</form>
-<!-- <a href="/exam/forgotpassword/">Forgot Password</a> <br /> -->
-<a href="/exam/register/">New User Registration</a>
+<!-- <a href="{{URL_ROOT}}/exam/forgotpassword/">Forgot Password</a> <br /> -->
+<a href="{{URL_ROOT}}/exam/register/">New User Registration</a>
{% endblock content %} \ No newline at end of file
diff --git a/templates/exam/question.html b/templates/exam/question.html
index 226bdca..dec476e 100644
--- a/templates/exam/question.html
+++ b/templates/exam/question.html
@@ -58,7 +58,7 @@ function update_time()
<p id="status"></p>
-<form id="code" action="/exam/{{ question.id }}/check/" method="post">
+<form id="code" action="{{URL_ROOT}}/exam/{{ question.id }}/check/" method="post">
{% csrf_token %}
<textarea rows="20" cols="100" name="answer">
@@ -78,7 +78,7 @@ you have {{ paper.questions_left }} question(s) left in {{ quiz_name }}.</p>
<p id="time_left"> <strong> Time left: </strong> </p>
<hr/>
-<form id="logout" action="/exam/quit/" method="post">
+<form id="logout" action="{{URL_ROOT}}/exam/quit/" method="post">
{% csrf_token %}
<input type="submit" name="quit" value="Quit exam and logout" />
</form>
diff --git a/templates/exam/quit.html b/templates/exam/quit.html
index a9d3515..37b5c08 100644
--- a/templates/exam/quit.html
+++ b/templates/exam/quit.html
@@ -6,7 +6,7 @@
<p>Your current answers are saved.</p>
<p> Are you sure you wish to quit the exam?</p>
-<form action="/exam/complete/" method="post">
+<form action="{{URL_ROOT}}/exam/complete/" method="post">
{% csrf_token %}
<input type="submit" name="yes" value="Yes!" />
<input type="submit" name="no" value="No!" />