import sys import re import csv import json from django.core.management.base import BaseCommand from university.models import OnlineExamQuestion class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( '--question_tag', required=True, dest='question_tag' ) parser.add_argument( '--file_path', required=True, dest='file_path' ) def handle(self, *args, **options): question_tag = options['question_tag'] file_path = options['file_path'] quesions = [] with open(file_path, 'r') as f: spamreader = csv.reader(f, delimiter=',') for row in spamreader: question_type = 0 id, question, answer, difficulty_level, question_type_str, action = row[:6] # if id in ['12']: # continue action = action.strip() answer = answer.strip() difficulty_level = difficulty_level.strip() question_type_str = question_type_str.strip() if action == '保留': pass elif action == '新增': pass elif action == '': pass elif action == '修改': pass elif action == '删除': continue else: raise Exception('unknown action type, id: {}'.format(id)) if question_type_str == '单选': question_type = 0 elif question_type_str == '多选': question_type = 1 else: raise Exception('unknown question type, id: {}'.format(id)) answer = list(filter(lambda x: x.strip(), list(answer))) difficulty_level = int(difficulty_level) try: problem, options = self.parse_question(question) except Exception as e: raise Exception('{}, id: {}'.format(e, id)) option_value_set = set([o['value'] for o in options]) answer_set = set(answer) if not answer_set.issubset(option_value_set): print(answer_set, options) raise Exception('answer_set is not in option_value_set, id: {}'.format(id)) if difficulty_level < 0 or difficulty_level > 5: raise Exception('wrong difficulty_level, id: {}'.format(id)) if question_type == 0: if len(answer) != 1: raise Exception('wrong answer with question_type, id: {}'.format(id)) else: if len(answer) < 2: raise Exception('wrong answer with question_type, id: {}'.format(id)) quesions.append({ 'question_type': question_type, 'problem': problem, 'body': options, 'answer': answer, 'difficulty_level': difficulty_level, 'comment': id, 'tag': question_tag }) print(json.dumps(quesions)) question_objects = [OnlineExamQuestion( tag=q['tag'], question_type=q['question_type'], problem=q['problem'], body=q['body'], answer=q['answer'], difficulty_level=q['difficulty_level'], comment=q['comment'] ) for q in quesions] OnlineExamQuestion.objects.filter(tag=question_tag).delete() OnlineExamQuestion.objects.bulk_create(question_objects) def parse_question(self, question): problem = '' options = [] lines = list(filter(lambda x: x.strip(), question.split('\n'))) try: problem = lines[0] for index, line in enumerate(lines[1:]): line = line.strip() value, _, text = re.findall(r'^([A-Z]{1}?)(\s|\.|、|。|:){1,2}(.+?)$', line)[0] options.append({ 'value': value, 'text': text }) return problem, options except Exception: print('\033[32m=======================================================\033[0m') print(question) mode = int(input('\033[32mMode: (0 auto, 1 manual):\033[0m') or '0') print('\033[32mProblem:\033[0m') lines = question.split('\n') problem_input = sys.stdin.readlines() if mode == 0: options_start_line = len(problem_input) else: options_start_line = int(input('\033[32mOptions start line:\033[0m')) problem = ''.join(problem_input) print(lines[options_start_line:]) for index, line in enumerate(filter(lambda x: x.strip(), lines[options_start_line:])): line = line.strip() value, _, text = re.findall(r'^([A-Z]{1}?)(\s|\.|、|。|:){1,2}(.+?)$', line)[0] options.append({ 'value': value, 'text': text }) return problem, options