Beispiel #1
0
 async def post(self):
     r_dict = {'code': 0}
     race_cid = self.get_argument('race_cid', '')
     unit_id = None
     try:
         title = self.get_argument('title', None)
         status = self.get_argument('status', None)  # 状态
         code = self.get_argument('code', None)
         if title and code:
             unit_count = await Company.count(
                 dict(record_flag=1, code=code, race_cid=race_cid))
             if unit_count > 0:
                 r_dict['code'] = -3
             else:
                 if status == 'on':
                     status = STATUS_UNIT_MANAGER_ACTIVE
                 else:
                     status = STATUS_UNIT_MANAGER_INACTIVE
                 unit = Company(title=title, code=code)
                 unit.race_cid = race_cid
                 unit.status = status
                 unit.created_dt = datetime.datetime.now()
                 unit.updated_id = self.current_user.oid
                 unit_id = await unit.save()
                 r_dict['code'] = 1
         else:
             if not title:
                 r_dict['code'] = -1
             if not code:
                 r_dict['code'] = -2
     except Exception:
         #  如果因为网络问题等其他问题导致前端展示添加不正确但是数据已经保存到数据库了,应该删除掉
         if unit_id:
             await Company.delete_by_ids([unit_id])
         logger.error(traceback.format_exc())
     return r_dict
Beispiel #2
0
    async def __subject_import_excel(self, race_cid, excel_file_content):
        result_code = 1
        #  所有单位的编号
        unit_code_list = await Company.distinct('code', {
            'race_cid': race_cid,
            'record_flag': 1
        })
        book = xlrd.open_workbook(file_contents=excel_file_content)
        #  获得第一个表的信息
        sheet = book.sheet_by_index(0)
        unit_list = []
        row_list = []

        title_list = []
        for ind, col in enumerate(sheet.row_values(0)):
            if col:
                title_list.append(col)
        # 判断表头是否正确
        if len(title_list) != 2:
            result_code = 2
            return result_code
        # 拿到所有的行数据
        for rownum in range(1, sheet.nrows):
            row_list.append([col for col in sheet.row_values(rownum)])
        #  上传文件中有一些已经存在过的,需要删除,重新覆盖
        delete_unit_oid_list = []
        if row_list:
            for i, row_data in enumerate(row_list):
                code_repeat = False
                # 单位编码
                unit_code = str(self.__get_replace_data(row_data[0], 2))
                #  单位编码不能超过16位
                if not unit_code or len(unit_code) > 16:
                    continue
                else:
                    reg = re.compile(r'^[a-zA-Z0-9]*$')
                    if not bool(reg.match(unit_code)):
                        continue
                    if unit_code in unit_code_list:
                        unit = await Company.find_one(dict(code=unit_code))
                        #  上传文件中有编码重复的行,只添加一个
                        if unit:
                            delete_unit_oid_list.append(unit.oid)
                            await unit.delete()
                        else:
                            code_repeat = True
                    unit = Company()
                    unit.race_cid = race_cid
                    unit.code = unit_code
                    unit.title = str(row_data[1])[:-2] if not isinstance(
                        row_data[1], str) else row_data[1]
                    unit.status = STATUS_UNIT_MANAGER_ACTIVE
                    unit_code_list.append(unit_code)
                if not code_repeat:
                    unit_list.append(unit)
                    if len(unit_list) == 500:
                        await Company.insert_many(unit_list)
                        unit_list = []
            if unit_list:
                await Company.insert_many(unit_list)
                await Company.delete_by_ids(delete_unit_oid_list)
        return result_code