diff options
author | Prabhu Ramachandran | 2015-09-04 18:57:57 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2015-09-04 18:57:57 +0530 |
commit | 31f5e743031d105b0406e9587dc33bb065cd6e4d (patch) | |
tree | 653fbd703850e1e12f875bc5422ec3cb809c6e07 /testapp/yaksh_app/management/commands/results2csv.py | |
parent | c9abbadbb0e6a4a60edb7ef2a14d6c74648b0677 (diff) | |
parent | 3ee050fe509b83f36188566ee57b3dd16d94a8a7 (diff) | |
download | online_test-31f5e743031d105b0406e9587dc33bb065cd6e4d.tar.gz online_test-31f5e743031d105b0406e9587dc33bb065cd6e4d.tar.bz2 online_test-31f5e743031d105b0406e9587dc33bb065cd6e4d.zip |
Merge pull request #55 from ankitjavalkar/yaksh
Change app name to yaksh
Diffstat (limited to 'testapp/yaksh_app/management/commands/results2csv.py')
-rw-r--r-- | testapp/yaksh_app/management/commands/results2csv.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/testapp/yaksh_app/management/commands/results2csv.py b/testapp/yaksh_app/management/commands/results2csv.py new file mode 100644 index 0000000..c9b05ce --- /dev/null +++ b/testapp/yaksh_app/management/commands/results2csv.py @@ -0,0 +1,69 @@ +# System library imports. +import sys +from os.path import basename + +# Django imports. +from django.core.management.base import BaseCommand +from django.template import Template, Context + +# Local imports. +from testapp.yaksh_app.models import Quiz, QuestionPaper + +result_template = Template('''\ +"name","username","rollno","email","answered","total","attempts","position",\ +"department","institute" +{% for paper in papers %}\ +"{{ paper.user.get_full_name.title }}",\ +"{{ paper.user.username }}",\ +"{{ paper.profile.roll_number }}",\ +"{{ paper.user.email }}",\ +"{{ paper.get_answered_str }}",\ +{{ paper.get_total_marks }},\ +{{ paper.answers.count }},\ +"{{ paper.profile.position }}",\ +"{{ paper.profile.department }}",\ +"{{ paper.profile.institute }}" +{% endfor %}\ +''') + +def results2csv(filename, stdout): + """Write exam data to a CSV file. It prompts the user to choose the + appropriate quiz. + """ + qs = Quiz.objects.all() + + if len(qs) > 1: + print "Select quiz to save:" + for q in qs: + stdout.write('%d. %s\n'%(q.id, q.description)) + quiz_id = int(raw_input("Please select quiz: ")) + try: + quiz = Quiz.objects.get(id=quiz_id) + except Quiz.DoesNotExist: + stdout.write("Sorry, quiz %d does not exist!\n"%quiz_id) + sys.exit(1) + else: + quiz = qs[0] + + papers = QuestionPaper.objects.filter(quiz=quiz, + user__profile__isnull=False) + stdout.write("Saving results of %s to %s ... "%(quiz.description, + basename(filename))) + # Render the data and write it out. + f = open(filename, 'w') + context = Context({'papers': papers}) + f.write(result_template.render(context)) + f.close() + + stdout.write('Done\n') + +class Command(BaseCommand): + args = '<results.csv>' + help = '''Writes out the results of a quiz to a CSV file. Prompt user + to select appropriate quiz if there are multiple. + ''' + + def handle(self, *args, **options): + """Handle the command.""" + # Save to file. + results2csv(args[0], self.stdout) |