def menu(request): ''' 获取 前端导航栏 ''' username, role, clientip = User(request).get_default_values() # 初始化返回数据 ret_data = RET_DATA.copy() ret_data['code'] = 0 # 请求正常,返回 0 ret_data['msg'] = '获取导航栏路由' ret_data['data'] = [] # 获取用户对路由的权限 try: if request.user.is_superuser: # 超级用户拥有所有权限 first_level_list = WebUriFirstLevelTb.objects.filter( status=1).all() second_level_list = WebUriSecondLevelTb.objects.filter( status=1).all() third_level_list = WebUriThirdLevelTb.objects.filter( status=1).all() else: user_web_uri_p = UserPermissionsTb.objects.get(user=request.user) first_level_list = [ per for per in user_web_uri_p.weburifirst_l.filter(status=1).all() ] second_level_list = [ per for per in user_web_uri_p.weburisecond_l.filter( status=1).all() ] third_level_list = [ per for per in user_web_uri_p.weburithird_l.filter(status=1).all() ] for group in user_web_uri_p.usergroup_p.filter( status=1).all(): # 循环将组权限 分配给用户 for first_level in group.weburifirst_l.filter(status=1).all(): if first_level not in first_level_list: first_level_list.append(first_level) for second_level in group.weburisecond_l.filter( status=1).all(): if second_level not in second_level_list: second_level_list.append(second_level) for third_level in group.weburithird_l.filter(status=1).all(): if third_level not in third_level_list: third_level_list.append(third_level) except Exception as e: logger.error(str(e)) return HttpResponse(json.dumps(ret_data)) # 将权限 格式化成合法的数据 for first_level in first_level_list: first_level_dict = { # 获取第一层导航路由 'title': first_level.title, 'name': first_level.name, 'jump': first_level.jump, 'icon': first_level.icon, 'list': [] } for second_level in first_level.next_l.filter(status=1).all(): if second_level in second_level_list: second_level_dict = { # 获取第二层导航路由 'title': second_level.title, 'name': second_level.name, 'jump': second_level.jump, 'icon': second_level.icon, 'list': [] } for third_level in second_level.next_l.filter(status=1).all(): if third_level in third_level_list: third_level_dict = { # 获取第三层导航路由 'title': third_level.title, 'name': third_level.name, 'jump': third_level.jump, 'icon': third_level.icon, 'list': [] } second_level_dict['list'].append(third_level_dict) first_level_dict['list'].append(second_level_dict) ret_data['data'].append(first_level_dict) return HttpResponse(json.dumps(ret_data))
def get_reflesh_project(request): ''' 获取 清缓存 列表数据 ''' username, role, clientip = User(request).get_default_values() # 初始化返回数据 ret_data = RET_DATA.copy() ret_data['code'] = 0 # 请求正常,返回 0 ret_data['msg'] = '获取[清缓存列表数据]成功' ret_data['data'] = {'domain_project': [], 'cdn': []} # 获取 cdn缓存项目 domain_project_list = DoaminProjectTb.objects.filter(status=1).all() for prot in domain_project_list: tmpdict = {} tmpdict['project'] = prot.project tmpdict['domain'] = [ {'id': domain.id, 'name': urlparse(domain.name).scheme+"://"+urlparse(domain.name).netloc, 'product': domain.get_product_display(), 'customer': domain.get_customer_display()} for domain in prot.domain.all() ] #tmpdict['cdn'] = [ {'name': cdn.get_name_display(), # 'account': cdn.account} for cdn in cdn_t.objects.all() ] ret_data['data']['domain_project'].append(tmpdict) # 获取cdn 账号 cdn_acc_list = Domainns(request).get_cdn_account() for cdn in cdn_acc_list: tmpdict = { 'id': cdn.id, 'name': cdn.get_name_display(), 'account': cdn.account, 'domain': [], } if cdn.get_name_display() == "wangsu": req = WsApi(cdn.secretid, cdn.secretkey) results, status = req.getdomains() if status: for line in results: if line['enabled'] == 'true': tmpdict['domain'].append({ 'name': line['domain-name'], 'ssl' : 1 if line['service-type']=='web-https' else 0, }) # elif cdn.get_name_display() == "tencent": # 腾讯云接口有问题,后面再修复 # req = TcApi(cdn.secretid, cdn.secretkey) # results, status = req.getdomains() # for line in results['data']['hosts']: # if line['disabled'] == 0 and line['status'] in [3, 4, 5]: # tmpdict['domain'].append({ # 'name': line['host'], # 'ssl' : 1 if line['ssl_type']!=0 else 0, # }) elif cdn.get_name_display() == "aws": req = AwsApi(cdn.secretid, cdn.secretkey) results, status = req.getdomains(['fenghuang']) if status: for item in results: tmpdict['domain'].append({ 'Id': item['Id'], 'name': item['domains'], 'ssl' : 0, 'product': item['product'], 'customer': item['customer'] }) else: tmpdict['domain'] = [] ret_data['data']['cdn'].append(tmpdict) # break #logger.info(ret_data['data']) ret_data['data']['cdn'].sort(key=lambda acc: acc['name']) # CDN账号按 name的分类 排序 return HttpResponse(json.dumps(ret_data))
def telegram_sendgroupmessage(request): ''' 往对应群组发送信息 ''' username, role, clientip = User(request).get_default_values() # 初始化返回数据 ret_data = RET_DATA.copy() ret_data['code'] = 0 # 请求正常,返回 0 ret_data['msg'] = '未发送任何消息' # 初始化 telegram 信息 message = MESSAGE_TEST.copy() # time.sleep(5) if request.method == 'POST': data = request.POST # 检查群组是否为空 if 'group' not in data or not data['group']: ret_data['msg'] = 'TG群组为空,请检查!' return HttpResponse(json.dumps(ret_data)) else: message['group'] = data['group'] # 检查是否有图片 if 'files[]' in request.FILES and request.FILES.getlist('files[]'): # request.FILES 是一个 MultiValueDict,如果传入的参数是一个数组,需要通过 getlist 方法来获取列表数据 for img in request.FILES.getlist('files[]'): logger.info(img) # 判断文件是不是 gif s = SendTelegram(message) if str(img)[-3:] == "gif": r = s.send_document(img) else: r = s.send_document(img) # r = s.send_photo(img) logger.info(message) if r: ret_data['msg'] = '图片发送成功' else: ret_data['code'] = 500 ret_data['msg'] = '图片发送失败' return HttpResponse(json.dumps(ret_data)) # 检查信息是否为空 if 'text' not in data or not data['text']: # ret_data['msg'] = '信息为空,请检查!' return HttpResponse(json.dumps(ret_data)) # 获取需要@的部门或组 atUsers = [] if 'atUsers' in data and data['atUsers']: atUsersDepList = [ int(id) for id in data['atUsers'].replace(' ', '').split(',') if IsSomeType(id).is_int() ] atUsersSelects = DepartmentUserTb.objects.filter( status=1, id__in=atUsersDepList).all() for department in atUsersSelects: atUsers.append(department.department + ': ' + ', '.join([ '@' + user.user for user in department.user.filter(status=1).all() ])) message['text'] = data['text'] + '\r\n\r\n' + '\r\n'.join(atUsers) s = SendTelegram(message) if s.send(): ret_data['msg'] = '发送成功!' else: ret_data['msg'] = 'telegram 接口错误,发送失败!' return HttpResponse(json.dumps(ret_data)) else: ret_data['msg'] = '403' return HttpResponse(json.dumps(ret_data))
def get_zone_records(request): ''' 获取DNSPOD 域名信息 ''' username, role, clientip = User(request).get_default_values() # 初始化返回数据 ret_data = RET_DATA.copy() ret_data['code'] = 0 # 请求正常,返回 0 ret_data['msg'] = '获取域名解析成功' ret_data['data'] = [] try: if request.method == 'POST': data = json.loads(request.body) logger.info(data) for zone in data['zones']: dnspod_acc = DnspodAccountTb.objects.get( name=zone['dnspod_acc']) dpapi = DpApi(DNSPOD_URL, dnspod_acc.key) result, status = dpapi.get_zone_records(zone['domain']) if not status: logger.error("查询 %s 域名失败!%s" % (zone['domain'], str(result))) return HttpResponseServerError('error!') else: #logger.info(result) for record in result['records']: if record['type'] in ['A', 'CNAME']: ret_data['data'].append({ 'dnspod_acc': zone['dnspod_acc'], 'zone': zone['domain'], 'zone_id': zone['zone_id'], 'sub_domain': record['name'], 'name': record['name'] + '.' + zone['domain'] if record['name'] != '@' else zone['domain'], 'type': record['type'], 'value': record['value'], 'record_id': record['id'], 'record_line': record['line'], 'record_line_id': record['line_id'], #'record_line_id': record['line_id'].replace('=', '%3D'), 'enabled': record['enabled'], }) except Exception as e: logger.error(str(e)) ret_data['code'] = 500 ret_data['msg'] = '获取域名信息失败: %s' % str(e) ret_data['count'] = len(ret_data['data']) return HttpResponse(json.dumps(ret_data))
def get_domain_records(request): ''' 域名列表获取 ''' username, role, clientip = User(request).get_default_values() # 初始化返回数据 ret_data = RET_DATA.copy() ret_data['code'] = 0 # 请求正常,返回 0 ret_data['msg'] = '域名列表获取成功' ret_data['data'] = [] try: if request.method == 'POST': data = json.loads(request.body) domains = DomainTb.objects.filter(name__icontains=data['name'], status__in=data['status'], group__id__in=data['group'], product__in=data['product'], customer__in=data['customer']) logger.info(data) if len(data['cdn']) != 0: domains = domains.filter(cdn__in=data['cdn']) if len(data['cf']) != 0: domains = domains.filter(cf__in=data['cf']) domains = domains.all().order_by('-id') for domain in domains: tmp_dict = {} tmp_dict['id'] = domain.id tmp_dict['name'] = domain.name tmp_dict['product'] = (domain.product, domain.get_product_display()) tmp_dict['customer'] = (domain.customer, domain.get_customer_display()) tmp_dict['content'] = domain.content tmp_dict['status'] = domain.status tmp_dict['group'] = { 'id': domain.group.id, 'group': domain.group.group, } tmp_dict['cdn'] = [{ 'id': cdn.id, 'name': cdn.get_name_display(), 'account': cdn.account, } for cdn in domain.cdn.all()] if domain.cf: tmp_dict['cf'] = [{ 'id': domain.cf.id, 'name': "cloudflare", 'account': domain.cf.name, }] else: tmp_dict['cf'] = [] ret_data['data'].append(tmp_dict) except Exception as e: logger.error(str(e)) ret_data['code'] = 500 ret_data['msg'] = '域名列表获取失败: %s' % str(e) return HttpResponse(json.dumps(ret_data))
def cdn_purge(cdn_d, uri_l=['/'], ret_data=RET_DATA.copy(), con=1, socket=None): ''' 按CDN 账号进行缓存清理:cdn 为查询object,ret_data 当前函数返回值, con 清缓存域名为并发数 cdn_d[cdn.get_name_display()+"_"+cdn.account] = { 'name': cdn.get_name_display(), 'domain': [], 'secretid': str(cdn.secretid), 'secretkey': str(cdn.secretkey), 'failed': [], 'success': [], } ''' for cdn in cdn_d: if cdn_d[cdn]['domain']: #开始清缓存,判断CDN接口是否存在 if cdn_d[cdn]['name'] == "wangsu": # 网宿 req = WsApi(cdn_d[cdn]['secretid'], cdn_d[cdn]['secretkey']) # elif cdn_d[cdn]['name'] == "tencent": # 腾讯云的接口有点问题,后面修复 # req = tcApi(cdn_d[cdn]['secretid'], cdn_d[cdn]['secretkey']) elif cdn_d[cdn]['name'] == "aws": # AWS req = AwsApi(cdn_d[cdn]['secretid'], cdn_d[cdn]['secretkey']) else: # CDN 接口不存在 ret_data['code'] = 500 ret_data['msg'] = cdn + ": CDN 接口不存在!" logger.error(ret_data['msg']) if socket: socket.send(text_data=json.dumps(ret_data)) socket.close() return cdn_d, ret_data # 开始清缓存 if cdn_d[cdn]['name'] == "aws": # aws cdn 域名清理 for Id in cdn_d[cdn]['domain']: cdn_d[cdn]['domain'].remove(Id) # 对已经清理过的域名剔除列表 result, status = req.purge(Id, uri_l) name = cdn + " - " + Id if status: ret_data['msg'] = name + ": 缓存清理执行中......" cdn_d[cdn]['success'] += [name] else: ret_data['msg'] = name + ": 缓存清理失败!" cdn_d[cdn]['failed'] += [name] if socket: ret_data['step_finished'] += 1 socket.send(text_data=json.dumps(ret_data)) else: while len(cdn_d[cdn]['domain']) != 0: domains_c = cdn_d[cdn]['domain'][:con] cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][ con:] # 对已经清理过的域名剔除列表 for uri in uri_l: result, status = req.purge(domains_c, uri) if status: ret_data['msg'] = "<br/>".join([ domain + uri + ": 清缓存成功。" for domain in domains_c ]) cdn_d[cdn]['success'] += [ domain + uri for domain in domains_c ] else: ret_data['msg'] = "<br/>".join([ domain + uri + ": 清缓存失败!" for domain in domains_c ]) cdn_d[cdn]['failed'] += [ domain + uri for domain in domains_c ] if socket: ret_data['step_finished'] += len(domains_c) # logger.info(ret_data['step_finished']) socket.send(text_data=json.dumps(ret_data)) return cdn_d, ret_data