summaryrefslogtreecommitdiff
path: root/exam/management/commands
diff options
context:
space:
mode:
Diffstat (limited to 'exam/management/commands')
-rw-r--r--exam/management/commands/dump_user_data.py50
-rw-r--r--exam/management/commands/results2csv.py24
2 files changed, 48 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):