Esempio n. 1
0
    async def post(self, *args, **kwargs):
        """
        注册科目
        :param args:
        :param kwargs:
        :return:
        """
        r_dict = {'respBody': '', 'errorCode': 200, 'errorMessage': 'success'}
        issuer = self.get_i_argument('issuer')
        category = int(self.get_i_argument('category'))
        title = self.get_i_argument('title')
        max_score = int(self.get_i_argument('maxscore'))
        id_number = self.get_i_argument('idNumber')
        try:

            res, tx_hash = fisco_client.fisco_add_data(
                'createSubject', [id_number, issuer, category, max_score])
            print('tx_hash:', tx_hash)
            subject = Subject()
            subject.title = title
            subject.issuer = issuer
            subject.category = category
            subject.max_score = max_score
            subject.id_number = id_number
            subject.tx_hash = tx_hash
            await subject.save()
            r_dict['respBody'] = res
        except Exception as e:
            print(e)
        return r_dict
Esempio n. 2
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