def ftp_report_business_unit(report_status, xml_file, tmp_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "2" if int(report_status) == 1 else "4" report_time = get_cur_time() # 更新客户信息状态 CustomerInfo.objects.filter(id=tmp_id).update( report_status=tmp_status, report_time=report_time) # 更新服务信息状态 AppServiceInfo.objects.filter(customer_id_id=tmp_id).update( report_status=tmp_status, report_time=report_time) # 更新域名信息状态 service_info = AppServiceInfo.objects.filter(customer_id_id=tmp_id) for one in service_info: DomainInfo.objects.filter(app_service_id_id=one.id).update( report_status=tmp_status, report_time=report_time) return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_report_business_unit(report_status, xml_file, tmp_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "2" if int(report_status) == 1 else "4" report_time = get_cur_time() # 更新客户信息状态,如果客户是新增未上报时,其它不用更新 customer_id_id = AppServiceInfo.objects.get( id=tmp_id).customer_id_id customer_report_status = CustomerInfo.objects.get( id=customer_id_id).report_status if int(customer_report_status) == 1: CustomerInfo.objects.filter(id=customer_id_id).update( report_status="2", report_time=report_time) # 更新服务信息状态 AppServiceInfo.objects.filter(id=tmp_id).update( report_status=tmp_status, report_time=report_time) # 更新域名信息状态,当服务信息是新增上报的时候。 if int(report_status) == 1: DomainInfo.objects.filter(app_service_id_id=tmp_id).update( report_status=tmp_status, report_time=report_time) return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_report_mount_node(report_status, xml_file, cdn_node_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "2" report_time = get_cur_time() # 如果子网的状态为新增未上报,更新子网上报状态 cdn_net_id = MountCdnNode.objects.get( cdn_node_id=cdn_node_id).cdn_net_id net_report_status = CdnNetInfo.objects.get( cdn_net_id=cdn_net_id).report_status if int(net_report_status) == 1: CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).update( report_status=tmp_status, report_time=report_time) if int(report_status) == 1: # 更新挂载的节点上报状态 MountCdnNode.objects.filter(cdn_node_id=cdn_node_id).update( report_status=tmp_status, report_time=report_time) # 更新子网下的节点下的机房上报状态 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, cdn_node_id=cdn_node_id).update( report_status=tmp_status, report_time=report_time) # 判断此节点是否已经上报过,如果没有上报过,修改其状态为新增已上报 node_status = CdnNodeInfo.objects.get( cdn_node_id=cdn_node_id).report_status if int(node_status) == 1: CdnNodeInfo.objects.filter(cdn_node_id=cdn_node_id).update( report_status=tmp_status, report_time=report_time) cdn_house = MountCdnHouse.objects.filter( cdn_net_id=cdn_net_id, cdn_node_id=cdn_node_id) for one in cdn_house: # 判断此节点下的机房是否已经上报过,如果没有上报过,修改其状态为新增已上报 house_status = CdnNodeHouse.objects.get( cdn_house_id=one.cdn_house_id).report_status if int(house_status) == 1: CdnNodeHouse.objects.filter( cdn_house_id=one.cdn_house_id).update( report_status=tmp_status, report_time=report_time) return 0 except: print(traceback.print_exc()) return 3 else: return 2
def logout_business_unit(request): try: tmp_id = request.POST.get("id") cdnId = BusinessUnit.objects.get(id=tmp_id).unit_licence doc = Document() xml_obj = XML() basicInfo = xml_obj.create_child_node(doc, doc, 'basicInfo') deleteInfo = xml_obj.create_child_node(doc, basicInfo, 'deleteInfo') xml_file = local_xml_path + "/basic_delete_business_unit.xml" xsd_file = local_xsd_path + "/basic_delete_business_unit.xsd" xml_obj.add_value_to_node(doc, deleteInfo, 'cdnId', cdnId) xml_obj.add_value_to_node(doc, deleteInfo, 'timeStamp', get_cur_time()) xml_obj.add_value_to_node(doc, basicInfo, 'timeStamp', get_cur_time()) print("xml_file:", xml_file) # 写入xml文件 with open(xml_file, "w") as f: doc.writexml(f, addindent=' ' * 4, newl='\n', encoding="UTF-8") # 利用xsd校验xml文件 ret = check_xml_file(xsd_file, xml_file) if ret != 1: print("%s check Fail!!!" % xml_file) return return_msg(1) # FTP上报xml文件到管局 return_code = ftp_logout_business_unit(xml_file, tmp_id) # 错误处理 return return_msg(return_code) except: print(traceback.print_exc()) return return_msg(4)
def save_mount_cdn_domain(request): back_dict = {"code": "0", "msg": "success"} back_dict_err = {"code": "1", "msg": "error"} try: print(request.POST) cdn_net_id = request.POST.get("cdn_net_id").strip() cdn_domain_id_list_str = request.POST.get("cdn_domain_id_list") cdn_domain_id_list = json.loads(cdn_domain_id_list_str) cdn_domain_id_list = list(filter(lambda x : x, cdn_domain_id_list)) # 获取先前挂载的域名 before_mount_domain = MountCdnDomain.objects.filter(cdn_net_id=cdn_net_id) before_mount_domain_id_list = [one.cdn_domain_id for one in before_mount_domain] # 找出新添加的节点信息 new_mount_domain = list(set(cdn_domain_id_list).difference(set(before_mount_domain_id_list))) # 新去掉的 new_mount_domain_bak = list(set(before_mount_domain_id_list).difference(set(cdn_domain_id_list))) if not new_mount_domain: # 没有新添加的,检查是否有删除的(并且是否是未上报的) if new_mount_domain_bak: for one in new_mount_domain_bak: status_tmp = MountCdnDomain.objects.get(cdn_domain_id=one).report_status if int(status_tmp) == 1: MountCdnDomain.objects.filter(cdn_domain_id=one).delete() return HttpResponse(json.dumps(back_dict)) cur_time = get_cur_time() for one_domain_id in new_mount_domain: MountCdnDomain.objects.create( cdn_net_id = cdn_net_id, cdn_domain_id = one_domain_id, report_status = "1", report_time = "--", create_time = cur_time ) # 修改子网的状态为更新未上报 net_status = CdnNetInfo.objects.get(cdn_net_id=cdn_net_id).report_status if int(net_status) in [2, 4]: CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).update(report_status="3") except: print(traceback.print_exc()) return HttpResponse(json.dumps(back_dict_err)) return HttpResponse(json.dumps(back_dict))
def ftp_report_business_unit(report_status, xml_file, tmp_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "2" if int(report_status) == 1 else "4" report_time = get_cur_time() BusinessUnit.objects.filter(id=tmp_id).update(status=tmp_status, time=report_time) return 0 except: return 3 else: return 2
def ftp_report_cdn_node(report_status, xml_file, cdn_node_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "4" report_time = get_cur_time() CdnNodeInfo.objects.filter(cdn_node_id=cdn_node_id).update( report_status=tmp_status, report_time=report_time) return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_logout_customer_info(xml_file, tmp_id, flag): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: if flag == "customer": report_time = get_cur_time() CustomerInfo.objects.filter(id=tmp_id).update( report_status="5", report_time=report_time) # 删除客户对应的服务信息 customer_info = CustomerInfo.objects.get(id=tmp_id) app_service_info = AppServiceInfo.objects.filter( customer_id_id=customer_info.id) for app in app_service_info: # 删除域名信息 DomainInfo.objects.filter( app_service_id_id=app.id).delete() # 删除服务信息 AppServiceInfo.objects.filter( customer_id_id=customer_info.id).delete() # 删除客户信息 CustomerInfo.objects.filter(id=tmp_id).delete() elif flag == "service": app_service_info = AppServiceInfo.objects.get(id=tmp_id) DomainInfo.objects.filter( app_service_id_id=app_service_info.id).delete() AppServiceInfo.objects.filter(id=tmp_id).delete() else: DomainInfo.objects.filter(id=tmp_id).delete() return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_logout_customer_info(xml_file, flag, cdn_net_id, cdn_node_id, cdn_house_id, cdn_domain_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: if flag == "net": report_time = get_cur_time() CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).update(report_status="5", report_time=report_time) # 删除子网下挂载的节点 MountCdnNode.objects.filter(cdn_net_id=cdn_net_id).delete() # 删除子网下挂载的机房 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id).delete() # 删除子网本身 CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).delete() elif flag == "node": # 删除子网下挂载的节点 MountCdnNode.objects.filter(cdn_node_id=cdn_node_id, cdn_net_id=cdn_net_id).delete() # 删除子网下挂载的节点下的机房 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, cdn_node_id=cdn_node_id).delete() elif flag == "house": # 删除子网下挂载的节点下的机房 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, cdn_node_id=cdn_node_id, cdn_house_id=cdn_house_id).delete() elif flag == "domain": MountCdnDomain.objects.filter(cdn_domain_id=cdn_domain_id).delete() else: print("flag error!") pass return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_logout_business_unit(xml_file, tmp_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: report_time = get_cur_time() BusinessUnit.objects.filter(id=tmp_id).update(status="5", time=report_time) # 暂时先别删除,应该过几天之后删除。以防管局没有注销成功 # 子网和客户信息都进行删除 business_unit = BusinessUnit.objects.get(id=tmp_id) customer_info = CustomerInfo.objects.filter( business_unit=business_unit.id) for one in customer_info: app_service_info = AppServiceInfo.objects.filter( customer_id_id=one.id) for app in app_service_info: # 删除域名信息 DomainInfo.objects.filter( app_service_id_id=app.id).delete() # 删除服务信息 AppServiceInfo.objects.filter(customer_id_id=one.id).delete() # 删除客户信息 CustomerInfo.objects.filter( business_unit=business_unit.id).delete() # 删除子网信息 CdnNetInfo.objects.filter().delete() # 删除经营者单位信息 BusinessUnit.objects.filter(id=tmp_id).delete() return 0 except: print(traceback.print_exc()) return 3 else: return 2
def ftp_report_cdn_net(report_status, xml_file, cdn_net_id): # 根据上报的类型获取上报文件的目录 report_type = 7 # ret = send_file_to_ftp(report_type, xml_file) ret = True # 上报成功后,更改数据的上报状态,失败后直接返回失败提示 if ret is True: try: tmp_status = "2" if int(report_status) == 1 else "4" report_time = get_cur_time() # 更新子网上报状态 CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).update(report_status=tmp_status, report_time=report_time) if int(report_status) in [1, 3]: # 更新挂载的节点上报状态 MountCdnNode.objects.filter(cdn_net_id=cdn_net_id, report_status="1").update(report_status="2", report_time=report_time) # 更新子网下的节点下的机房上报状态 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, report_status="1").update(report_status="2", report_time=report_time) # 更新挂载的域名上报状态 MountCdnDomain.objects.filter(cdn_net_id=cdn_net_id, report_status="1").update(report_status="2", report_time=report_time) cdn_node = MountCdnNode.objects.filter(cdn_net_id=cdn_net_id) # 更新节点自己也为已上报 for one in cdn_node: node_status = CdnNodeInfo.objects.get(cdn_node_id=one.cdn_node_id).report_status if int(node_status) == 1: CdnNodeInfo.objects.filter(cdn_node_id=one.cdn_node_id).update(report_status="2", report_time=report_time) # 更新机房自己也为已上报 cdn_house = MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, cdn_node_id=one.cdn_node_id) for h in cdn_house: CdnNodeHouse.objects.filter(cdn_house_id=h.cdn_house_id, report_status="1").update(report_status="2", report_time=report_time) return 0 except: print(traceback.print_exc()) return 3 else: return 2
def logout_customer_info(request): try: tmp_id = request.POST.get("id") # flag 表示注销客户信息,服务信息,域名信息中的那一个 flag = request.POST.get("flag") if flag == "customer": customer_info = CustomerInfo.objects.get(id=tmp_id) cdnId = BusinessUnit.objects.get( id=customer_info.business_unit).unit_licence xml_file = local_xml_path + "/basic_delete_customer_info.xml" xsd_file = local_xsd_path + "/basic_delete_customer_info.xsd" elif flag == "service": app_service_info = AppServiceInfo.objects.get(id=tmp_id) customer_info = CustomerInfo.objects.get( id=app_service_info.customer_id_id) xml_file = local_xml_path + "/basic_delete_app_service_info.xml" xsd_file = local_xsd_path + "/basic_delete_app_service_info.xsd" elif flag == "domain": domain_info = DomainInfo.objects.get(id=tmp_id) app_service_info = AppServiceInfo.objects.get( id=domain_info.app_service_id_id) customer_info = CustomerInfo.objects.get( id=app_service_info.customer_id_id) xml_file = local_xml_path + "/basic_delete_domain_info.xml" xsd_file = local_xsd_path + "/basic_delete_domain_info.xsd" else: return return_msg(4) cdnId = BusinessUnit.objects.get( id=customer_info.business_unit).unit_licence doc = Document() xml_obj = XML() basicInfo = xml_obj.create_child_node(doc, doc, 'basicInfo') deleteInfo = xml_obj.create_child_node(doc, basicInfo, 'deleteInfo') xml_obj.add_value_to_node(doc, deleteInfo, 'cdnId', cdnId) customerDeleteList = xml_obj.create_child_node(doc, deleteInfo, 'customerDeleteList') xml_obj.add_value_to_node(doc, customerDeleteList, 'customerId', customer_info.customer_id) # 注销服务或域名信息 if flag != "customer": serviceDeleteList = xml_obj.create_child_node( doc, customerDeleteList, 'serviceDeleteList') xml_obj.add_value_to_node(doc, serviceDeleteList, 'serviceId', app_service_info.service_id) # 注销域名信息 if flag == "domain": xml_obj.add_value_to_node(doc, serviceDeleteList, 'domainId', domain_info.domain_id) for c in json.loads(app_service_info.service_content): xml_obj.add_value_to_node(doc, serviceDeleteList, 'serviceContent', c) xml_obj.add_value_to_node(doc, deleteInfo, 'timeStamp', get_cur_time()) xml_obj.add_value_to_node(doc, basicInfo, 'timeStamp', get_cur_time()) print("xml_file:", xml_file) # 写入xml文件 with open(xml_file, "w") as f: doc.writexml(f, addindent=' ' * 4, newl='\n', encoding="UTF-8") # 利用xsd校验xml文件 ret = check_xml_file(xsd_file, xml_file) if ret != 1: print("%s check Fail!!!" % xml_file) return return_msg(1) # FTP上报xml文件到管局 return_code = ftp_logout_customer_info(xml_file, tmp_id, flag) # 错误处理 return return_msg(return_code) except: print(traceback.print_exc()) return return_msg(4)
def add_command(request): back_dict = {"code": 0, "msg": "success"} back_dict_err = {"code": 1, "msg": "error"} def _get(): try: # 管理指令类型下拉 command_type_select = get_table_data(CommandType) # 操作类型下拉 operate_type_select = get_table_data(CommandOperateType) # 规则类型下拉 rule_type_select = get_table_data(RuleType) # 机房列表 house_list = CdnNodeHouse.objects.all() for one in house_list: # 为了和选中的机房中的id对应,那里是字符串类型,不然页面的勾选不能回显。 one.id = str(one.id) if request.GET.get('id'): # 表示获取要编辑的数据 edit_map = Command.objects.get(id=request.GET['id']) edit_map.command_type_name = get_dict_name( edit_map.command_type, CommandType) edit_map.operate_type_name = get_dict_name( edit_map.operate_type, CommandOperateType) edit_map.rule_type_name = get_dict_name( edit_map.rule_type, RuleType) edit_map.house = json.loads(edit_map.house) edit_map.keyword = json.loads(edit_map.keyword) # 从下拉框中过滤掉要编辑的数据 command_type_select = del_dict_one_item( edit_map.command_type, command_type_select) operate_type_select = del_dict_one_item( edit_map.operate_type, operate_type_select) rule_type_select = del_dict_one_item(edit_map.rule_type, rule_type_select) except: print(traceback.print_exc()) return render_to_response('command_edit.html', locals()) if request.method == "GET": return _get() print("#####################################") print(request.POST) print("#####################################") tmp_id = request.POST.get("id") command_id = request.POST.get("command_id") print("command_id", command_id, type(command_id)) command_type = request.POST.get("command_type") operate_type = request.POST.get("operate_type") effect_time = request.POST.get("effect_time") deadline = request.POST.get("deadline") house = request.POST.getlist("house", []) if request.POST.get("is_write_log", ""): is_write_log = "1" # 写日志 else: is_write_log = "0" # 不写日志 if request.POST.get("is_report", ""): is_report = "1" # 上报 else: is_report = "0" # 不上报 filter_reason = request.POST.get("filter_reason") rule_type = request.POST.get("rule_type") rule_start_value = request.POST.get("rule_start_value") rule_end_value = request.POST.get("rule_end_value") keyword = request.POST.getlist("keyword", []) # 获取登录账号名 unit_licence = BusinessUnit.objects.all()[0].unit_licence send_user = "******" create_time = get_cur_time() command_origin = "企侧" # 指令优先级(2048-4095)通过不同的规则类型进行划分 priority = 3088 def _update(): try: Command.objects.filter(id=tmp_id).update( command_type=command_type, operate_type=operate_type, effect_time=effect_time, send_user=send_user, deadline=deadline, unit_licence=unit_licence, house=json.dumps(house), priority=priority, command_origin=command_origin, is_write_log=is_write_log, is_report=is_report, filter_reason=filter_reason, rule_type=rule_type, rule_start_value=rule_start_value, rule_end_value=rule_end_value, keyword=json.dumps(keyword), ) except: print(traceback.print_exc()) return HttpResponse(json.dumps(back_dict_err)) return HttpResponseRedirect('/command_list') def _add(): try: Command.objects.create( command_id=int(command_id), command_type=command_type, operate_type=operate_type, effect_time=effect_time, send_user=send_user, deadline=deadline, unit_licence=unit_licence, house=json.dumps(house), priority=priority, command_origin=command_origin, is_write_log=is_write_log, is_report=is_report, filter_reason=filter_reason, rule_type=rule_type, rule_start_value=rule_start_value, rule_end_value=rule_end_value, keyword=keyword, create_time=create_time, ) except: print(traceback.print_exc()) return HttpResponse(json.dumps(back_dict_err)) return HttpResponseRedirect('/command_list') if request.method == "POST" and request.POST['id']: return _update() return _add()
def logout_cdn_net(request): try: print("############################") print(request.POST) cdn_net_id = request.POST.get("cdn_net_id", "").strip() cdn_node_id = request.POST.get("cdn_node_id", "").strip() cdn_house_id = request.POST.get("cdn_house_id", "").strip() cdn_domain_id = request.POST.get("cdn_domain_id", "").strip() # flag 表示注销子网信息,节点信息,机房,域名信息中的那一个 flag = request.POST.get("flag") if flag == "net": xml_file = local_xml_path + "/basic_delete_cdn_net_info.xml" xsd_file = local_xsd_path + "/basic_delete_cdn_net_info.xsd" elif flag == "node": node_info = CdnNodeInfo.objects.get(cdn_node_id=cdn_node_id) xml_file = local_xml_path + "/basic_delete_node_info.xml" xsd_file = local_xsd_path + "/basic_delete_node_info.xsd" elif flag == "house": xml_file = local_xml_path + "/basic_delete_house_info.xml" xsd_file = local_xsd_path + "/basic_delete_house_info.xsd" elif flag == "domain": xml_file = local_xml_path + "/basic_delete_speed_domain_info.xml" xsd_file = local_xsd_path + "/basic_delete_speed_domain_info.xsd" else: return return_msg(4) cdnId = BusinessUnit.objects.all()[0].unit_licence doc = Document() xml_obj = XML() basicInfo = xml_obj.create_child_node(doc, doc, 'basicInfo') deleteInfo = xml_obj.create_child_node(doc, basicInfo, 'deleteInfo') xml_obj.add_value_to_node(doc, deleteInfo, 'cdnId', cdnId) cdnnetDeleteList = xml_obj.create_child_node(doc, deleteInfo, 'cdnnetDeleteList') xml_obj.add_value_to_node(doc, cdnnetDeleteList, 'cdnNetId', cdn_net_id) # 注销服务或域名信息 if flag == "net": pass elif flag == "node": nodeDeleteList = xml_obj.create_child_node(doc, cdnnetDeleteList, 'nodeDeleteList') xml_obj.add_value_to_node(doc, nodeDeleteList, 'nodeId', cdn_node_id) elif flag == "house": nodeDeleteList = xml_obj.create_child_node(doc, cdnnetDeleteList, 'nodeDeleteList') xml_obj.add_value_to_node(doc, nodeDeleteList, 'nodeId', cdn_node_id) houseDeleteList = xml_obj.create_child_node(doc, nodeDeleteList, 'nodeDeleteList') xml_obj.add_value_to_node(doc, houseDeleteList, 'houseId', cdn_house_id) elif flag == "domain": xml_obj.add_value_to_node(doc, cdnnetDeleteList, 'domainId', cdn_domain_id) xml_obj.add_value_to_node(doc, deleteInfo, 'timeStamp', get_cur_time()) xml_obj.add_value_to_node(doc, basicInfo, 'timeStamp', get_cur_time()) print("xml_file:", xml_file) # 写入xml文件 with open(xml_file, "w") as f: doc.writexml(f, addindent=' ' * 4, newl='\n', encoding="UTF-8") # 利用xsd校验xml文件 ret = check_xml_file(xsd_file, xml_file) if ret != 1: print("%s check Fail!!!" % xml_file) return return_msg(1) # FTP上报xml文件到管局 return_code = ftp_logout_customer_info(xml_file,flag, cdn_net_id, cdn_node_id, cdn_house_id, cdn_domain_id) # 错误处理 return return_msg(return_code) except: print(traceback.print_exc()) return return_msg(4)
def save_mount_cdn_node(request): back_dict = {"code": "0", "msg": "success"} back_dict_err = {"code": "1", "msg": "error"} try: print(request.POST) cdn_net_id = request.POST.get("cdn_net_id").strip() cdn_node_id_list_str = request.POST.get("cdn_node_id_list") cdn_node_id_list = json.loads(cdn_node_id_list_str) cdn_node_id_list = list(filter(lambda x: x, cdn_node_id_list)) # 获取先前挂载的节点 before_mount_node = MountCdnNode.objects.filter(cdn_net_id=cdn_net_id) before_mount_node_id_list = [ one.cdn_node_id for one in before_mount_node ] # 找出新添加的节点信息 new_mount_node = list( set(cdn_node_id_list).difference(set(before_mount_node_id_list))) # 新去掉的 new_mount_node_bak = list( set(before_mount_node_id_list).difference(set(cdn_node_id_list))) if not new_mount_node: # 没有新添加的,检查是否有删除的(并且是否是未上报的) if new_mount_node_bak: for one in new_mount_node_bak: status_tmp = MountCdnNode.objects.get( cdn_node_id=one).report_status if int(status_tmp) == 1: MountCdnNode.objects.filter(cdn_node_id=one).delete() # 删除子网下挂载的节点下挂载的机房信息 MountCdnHouse.objects.filter(cdn_net_id=cdn_net_id, cdn_node_id=one).delete() return HttpResponse(json.dumps(back_dict)) if not new_mount_node: return HttpResponse(json.dumps(back_dict)) cur_time = get_cur_time() # 挂载节点 for one_node_id in new_mount_node: MountCdnNode.objects.create(cdn_net_id=cdn_net_id, cdn_node_id=one_node_id, report_status="1", report_time="--", create_time=cur_time) # 将子网,节点,机房 三者之间的关系对应起来 for one_node_id in new_mount_node: mount_house_list = MountCdnHouse.objects.filter( cdn_node_id=one_node_id, cdn_net_id=None) mount_house_id_list = [ one.cdn_house_id for one in mount_house_list ] for one_house_id in mount_house_id_list: MountCdnHouse.objects.create(cdn_net_id=cdn_net_id, cdn_node_id=one_node_id, cdn_house_id=one_house_id, report_status="1", report_time="--", create_time=cur_time) # 修改子网的状态为更新未上报 net_status = CdnNetInfo.objects.get( cdn_net_id=cdn_net_id).report_status if int(net_status) in [2, 4]: CdnNetInfo.objects.filter(cdn_net_id=cdn_net_id).update( report_status="3") except: print(traceback.print_exc()) return HttpResponse(json.dumps(back_dict_err)) return HttpResponse(json.dumps(back_dict))
def save_mount_cdn_house(request): back_dict = {"code": "0", "msg": "success"} back_dict_err = {"code": "1", "msg": "error"} try: print(request.POST) cdn_node_id = request.POST.get("cdn_node_id").strip() cdn_house_id_list_str = request.POST.get("cdn_house_id_list") cdn_house_id_list = json.loads(cdn_house_id_list_str) cdn_house_id_list = list(filter(lambda x: x, cdn_house_id_list)) # 判断此节点是否已经被子网进行挂载了。没有挂载,就进行删除后添加。 is_mount_by_net = MountCdnNode.objects.filter(cdn_node_id=cdn_node_id) if not is_mount_by_net: MountCdnHouse.objects.filter(cdn_node_id=cdn_node_id, cdn_net_id=None).delete() # 获取先前挂载的机房 before_mount_house = MountCdnHouse.objects.filter( cdn_net_id=None, cdn_node_id=cdn_node_id) before_mount_house_id_list = [ one.cdn_house_id for one in before_mount_house ] # 找出新添加的节点信息 new_mount_house = list( set(cdn_house_id_list).difference(set(before_mount_house_id_list))) # 暂时先不考虑去掉一个新添加的没有上报的机房。 cur_time = get_cur_time() for one in new_mount_house: MountCdnHouse.objects.create(cdn_node_id=cdn_node_id, cdn_house_id=one.strip(), create_time=cur_time) # 将新添加的机房都挂载子网下 if is_mount_by_net: cdn_net_info = MountCdnNode.objects.filter(cdn_node_id=cdn_node_id) # 建立子网,节点,机房三者的关系,表示新添加的机房挂载子网下 for O in cdn_net_info: for one_cdn_house_id in new_mount_house: MountCdnHouse.objects.create( cdn_net_id=O.cdn_net_id, cdn_node_id=cdn_node_id, cdn_house_id=one_cdn_house_id.strip(), report_status="1", report_time=cur_time, create_time=cur_time) # 更新子网下挂载的节点的状态为新增未上报 MountCdnNode.objects.filter(cdn_node_id=cdn_node_id).update( report_status="1") # 更新子网的状态 for H in cdn_net_info: net_status = CdnNetInfo.objects.get( cdn_net_id=H.cdn_net_id).report_status if int(net_status) not in [1, 3]: CdnNetInfo.objects.filter(cdn_net_id=H.cdn_net_id).update( report_status="3") except: print(traceback.print_exc()) return HttpResponse(json.dumps(back_dict_err)) return HttpResponse(json.dumps(back_dict))