def search_data(request): """ 根据条件查询数据,并获取第一页数据 example:terms={'id': 79},values=('id', 'path', 'error')) :return data=data_paginator() """ try: model = MASTER_MODEL query_mark = MASTER_NAME today_time = datetime.datetime.today() start_time = datetime.datetime(today_time.year, today_time.month, 1) time_range = (start_time, today_time) kwargs = {'err_time__range': time_range} # 查询一个月内的数据 check_acc = False fun_check = None # 默认查询所有数据, 使用时间从新到旧方式排序 data_search.data_search(request, model, query_mark=query_mark, values=errorLog_values, order=('-id', ), **kwargs) # 分页后第一页数据返回 data = data_search.data_paginator(request, model, query_mark) return data except Exception as e: log_print(error=e.__str__()) errlog_add(request, e.__str__()) return restful.server_error(message=e.__str__())
def clean_querys(request): """清理全部缓存""" try: model = MASTER_MODEL query_mark = MASTER_NAME utils.clean_querys(request, model, query_mark) msg = '操作成功!已成功执行缓存清理操作。' return restful.ok(message=msg) except Exception as e: log_print(error=e.__str__()) # 保存日志到文件 errlog_add(request, e.__str__()) # 保存日志到数据库 msg = '操作失败!错误:%s。' % e.__str__() return restful.server_error(message=msg)
def load_data(request): """获取分页数据""" try: model = MASTER_MODEL query_mark = MASTER_NAME # 返回分页数据 data = data_search.data_paginator(request, model=model, query_mark=query_mark) return data except Exception as e: log_print(error=e.__str__()) errlog_add(request, e.__str__()) return restful.server_error(message=e.__str__())
def incoming_search_data(request): try: # 条件查询数据,保存到缓存中,并实现page=1分页展示 # {'mat_extend_mark': 'Sd'} <class 'dict'> values = ('id', 'user__user_name') # 需要展示的值:.value(内容) # 查询数据并且缓存 data_search.data_search(request, INCOMING_MODEL, INCOMING_NAME, values) # 首页分页 data = data_search.data_paginator(request, INCOMING_MODEL, INCOMING_NAME,) return restful.ok(data=data) except Exception as e: log_print(excepts=e) errlog_add(request, e.__str__()) return restful.server_error(message=e.__str__())
def export_excel(request): """导出EXCEL""" try: model = MASTER_MODEL query_mark = MASTER_NAME filename = MASTER_NAME + '.xlsx' response = attachment.export_excel( request, model=model, query_mark=query_mark, filename=filename, sheet_name='2020', sheet_head_data=errorLog_sheetHeadData) return response except Exception as e: log_print(e.__str__()) errlog_add(request, e.__str__()) msg = '操作失败!错误:%s。' % e.__str__() return restful.server_error(message=msg)
def data_paginator(request, model, query_mark, paginator_page=2, num=2): """:前端传递值: 'page'-->int:页码数; 默认page=1 (json数据分页接口) :param model:-->model :被分页数据原始模型名称 :param query_mark:-->str : 模块名称 :param paginator_page:每页需要展示的数据个数; :param num:int-->页码两边需要展示的页码数量; :return: Return JsonResponse of paginator data, or message of str if redis.key is None """ try: # 判断前端是否传值,没有默认为page=1 if request.GET.get('page'): page = int(request.GET.get('page')) else: page = 1 rc_search_result = redis.Redis(connection_pool=pool_result) # 连接redis池 rc_key = get_key(request, model, query_mark) # 获取redis key if rc_search_result.get(rc_key) is None: # 判断是否有key值 return restful.ok(message='缓存过期,请重新查询!') else: data = json.loads(rc_search_result.get(rc_key)) paginator_page = paginator_page # 每页需要展示的数据个数 num = num # 需要两边展示的页码数量 paginator = Paginator(data, paginator_page) # 数据分组 data_sum = paginator.count # 所有数据个数 page_sum = paginator.num_pages # 所有数据页数 current_page_data = paginator.page(page) # 获取当前页数据对象 has_next_page = current_page_data.has_next() # 是否有下一页 has_previous_page = current_page_data.has_previous() # 是否有上一页 if page <= page_sum - num - 2: """判断左右两边存在合并情况""" right_has_more = True # 1...234.. right_pages = list(range(page + 1, page + num + 1)) else: """89, 789格式""" right_has_more = False right_pages = list(range(page + 1, page_sum + 1)) if page >= num + 3: """...234...12""" left_has_more = True left_pages = list((page - num, page)) else: left_has_more = False left_pages = list(range(1, page)) if page != 1: previous_page_number = current_page_data.previous_page_number() else: previous_page_number = 1 if page != page_sum: next_page_number = current_page_data.next_page_number() else: next_page_number = page_sum pa_data = { # 获取当前页数据(queryset) 'current_page_data': list(current_page_data.object_list), 'current_page': page, # 选中页码 'data_sum': data_sum, # 所有数据个数 'page_sum': page_sum, # 所有数据页数 'has_next': has_next_page, # 是否存在下一页 'has_previous': has_previous_page, 'right_pages': right_pages, # 右边额外展示的页码list 'left_pages': left_pages, 'previous_page_number': previous_page_number, # 点击上一页返回页面id 'next_page_number': next_page_number, # 点击下一页返回页面id 'right_has_more': right_has_more, 'left_has_more': left_has_more, } return restful.ok(message='读取成功!', data=pa_data) except Exception as e: log_print(waring=traceback.format_exc()) # 返回异常堆栈信息到日志中,e是异常类 errlog_add(request, e.__str__()) # 异常保存到数据库 return restful.server_error(message=e.__str__())