diff options
author | Prabhu Ramachandran | 2011-11-21 10:15:50 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2011-11-21 10:15:50 +0530 |
commit | 587fb65d126f5c12fd735dceca4420871239e52d (patch) | |
tree | 61ccf427f3ba8396f911cf0cb9f23cc0eed1e8b8 /exam/management/commands/dump_user_data.py | |
parent | 7de8141a6aa4f7039e8b1eb4f2b1e2db126e902e (diff) | |
download | online_test-587fb65d126f5c12fd735dceca4420871239e52d.tar.gz online_test-587fb65d126f5c12fd735dceca4420871239e52d.tar.bz2 online_test-587fb65d126f5c12fd735dceca4420871239e52d.zip |
ENH: Adding dump_user_data, results2csv commands
Abstracted out the data generation functions in views.py so they may be
reused.
Diffstat (limited to 'exam/management/commands/dump_user_data.py')
-rw-r--r-- | exam/management/commands/dump_user_data.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/exam/management/commands/dump_user_data.py b/exam/management/commands/dump_user_data.py new file mode 100644 index 0000000..3a3e816 --- /dev/null +++ b/exam/management/commands/dump_user_data.py @@ -0,0 +1,58 @@ +# Django imports. +from django.core.management.base import BaseCommand + +# Local imports. +from exam.views import get_user_data +from exam.models import User + + +def dump_user_data(unames, stdout): + '''Dump user data given usernames (a sequence) if none is given dump all + their data. The data is dumped to stdout. + ''' + if not unames: + try: + users = User.objects.all() + except User.DoesNotExist: + pass + else: + users = [] + for uname in unames: + try: + user = User.objects.get(username__exact = uname) + except User.DoesNotExist: + stdout.write('User %s does not exist'%uname) + else: + users.append(user) + + 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') + + +class Command(BaseCommand): + args = '<username1> ... <usernamen>' + help = '''Dumps all user data to stdout, optional usernames can be + specified. If none is specified all user data is dumped. + ''' + + def handle(self, *args, **options): + """Handle the command.""" + # Dump data. + dump_user_data(args, self.stdout) + |