Exemple #1
0
    async def __subject_import_excel(self, excel_file_content):
        result_code = 1
        custom_code_list = await Subject.distinct('custom_code', {'record_flag': 1})
        book = xlrd.open_workbook(file_contents=excel_file_content)
        sheet = book.sheet_by_index(0)
        subject_list = []
        subject_option_list = []
        row_list = []
        k_first_dict = dict(zip(KNOWLEDGE_FIRST_LEVEL_DICT.values(), KNOWLEDGE_FIRST_LEVEL_DICT.keys()))
        k_second_dict = dict(zip(KNOWLEDGE_SECOND_LEVEL_DICT.values(), KNOWLEDGE_SECOND_LEVEL_DICT.keys()))

        # 维度信息
        field_dict = {'record_flag': 1, 'status': STATUS_SUBJECT_DIMENSION_ACTIVE, 'parent_cid': {'$in': ['', None]}}
        all_dimension_list = await SubjectDimension.find(field_dict).to_list(None)
        sub_dict = dict(record_flag=1, status=STATUS_SUBJECT_DIMENSION_ACTIVE)
        subject_dimension_dict = {}
        for subject_dimension in all_dimension_list:
            sub_dict['parent_cid'] = subject_dimension.cid
            sub_dimension_list = await SubjectDimension.find(sub_dict).to_list(None)
            if sub_dimension_list:
                subject_dimension_dict[subject_dimension.cid] = sub_dimension_list

        # 按顺序得到表头对应的维度cid(如果题目表头除维度之外有增减,3就随之改变)
        cid_list = []
        title_list = []
        is_empty = 0
        for ind, col in enumerate(sheet.row_values(0)):
            if col:
                title_list.append(col)
            if 3 < ind < (len(all_dimension_list) + 4):
                if not col:
                    is_empty = 1
                flag = 1
                for dimension in all_dimension_list:
                    if dimension.title == str(self.__get_replace_data(col, 2)):
                        flag = 2
                        cid_list.append(dimension.cid)
                        break
                if flag == 1:
                    continue
            if (ind < len(cid_list) + 8) and not col:
                is_empty = 1

        # 判断表头是否正确(如果题目表头除维度之外有增减,8就随之改变)
        if not (len(cid_list) + 8) == len(title_list) or len(cid_list) < 1 or is_empty:
            result_code = 2
            return result_code

        for rownum in range(1, sheet.nrows):
            row_list.append([col for col in sheet.row_values(rownum)])

        if row_list:
            for i, row_data in enumerate(row_list):
                is_exist = 0

                # 题目ID
                custom_code = str(self.__get_replace_data(row_data[1], 2))
                if not custom_code or len(custom_code) > 20 or len(custom_code) < 5:
                    continue
                else:
                    reg = re.compile(r'^[a-zA-Z0-9]*$')
                    if not bool(reg.match(custom_code)):
                        continue
                    if custom_code not in custom_code_list:
                        subject = Subject()
                        subject_code = str(get_increase_code(KEY_INCREASE_SUBJECT_CODE))
                        subject.code = subject_code
                        subject.custom_code = custom_code
                        subject.status = STATUS_SUBJECT_ACTIVE
                        custom_code_list.append(custom_code)
                    else:
                        subject = await Subject.find_one(dict(custom_code=custom_code))
                        # await SubjectOption.delete_many({'subject_cid': subject.cid})
                        subject.updated_dt = datetime.datetime.now()
                        is_exist = 1

                # 一级知识点
                subject.knowledge_first = self.__get_replace_data(row_data[2], 1, data_dict=k_first_dict)

                # 二级知识点
                subject.knowledge_second = self.__get_replace_data(row_data[3], 1, data_dict=k_second_dict)

                # 知识维度
                dimension_dict = {}
                is_wrong = 0
                end_dimension_len = 4 + len(cid_list)
                for index in range(4, end_dimension_len):
                    c_code = self.__get_replace_data(row_data[index], 2)
                    cid = cid_list[index - 4]
                    if cid and c_code:
                        sub_list = subject_dimension_dict.get(cid)
                        if sub_list:
                            is_wrong = 0
                            for sub_dimension in sub_list:
                                if sub_dimension.code == c_code:
                                    is_wrong = 1
                                    dimension_dict[cid] = sub_dimension.cid
                                    break
                            if not is_wrong:
                                result_code = 3
                                is_wrong = 0
                                break
                if dimension_dict and is_wrong:
                    subject.dimension_dict = dimension_dict
                else:
                    continue

                # 答案解析
                resolving = self.__get_replace_data(row_data[end_dimension_len], 2)
                if not resolving:
                    continue
                else:
                    subject.resolving = resolving

                # 题目
                title = self.__get_replace_data(row_data[end_dimension_len + 1], 2)
                if not title:
                    continue
                else:
                    subject.title = title

                # 答案
                count = 0
                if not row_data[end_dimension_len + 2]:
                    continue
                else:
                    if not row_data[end_dimension_len + 2].strip().isalpha() or len(
                            row_data[end_dimension_len + 2].strip()) > 1:
                        continue
                    count = ord(row_data[end_dimension_len + 2].strip().upper()) - 64

                if (count + end_dimension_len + 2) > len(row_data):
                    continue
                else:
                    if not row_data[count + end_dimension_len + 2]:
                        continue
                # 选项
                num = 0
                tmp_option_list = await SubjectOption.find({'subject_cid': subject.cid}).sort([('sort', ASC)]).to_list(
                    None)
                for index in range(end_dimension_len + 3, len(row_data)):
                    if row_data[index]:
                        option_title = self.__get_replace_data(row_data[index], 2)
                        if not option_title:
                            continue
                        else:
                            sort = index - (end_dimension_len + 2)
                            a_index = sort - 1
                            update, subject_option = False, None
                            if tmp_option_list:
                                if len(tmp_option_list) > a_index:
                                    subject_option = tmp_option_list[a_index]
                                    update = True
                            if subject_option is None:
                                subject_option = SubjectOption(
                                    code=str(get_increase_code(KEY_INCREASE_SUBJECT_OPTION_CODE)))

                            if isinstance(option_title, (float, int)):
                                option_title = str(option_title)

                            subject_option.title = option_title
                            subject_option.sort = sort
                            if sort == count:
                                subject_option.correct = True
                            else:
                                subject_option.correct = False
                            subject_option.subject_code = subject.code
                            subject_option.subject_cid = subject.cid
                            if update:
                                subject_option.updated_dt = datetime.datetime.now()
                                subject_option.updated_id = self.current_user.oid
                                subject_option.needless = {}
                                await subject_option.save()
                            else:
                                subject_option_list.append(subject_option)
                            num += 1
                subject.needless = {'option_quantity': num}
                if is_exist:
                    await subject.save()
                else:
                    subject_list.append(subject)
                    if len(subject_list) == 500:
                        await  Subject.insert_many(subject_list)
                        subject_list = []
                    if len(subject_option_list) == 500:
                        await SubjectOption.insert_many(subject_option_list)
                        subject_option_list = []
            if subject_list:
                await Subject.insert_many(subject_list)
            if subject_option_list:
                await SubjectOption.insert_many(subject_option_list)
        return result_code
