def handle(self, *args, **options): if options['replace']: logger.info('erasing existing questions') Question.objects.all().delete() with open(options['filename']) as f: data = json.load(f) data_batches = self.chunks(data, self.batch_size) logger.info('{} batches of {} ({} total)'.format( len(data_batches), self.batch_size, len(data))) for i, data_batch in enumerate(data_batches): logger.info('loading batch {}...'.format(i + 1)) questions = [] for d in data_batch: try: question = Question( air_date=self.parse_date(d['air_date']), show_number=self.parse_int(d['show_number']), round=d['round'], category=d['category'], question=d['question'][1:-1], answer=d['answer'], amount=self.parse_int(d['value']), ) question.full_clean() questions.append(question) except Exception: logger.error('error parsing {}'.format(d)) raise Question.objects.bulk_create(questions)
def handle(self, *args, **options): if options["replace"]: logger.info("erasing existing questions") Question.objects.all().delete() with open(options["filename"]) as f: data = json.load(f) data_batches = self.chunks(data, self.batch_size) logger.info("{} batches of {} ({} total)".format(len(data_batches), self.batch_size, len(data))) for i, data_batch in enumerate(data_batches): logger.info("loading batch {}...".format(i + 1)) questions = [] for d in data_batch: try: question = Question( air_date=self.parse_date(d["air_date"]), show_number=self.parse_int(d["show_number"]), round=d["round"], category=d["category"], question=d["question"][1:-1], answer=d["answer"], amount=self.parse_int(d["value"]), ) question.full_clean() questions.append(question) except Exception: logger.error("error parsing {}".format(d)) raise Question.objects.bulk_create(questions)
def run(self, game): logger.debug("Filling in questions for game: {}".format(game)) self.delete_questions(game) for question_plugin in self.question_plugins: answer = question_plugin.answer(game) question = Question(description=question_plugin.description, answer=answer, position=question_plugin.position, game=game) question.save() game.questions.add(question) game.save()
def save(self): question = Question( title=self.cleaned_data['title'], text=self.cleaned_data['text'], author=self.custom_user, ) question.save() tags = self.cleaned_data["tags"] for tag in tags.split(","): if tag is not None: Question.objects.add_tag(question=question, tag_name=tag) return question
class TestQuestionModel(UnitTest): def setup(self): super().setup() self.question = Question(description='Test Question', position=10, answer='An answer to the question', game=self.game) def test_init(self): assert self.question.description == 'Test Question' assert self.question.position == 10 assert self.question.answer == 'An answer to the question' assert self.question.game == self.game def test_save(self): assert self.question.id is None self.question.save() assert self.question.id is not None
def question(data_json, subject): from web.models import Question from web.database import db with open(data_json) as file: data = json.load(file) with db.transaction(): if isinstance(data, list): for question in data: proposition_1 = question["propositions"][0] proposition_2 = question["propositions"][1] proposition_3 = question["propositions"][2] proposition_4 = question["propositions"][3] Question.create(subject=subject, content=question["question"], difficulty=question["difficulty"], proposition_1=proposition_1, proposition_2=proposition_2, proposition_3=proposition_3, proposition_4=proposition_4, correct_answer=question["answer"])
def mutate(root, input, context, info): tag_ids = input.get('tags') tag_objects = Tag.objects.filter(id__in=tag_ids) question_id = input.get('question_id') question = Question.get(pk=question_id) question.tags.clear() question.tags.add(*tag_objects) question.save() return EditQuestionTags(question=question, ok=True)
def setup(self): super().setup() self.question = Question(description='Test Question', position=10, answer='An answer to the question', game=self.game)
def mutate(root, args, context, info): question = Question(text=args.get('text'), owner_id=int(args.get('owner_id'))) question.save() ok = True return CreateQuestion(question=question, ok=ok)
def questions(): from web.models import Question from web.database import db if click.confirm('Caution, dropping a table. Continue ?'): with db.atomic(): Question.drop_table()