summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2011-11-21 13:39:00 +0530
committerPrabhu Ramachandran2011-11-21 13:39:00 +0530
commit948010603065f72c83dbbb871154f17da3332b4f (patch)
treedf308b088a6931e33561a5be79413886742f8561
parent49ce8322b9e9cc3104795510173ab484a3e6c593 (diff)
downloadonline_test-948010603065f72c83dbbb871154f17da3332b4f.tar.gz
online_test-948010603065f72c83dbbb871154f17da3332b4f.tar.bz2
online_test-948010603065f72c83dbbb871154f17da3332b4f.zip
ENH: Showing login and IP details of users.
Also cleaned up the dumping code to use templates.
-rw-r--r--exam/management/commands/dump_user_data.py50
-rw-r--r--exam/management/commands/results2csv.py24
-rw-r--r--exam/views.py4
-rw-r--r--templates/exam/user_data.html4
4 files changed, 56 insertions, 26 deletions
diff --git a/exam/management/commands/dump_user_data.py b/exam/management/commands/dump_user_data.py
index 3a3e816..24863eb 100644
--- a/exam/management/commands/dump_user_data.py
+++ b/exam/management/commands/dump_user_data.py
@@ -1,10 +1,40 @@
# Django imports.
from django.core.management.base import BaseCommand
+from django.template import Template, Context
# Local imports.
from exam.views import get_user_data
from exam.models import User
+data_template = Template('''\
+===============================================================================
+Data for {{ user_data.name }} ({{ user_data.username }})
+
+Name: {{ user_data.name }}
+Username: {{ user_data.username }}
+Roll number: {{ user_data.rollno }}
+Email: {{ user_data.email }}
+Date joined: {{ user_data.date_joined }}
+Last login: {{ user_data.last_login }}
+{% for paper in user_data.papers %}
+Paper: {{ paper.name }}
+-----------------------------------------
+Total marks: {{ paper.total }}
+Questions correctly answered: {{ paper.answered }}
+Total attempts at questions: {{ paper.attempts }}
+Start time: {{ paper.start_time }}
+User IP address: {{ paper.user_ip }}
+{% if paper.answers %}
+Answers
+-------
+{% for question, answer in paper.answers.items %}
+Question: {{ question }}
+{{ answer|safe }}
+{% endfor %} \
+{% endif %} {# if paper.answers #} \
+{% endfor %} {# for paper in user_data.papers #}
+''')
+
def dump_user_data(unames, stdout):
'''Dump user data given usernames (a sequence) if none is given dump all
@@ -27,23 +57,9 @@ def dump_user_data(unames, stdout):
for user in users:
data = get_user_data(user.username)
- stdout.write('='*80 + '\n')
- stdout.write('Data for %s (%s)\n'%(data['name'], data['username']))
- stdout.write('Roll Number: %s, email: %s\n'%(data['rollno'], data['email']))
- stdout.write('-'*40 + '\n')
- for paper in data['papers']:
- title = "Paper: %s\n"%paper['name']
- stdout.write(title)
- stdout.write('-'*len(title) + '\n')
- stdout.write('Total marks: %d\n'%paper['total'])
- stdout.write('Questions correctly answered: %s\n'%paper['answered'])
- stdout.write('Attempts: %d\n'%paper['attempts'])
- stdout.write('\nAnswers\n----------\n\n')
- for question, answer in paper['answers'].iteritems():
- stdout.write('Question: %s\n'%question)
- stdout.write(answer)
- stdout.write('\n')
-
+ context = Context({'user_data': data})
+ result = data_template.render(context)
+ stdout.write(result)
class Command(BaseCommand):
args = '<username1> ... <usernamen>'
diff --git a/exam/management/commands/results2csv.py b/exam/management/commands/results2csv.py
index 8d154e4..1adf442 100644
--- a/exam/management/commands/results2csv.py
+++ b/exam/management/commands/results2csv.py
@@ -4,11 +4,21 @@ from os.path import basename
# Django imports.
from django.core.management.base import BaseCommand
+from django.template import Template, Context
# Local imports.
from exam.views import get_quiz_data
from exam.models import Quiz
+result_template = Template('''\
+"name","username","rollno","email","answered","total","attempts"
+{% for paper in paper_list %}\
+"{{ paper.name }}","{{ paper.username }}","{{ paper.rollno }}",\
+"{{ paper.email }}","{{ paper.answered }}",{{ paper.total }},\
+{{ paper.attempts }}
+{% endfor %}\
+''')
+
def results2csv(filename, stdout):
"""Write exam data to a CSV file. It prompts the user to choose the
appropriate quiz.
@@ -31,16 +41,12 @@ def results2csv(filename, stdout):
paper_list = get_quiz_data(quiz.id)
stdout.write("Saving results of %s to %s ... "%(quiz.description,
basename(filename)))
+ # Render the data and write it out.
f = open(filename, 'w')
- fields = ['name', 'username', 'rollno', 'email', 'answered', 'total',
- 'attempts']
- SEP = ','
- f.write(SEP.join(['"%s"'%x for x in fields]) + '\n')
- for paper in paper_list:
- # name, username, rollno, email, answered
- f.write(SEP.join(['"%s"'%(paper[x]) for x in fields[:5]]) + SEP)
- # total, attempts
- f.write(SEP.join(['%d'%(paper[x]) for x in fields[5:]]) + '\n')
+ context = Context({'paper_list': paper_list})
+ f.write(result_template.render(context))
+ f.close()
+
stdout.write('Done\n')
class Command(BaseCommand):
diff --git a/exam/views.py b/exam/views.py
index 7c561a7..7157c45 100644
--- a/exam/views.py
+++ b/exam/views.py
@@ -343,6 +343,8 @@ def get_user_data(username):
data['email'] = user.email
data['rollno'] = profile.roll_number if profile else ''
data['name'] = user.get_full_name()
+ data['date_joined'] = str(user.date_joined)
+ data['last_login'] = str(user.last_login)
papers = []
for q_paper in q_papers:
paper = {}
@@ -353,6 +355,8 @@ def get_user_data(username):
paper['attempts'] = q_paper.answers.count()
total = sum( [marks[int(id)] for id in qa if id] )
paper['total'] = total
+ paper['user_ip'] = q_paper.user_ip
+ paper['start_time'] = str(q_paper.start_time)
answers = {}
for answer in q_paper.answers.all():
qs = answer.question.summary
diff --git a/templates/exam/user_data.html b/templates/exam/user_data.html
index 7560903..a2649fb 100644
--- a/templates/exam/user_data.html
+++ b/templates/exam/user_data.html
@@ -10,6 +10,8 @@
<p> Username: {{ user_data.username }} </p>
<p> Roll number: {{ user_data.rollno }} </p>
<p> Email: {{ user_data.email }} </p>
+<p> Date joined: {{ user_data.date_joined }} </p>
+<p> Last login: {{ user_data.last_login }} </p>
{% if user_data.papers %}
@@ -20,6 +22,8 @@
<p> Answered questions: {{ paper.answered }} </p>
<p> Total attempts at questions: {{ paper.attempts }} </p>
<p> Total marks: {{ paper.total }} </p>
+<p> Start time: {{ paper.start_time }} </p>
+<p> User IP address: {{ paper.user_ip }} </p>
{% if paper.answers %}
<h3> Answers </h3>