diff options
author | parth | 2012-03-20 01:31:35 +0530 |
---|---|---|
committer | parth | 2012-03-20 01:31:35 +0530 |
commit | 3a0564411258ac332d85ee1f56a5dda17e7bff86 (patch) | |
tree | 9a8e0f7435b04615406ec742f2b86a8e48c7ba16 /allotter | |
parent | f3f59791bb4b272271c851b1ca34b131fae1a9d9 (diff) | |
download | aloha-3a0564411258ac332d85ee1f56a5dda17e7bff86.tar.gz aloha-3a0564411258ac332d85ee1f56a5dda17e7bff86.tar.bz2 aloha-3a0564411258ac332d85ee1f56a5dda17e7bff86.zip |
working loadoptions with command line arguments
Diffstat (limited to 'allotter')
-rw-r--r-- | allotter/management/commands/loadoptions.py | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/allotter/management/commands/loadoptions.py b/allotter/management/commands/loadoptions.py index afa7234..f7c01e6 100644 --- a/allotter/management/commands/loadoptions.py +++ b/allotter/management/commands/loadoptions.py @@ -1,24 +1,20 @@ -import argparse +from optparse import make_option from csv import reader -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import BaseCommand from allotter.models import Exam, Option class Command(BaseCommand): - args = '<file_name...>' - help = "Give the filename of the csv file that has all the option code, name and exam code relation" + option_list = BaseCommand.option_list + ( + make_option('--pcc',metavar='Paper course code file name', type=str), + make_option('--cc',metavar='Course code file name', type=str), + ) + help = "Give the filenames of the csv files that has all the option code, name and exam code relation" def handle(self, *args, **options): clean_option() - parser = argparse.ArgumentParser(description='Process some integers.') - - parser.add_argument('-pcc',metavar='Paper course code file name', type=str) - parser.add_argument('-cc',metavar='Course code file name', type=str) - - args = parser.parse_args() - - load_option(vars(args)['pcc']) + load_option(options) self.stdout.write('Done\n') @@ -28,24 +24,37 @@ def clean_option(): data = Option.objects.all() data.delete() -def load_option(filename): +def load_option(options): """Load option code and option name from the given csv file. The file should declare a list of "exam_code,option_code,option,code". """ + paperCourseFileName=options.get('pcc') + courseCodeFileName=options.get('cc') try: - csvFile = open(filename, 'rb') + paperCourseFile = open(paperCourseFileName, 'rb') + except IOError as (errno,strerror): + print "I/O error({0}): {1}".format(errno, strerror) + + try: + courseCodeFile = open(courseCodeFileName, 'rb') except IOError as (errno,strerror): print "I/O error({0}): {1}".format(errno, strerror) - csvReader = reader(csvFile, delimiter=",") + paperReader = reader(paperCourseFile, delimiter=",") + courseReader = reader(courseCodeFile, delimiter=",") + + courseDict = {} + + for data in courseReader: + courseDict[int(data[0])]=data[1] - for data in csvReader: + for data in paperReader: exam = Exam.objects.get(exam_code=data[0]) for value in data[1:len(data)]: try: new_option = Option.objects.get(opt_code=value) except Option.DoesNotExist: - new_option = Option(opt_name="Test",opt_code=value) + new_option = Option(opt_name=courseDict[int(value)],opt_code=value) new_option.save() new_option.exam.add(exam) - print "Added (option Test with code {0} and exam {1})".format(value,exam) + print "Added (option {0} with code {1} and exam {2})".format(courseDict[int(value)],value,exam)
\ No newline at end of file |