Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created March 17, 2013 17:09
Show Gist options
  • Save ir4y/5182530 to your computer and use it in GitHub Desktop.
Save ir4y/5182530 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from testing.models import QuestionType, Question, Answer
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option("-n", "--name",
dest="typename",
help=u"Название типа теста",
metavar="TYPENAME"),
make_option("-f", "--file",
dest="filename",
help=u"Файл с вопросами теста в формате csv (\"вопрос;вариант_ответа1,вариант_ответа2\")",
metavar="FILENAME"),
make_option("-d", "--default_answers",
dest="answers",
help=u"Варианты ответов по умолчанию через ;(\"Вариант_ответа1,вариант_ответа2\")",
metavar="ANSWERS"),
)
def handle(self, *args, **options):
typename = options['typename']
if not typename:
raise CommandError('You must setup test ype name')
filename = options['filename']
if not filename:
raise CommandError('You must choose filename')
if options['answers']:
default_answers_list = options['answers'].split(";")
else:
default_answers_list = []
question_type,status = QuestionType.objects.get_or_create(name=typename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment