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 post(self, request): """ 创建录入表单, :param request: :return: json """ incoming_doc_mark = request.POST.get('incoming_doc_mark') # 入库申请(单)编号 stock_mark = request.POST.get('stock_mark') # 存放编号 # user = 当前用户id为1 user_id = User.objects.get(pk=1) apply_cons_mark = request.POST.get('apply_cons_mark') # 申请施工单位代码/入库申请单位 asset_name = request.POST.get('asset_name') # 资产名称 proj_from = request.POST.get('proj_from') # 物资来源 proj_mark = request.POST.get('proj_mark') # 工程编号(选填) proj_name = request.POST.get('proj_name') # 项目名称 ini_from = request.POST.get('ini_from') # 详细来源(选填)(选填) mat_mark = request.POST.get('mat_mark') # 物料编码 mat_extend_mark = request.POST.get('mat_extend_mark') # 物料扩展码(选填) mat_from = request.POST.get('mat_from') # 物料描述(选填) pars = request.POST.get('pars') # 规格型号 mat_type = request.POST.get('mat_type') # 物资类型 dp = request.POST.get('dp') # 生产日期(选填) supplyer = request.POST.get('supplyer') # 厂家(选填) bp = request.POST.get('bp') # 出厂编号(选填) use_date = request.POST.get('use_date') # 投运日期 remove_date = request.POST.get('remove_date') # 拆除日期 pms_status = request.POST.get('pms_status') # PMS台账情况(选填) test_result = request.POST.get('test_result') # 试验结果 wh_mark = request.POST.get('wh_mark') # 仓库代码(拟入仓库名称) num = request.POST.get('num') # 入库数量 is_approve_reason = request.POST.get('is_approve_reason') is_visible = request.POST.get('is_visible') incoming_apply_file = IncomingApply( user=user_id, incoming_doc_mark=incoming_doc_mark, stock_mark=stock_mark, apply_cons_mark=apply_cons_mark, asset_name=asset_name, proj_from=proj_from, proj_mark=proj_mark, proj_name=proj_name, ini_from=ini_from, mat_mark=mat_mark, mat_extend_mark=mat_extend_mark, mat_from=mat_from, mat_type=mat_type, pars=pars, dp=dp, supplyer=supplyer, bp=bp, use_date=use_date, remove_date=remove_date, pms_status=pms_status, test_result=test_result, wh_mark=wh_mark, num=num, is_approve_reason=is_approve_reason, ) try: incoming_apply_file.save() return restful.ok(data='创建入库申请单成功!') except: return restful.server_error(message='已存在该入库申请单编号!')
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__())