def ajax_delete(): """ 用户删除 :return: """ ajax_success_msg = AJAX_SUCCESS_MSG.copy() ajax_failure_msg = AJAX_FAILURE_MSG.copy() # 检查删除权限 if not permission_user_section_del.can(): ext_msg = _('Permission Denied') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求方法 if not (request.method == 'GET' and request.is_xhr): ext_msg = _('Method Not Allowed') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求参数 user_id = request.args.get('user_id', 0, type=int) if not user_id: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) user_info = get_user_row_by_id(user_id) # 检查资源是否存在 if not user_info: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查资源是否删除 if user_info.status_delete == STATUS_DEL_OK: ext_msg = _('Already deleted') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查是否正在使用 # 报价、订单 if count_quotation(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) current_time = datetime.utcnow() user_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_user(user_id, user_data) if result: ajax_success_msg['msg'] = _('Del Success') return jsonify(ajax_success_msg) else: ajax_failure_msg['msg'] = _('Del Failure') return jsonify(ajax_failure_msg)
def filter_count_quotation(user_id=None): """ 报价计数 :param user_id: :return: """ if not user_id: return 0 count = count_quotation(Quotation.uid == user_id) return count
def quotation_import(): """ 报价导入 :return: """ template_name = 'system/quotation_import.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('quotation import') # 加载表单 form = QuotationUploadForm(request.form) if request.method == 'POST': ajax_success_msg = AJAX_SUCCESS_MSG.copy() ajax_failure_msg = AJAX_FAILURE_MSG.copy() try: # files = [] file_item = request.files.get('file') csv_data = request.get_array(field_name='file') # 校验数据是否有效 if len(csv_data) < 3: raise Exception('数据错误') csv_data.pop(0) csv_head = csv_data.pop(0) csv_count = len(csv_data) column_names = Quotation.__table__.columns.keys() if not set(csv_head).issubset(set(column_names)): raise Exception('数据错误') # 清空历史 delete_count = delete_quotation_table() # 执行导入 for item in csv_data: add_quotation(dict(zip(csv_head, item))) file_info = { 'name': file_item.filename, 'content_type': file_item.content_type, 'size': bytes2human(get_file_size(file_item)), } import_info = { 'delete_count': delete_count, 'csv_count': csv_count, 'db_count': count_quotation(), } # files.append(file_info) ajax_success_msg['file_info'] = file_info ajax_success_msg['import_info'] = import_info return jsonify(ajax_success_msg) except Exception as e: ajax_failure_msg['msg'] = e.message return jsonify(ajax_failure_msg) # 渲染模板 return render_template(template_name, form=form, **document_info)
def lists(): """ 客户列表 :return: """ template_name = 'customer/lists.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('customer lists') # 搜索条件 form = CustomerSearchForm(request.form) form.owner_uid.choices = DEFAULT_SEARCH_CHOICES_INT + [ (user.id, user.name) for user in get_user_rows(**{'status_delete': STATUS_DEL_NO}) ] # app.logger.info('') search_condition = [ Customer.status_delete == STATUS_DEL_NO, ] if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Search Failure'), 'danger') # 单独处理csrf_token if hasattr(form, 'csrf_token') and getattr(form, 'csrf_token').errors: map(lambda x: flash(x, 'danger'), form.csrf_token.errors) else: if form.company_name.data: search_condition.append( Customer.company_name.like('%%%s%%' % form.company_name.data)) if form.company_type.data != DEFAULT_SEARCH_CHOICES_INT_OPTION: search_condition.append( Customer.company_type == form.company_type.data) if form.owner_uid.data != DEFAULT_SEARCH_CHOICES_INT_OPTION: search_condition.append( Customer.owner_uid == form.owner_uid.data) if form.start_create_time.data: search_condition.append( Customer.create_time >= form.start_create_time.data) if form.end_create_time.data: search_condition.append( Customer.create_time <= form.end_create_time.data) # 处理导出 if form.op.data == OPERATION_EXPORT: # 检查导出权限 if not permission_customer_section_export.can(): abort(403) column_names = Customer.__table__.columns.keys() query_sets = get_customer_rows(*search_condition) return excel.make_response_from_query_sets( query_sets=query_sets, column_names=column_names, file_type='csv', file_name='%s.csv' % _('customer lists')) # 批量删除 if form.op.data == OPERATION_DELETE: # 检查删除权限 if not permission_customer_section_del.can(): abort(403) customer_ids = request.form.getlist('customer_id') # 检查删除权限 permitted = True for customer_id in customer_ids: # TODO 资源删除权限验证 if False: ext_msg = _('Permission Denied') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 检查是否正在使用 # 1、报价 if count_quotation(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 2、订单 if count_sales_order(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 3、敏感型号 if count_production_sensitive(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break if permitted: result_total = True for customer_id in customer_ids: current_time = datetime.utcnow() customer_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_customer(customer_id, customer_data) if result: # 发送删除信号 signal_data = { 'customer_id': customer_id, 'status_delete': STATUS_DEL_OK, 'current_time': current_time, } signal_customer_status_delete.send(app, **signal_data) result_total = result_total and result if result_total: flash(_('Del Success'), 'success') else: flash(_('Del Failure'), 'danger') # 翻页数据 pagination = get_customer_pagination(form.page.data, PER_PAGE_BACKEND, *search_condition) # 渲染模板 return render_template(template_name, form=form, pagination=pagination, **document_info)
def ajax_delete(): """ 客户删除 :return: """ ajax_success_msg = AJAX_SUCCESS_MSG.copy() ajax_failure_msg = AJAX_FAILURE_MSG.copy() # 检查删除权限 if not permission_customer_section_del.can(): ext_msg = _('Permission Denied') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求方法 if not (request.method == 'GET' and request.is_xhr): ext_msg = _('Method Not Allowed') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求参数 customer_id = request.args.get('customer_id', 0, type=int) if not customer_id: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) customer_info = get_customer_row_by_id(customer_id) # 检查资源是否存在 if not customer_info: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查资源是否删除 if customer_info.status_delete == STATUS_DEL_OK: ext_msg = _('Already deleted') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查是否正在使用 # 报价、订单、敏感型号 if count_quotation(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) if count_sales_order(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) if count_production_sensitive(**{ 'customer_cid': customer_id, 'status_delete': STATUS_DEL_NO }): ext_msg = _('Currently In Use') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) current_time = datetime.utcnow() customer_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_customer(customer_id, customer_data) if result: # 发送删除信号 signal_data = { 'customer_id': customer_id, 'status_delete': STATUS_DEL_OK, 'current_time': current_time, } signal_customer_status_delete.send(app, **signal_data) ajax_success_msg['msg'] = _('Del Success') return jsonify(ajax_success_msg) else: ajax_failure_msg['msg'] = _('Del Failure') return jsonify(ajax_failure_msg)
def lists(): """ 用户列表 :return: """ template_name = 'user/lists.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('user lists') # 搜索条件 form = UserSearchForm(request.form) search_condition = [ User.status_delete == STATUS_DEL_NO, ] if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Search Failure'), 'danger') # 单独处理csrf_token if hasattr(form, 'csrf_token') and getattr(form, 'csrf_token').errors: map(lambda x: flash(x, 'danger'), form.csrf_token.errors) else: if form.name.data: search_condition.append(User.name == form.name.data) if form.role_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION: search_condition.append(User.role_id == form.role_id.data) if form.start_create_time.data: search_condition.append(User.create_time >= form.start_create_time.data) if form.end_create_time.data: search_condition.append(User.create_time <= form.end_create_time.data) # 处理导出 if form.op.data == OPERATION_EXPORT: # 检查导出权限 if not permission_user_section_export.can(): abort(403) column_names = User.__table__.columns.keys() query_sets = get_user_rows(*search_condition) return excel.make_response_from_query_sets( query_sets=query_sets, column_names=column_names, file_type='csv', file_name='%s.csv' % _('user lists') ) # 批量删除 if form.op.data == OPERATION_DELETE: # 检查删除权限 if not permission_user_section_del.can(): abort(403) user_ids = request.form.getlist('user_id') # 检查删除权限 permitted = True for user_id in user_ids: # 检查是否正在使用 # 1、报价 if count_quotation(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 2、销售订单 if count_sales_order(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 3、销售出货 if count_delivery(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 4、询价 if count_enquiry(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 5、采购订单 if count_buyer_order(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break # 6、采购进货 if count_purchase(**{'uid': user_id, 'status_delete': STATUS_DEL_NO}): ext_msg = _('Currently In Use') flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg), 'danger') permitted = False break if permitted: result_total = True for user_id in user_ids: current_time = datetime.utcnow() user_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_user(user_id, user_data) result_total = result_total and result if result_total: flash(_('Del Success'), 'success') else: flash(_('Del Failure'), 'danger') # 翻页数据 pagination = get_user_pagination(form.page.data, PER_PAGE_BACKEND, *search_condition) # 渲染模板 return render_template( template_name, form=form, pagination=pagination, **document_info )