Exemple #2
0
    async def post(self):
        r_dict = {'code': 0}
        subject_id = None
        try:
            title = self.get_argument('title', None)
            option_str = self.get_argument('option_list', None)
            option_list = json.loads(option_str) if option_str else []
            # difficulty = self.get_argument('difficulty', None)
            # category = self.get_argument('category', None)
            content = self.get_argument('content', None)
            status = self.get_argument('status', None)  # 状态
            knowledge_first = self.get_argument('knowledge_first', None)
            knowledge_second = self.get_argument('knowledge_second', None)
            category_use = self.get_argument('category_use', None)
            resolving = self.get_argument('resolving', None)
            subject_dimension_list = self.get_arguments('subject_dimension')
            custom_code = self.get_argument('custom_code', None)
            if title and len(option_list) >= 2 and resolving and custom_code:
                c_count = await Subject.count(dict(custom_code=custom_code))
                if c_count > 0:
                    r_dict['code'] = -10
                else:
                    if status == 'on':
                        status = STATUS_SUBJECT_ACTIVE
                    else:
                        status = STATUS_SUBJECT_INACTIVE
                    image_cid = None
                    image_cid_list = await save_upload_file(self, 'image', category=CATEGORY_UPLOAD_FILE_IMG_SUBJECT)
                    if image_cid_list:
                        image_cid = image_cid_list[0]

                    code = get_increase_code(KEY_INCREASE_SUBJECT_CODE)
                    # subject = Subject(code=code, title=title, difficulty=int(difficulty), category=int(category))
                    subject = Subject(code=code, title=title)
                    subject.custom_code = custom_code
                    subject.image_cid = image_cid
                    subject.status = status
                    subject.content = content
                    subject.created_id = self.current_user.oid
                    subject.updated_id = self.current_user.oid
                    subject.resolving = resolving
                    knowledge_first = int(knowledge_first) if knowledge_first else None
                    knowledge_second = int(knowledge_second) if knowledge_second else None
                    subject.knowledge_first = knowledge_first
                    subject.knowledge_second = knowledge_second
                    subject.category_use = int(category_use) if category_use else None
                    subject_dimension_dict = {}
                    if subject_dimension_list:
                        for subject_dimension in subject_dimension_list:
                            try:
                                subject_dimension_cid, sub_subject_dimension_cid = subject_dimension.split('_')
                                subject_dimension_dict[subject_dimension_cid] = sub_subject_dimension_cid
                            except ValueError as e:
                                if subject_dimension != 'DFKX':
                                    raise e

                    subject.dimension_dict = subject_dimension_dict
                    # 冗余信息
                    if not isinstance(subject.needless, dict):
                        subject.needless = {}
                    subject.needless['option_quantity'] = len(option_list)

                    if option_list:
                        subject_id = await subject.save()
                        for index, option_dict in enumerate(option_list):
                            if option_dict:
                                so = SubjectOption(subject_cid=subject.cid, code=str(index + 1),
                                                   title=option_dict.get('content'),
                                                   correct=option_dict.get('is_correct'))
                                so.sort = str(index + 1)
                                so.created_id = self.current_user.oid
                                so.updated_id = self.current_user.oid
                                await so.save()

                        await set_subject_choice_rules_redis_value(1)
                        r_dict['code'] = 1
            else:
                if not title:
                    r_dict['code'] = -1
                # if not difficulty:
                #     r_dict['code'] = -4
                # if not category:
                #     r_dict['code'] = -5
                if not resolving:
                    r_dict['code'] = -8
                if not option_list and len(option_list) < 2:
                    r_dict['code'] = -6
                if not custom_code:
                    r_dict['code'] = -9
                if not category_use:
                    r_dict['code'] = -11
        except Exception as e:
            if subject_id:
                subject = await Subject.get_by_id(subject_id)
                if subject:
                    await Subject.delete_by_ids([subject.oid])
                    await Subject.delete_many({'code': subject.code})
            logger.error(traceback.format_exc())
        return r_dict