def import_warranty_file(self, request, filename, model_name, admin_class): data = xlrd.open_workbook(filename) table = data.sheet_by_name(model_name) nrows = table.nrows ncols = table.ncols WorkList = [] x = y = z = 0 for i in range(1, nrows): row = table.row_values(i) for j in range(0, ncols): if type(row[j]) == float: row[j] = int(row[j]) if row: if admin_class.model.objects.filter(id=row[0]).exists(): x = x + 1 else: y += 1 WorkList.append( admin_class.model(id=row[0], company=row[1], start_date=row[2], end_date=row[3])) else: z += 1 import_status = {'seccuss': y, 'skip': x, 'error': z} try: admin_class.model.objects.bulk_create(WorkList) except AttributeError as e: savelog.log_info("%s" % request.user, "Error", '导入配置文件失败:%s' % e) return import_status
def import_nic_file(self, request, filename, model_name, admin_class): data = xlrd.open_workbook(filename) table = data.sheet_by_name(model_name) nrows = table.nrows ncols = table.ncols WorkList = [] x = y = z = 0 for i in range(1, nrows): row = table.row_values(i) for j in range(0, ncols): if type(row[j]) == float: row[j] = int(row[j]) if row: company_id = self._company_id(admin_class, row[2]) server_id = self._server_id(admin_class, row[9]) protocol_id = self._protocol_id(admin_class, row[7]) if admin_class.model.objects.filter(ipaddress=row[3]).exists(): x = x + 1 else: y += 1 WorkList.append( admin_class.model(id=row[0], sn=row[1], company_id=company_id, ipaddress=row[3], model=row[4], mac_address=row[5], types=row[6], protocol_id=protocol_id, speed=row[8], server_id=server_id, remarks=row[10])) else: z += 1 import_status = {'seccuss': y, 'skip': x, 'error': z} try: admin_class.model.objects.bulk_create(WorkList) except AttributeError as e: savelog.log_info("%s" % request.user, "Error", '导入配置文件失败:%s' % e) return import_status
def import_servers_file(self, request, filename, model_name, admin_class): data = xlrd.open_workbook(filename) table = data.sheet_by_name(model_name) nrows = table.nrows ncols = table.ncols WorkList = [] PositionList = [] x = y = z = 0 for i in range(1, nrows): row = table.row_values(i) for j in range(0, ncols): if type(row[j]) == float: row[j] = int(row[j]) if row: if admin_class.model.objects.filter(ipaddress=row[7]).exists(): x = x + 1 else: y += 1 try: company_id = self._company_id(admin_class, row[2]) cabint_id = self._cabint_id(admin_class, row[4]) group_id = self._group_id(admin_class, row[12]) warranty_id = self._warranty_id(admin_class, row[13]) vlan_id = self._vlan_id(admin_class, row[8]) cabint_useposition = int( admin_class.model.cabint.get_queryset().filter( id=cabint_id).values( 'useposition')[0]['useposition']) company_height = int( admin_class.model.company.get_queryset().filter( id=company_id).values("height")[0]['height']) PositionList.append({ 'id': cabint_id, 'useposition': cabint_useposition + company_height }) WorkList.append( admin_class.model(id=row[0], sn=row[1], company_id=company_id, types=row[3], cabint_id=cabint_id, position=row[5], hostname=row[6].strip(), ipaddress=row[7], vlan_id=vlan_id, system=str(row[9]), version=row[10], status=row[11], group_id=group_id, warranty_id=warranty_id, userinfo=row[14], contacts=row[15])) except IndexError as e: savelog.log_info("%s" % request.user, 'Error', "%s" % e) pass else: z += 1 import_status = {'seccuss': y, 'skip': x, 'error': z} try: _position = {} admin_class.model.objects.bulk_create(WorkList) for position in PositionList: if position['id'] not in _position: _position[position['id']] = position['useposition'] else: _position[position['id']] += position['useposition'] for key, value in _position.items(): admin_class.model.cabint.get_queryset().filter(id=key).update( useposition=value) except ValueError as e: savelog.log_info("%s" % request.user, "Error", '更新机柜位置错误:%s' % e) except AttributeError as e: savelog.log_info("%s" % request.user, "Error", '导入配置文件失败:%s' % e) return import_status