Example #1
0
def ajax_delete():
    """
    渠道删除
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

    # 检查删除权限
    if not permission_supplier_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)

    # 检查请求参数
    supplier_id = request.args.get('supplier_id', 0, type=int)
    if not supplier_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)

    supplier_info = get_supplier_row_by_id(supplier_id)
    # 检查资源是否存在
    if not supplier_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 supplier_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)

    current_time = datetime.utcnow()
    supplier_data = {
        'status_delete': STATUS_DEL_OK,
        'delete_time': current_time,
        'update_time': current_time,
    }
    result = edit_supplier(supplier_id, supplier_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)
Example #2
0
def lists():
    """
    渠道列表
    :return:
    """
    template_name = 'supplier/lists.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('supplier lists')

    # 搜索条件
    form = SupplierSearchForm(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 = [
        Supplier.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(
                    Supplier.company_name.like('%%%s%%' %
                                               form.company_name.data))
            if form.company_type.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(
                    Supplier.company_type == form.company_type.data)
            if form.owner_uid.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(
                    Supplier.owner_uid == form.owner_uid.data)
            if form.start_create_time.data:
                search_condition.append(
                    Supplier.create_time >= form.start_create_time.data)
            if form.end_create_time.data:
                search_condition.append(
                    Supplier.create_time <= form.end_create_time.data)
        # 处理导出
        if form.op.data == OPERATION_EXPORT:
            # 检查导出权限
            if not permission_supplier_section_export.can():
                abort(403)
            column_names = Supplier.__table__.columns.keys()
            query_sets = get_supplier_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' % _('supplier lists'))
        # 批量删除
        if form.op.data == OPERATION_DELETE:
            # 检查删除权限
            if not permission_supplier_section_del.can():
                abort(403)
            supplier_ids = request.form.getlist('supplier_id')
            # 检查删除权限
            permitted = True
            for supplier_id in supplier_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_enquiry(**{
                        'supplier_cid': supplier_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_buyer_order(**{
                        'supplier_cid': supplier_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 supplier_id in supplier_ids:
                    current_time = datetime.utcnow()
                    supplier_data = {
                        'status_delete': STATUS_DEL_OK,
                        'delete_time': current_time,
                        'update_time': current_time,
                    }
                    result = edit_supplier(supplier_id, supplier_data)
                    if result:
                        # 发送删除信号
                        signal_data = {
                            'supplier_id': supplier_id,
                            'status_delete': STATUS_DEL_OK,
                            'current_time': current_time,
                        }
                        signal_supplier_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_supplier_pagination(form.page.data, PER_PAGE_BACKEND,
                                         *search_condition)

    # 渲染模板
    return render_template(template_name,
                           form=form,
                           pagination=pagination,
                           **document_info)
Example #3
0
def edit(supplier_id):
    """
    渠道编辑
    """
    supplier_info = get_supplier_row_by_id(supplier_id)
    # 检查资源是否存在
    if not supplier_info:
        abort(404)
    # 检查资源是否删除
    if supplier_info.status_delete == STATUS_DEL_OK:
        abort(410)

    template_name = 'supplier/edit.html'

    # 加载编辑表单
    form = SupplierEditForm(request.form)
    form.owner_uid.choices = get_user_choices()

    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('supplier edit')

    # 进入编辑页面
    if request.method == 'GET':
        # 表单赋值
        form.id.data = supplier_info.id
        form.company_name.data = supplier_info.company_name
        form.company_address.data = supplier_info.company_address
        form.company_site.data = supplier_info.company_site
        form.company_tel.data = supplier_info.company_tel
        form.company_fax.data = supplier_info.company_fax
        form.company_email.data = supplier_info.company_email
        form.company_type.data = supplier_info.company_type
        form.owner_uid.data = supplier_info.owner_uid
        form.create_time.data = supplier_info.create_time
        form.update_time.data = supplier_info.update_time
        # 渲染页面
        return render_template(template_name,
                               supplier_id=supplier_id,
                               form=form,
                               **document_info)

    # 处理编辑请求
    if request.method == 'POST':
        # 表单校验失败
        if supplier_id != form.id.data or not form.validate_on_submit():
            flash(_('Edit Failure'), 'danger')
            # flash(form.errors, 'danger')
            return render_template(template_name,
                                   supplier_id=supplier_id,
                                   form=form,
                                   **document_info)
        # 表单校验成功
        current_time = datetime.utcnow()
        supplier_data = {
            'company_name': form.company_name.data,
            'company_address': form.company_address.data,
            'company_site': form.company_site.data,
            'company_tel': form.company_tel.data,
            'company_fax': form.company_fax.data,
            'company_email': form.company_email.data,
            'company_type': form.company_type.data,
            'owner_uid': form.owner_uid.data,
            'update_time': current_time,
        }
        result = edit_supplier(supplier_id, supplier_data)
        # 编辑操作成功
        if result:
            flash(_('Edit Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('supplier.lists'))
        # 编辑操作失败
        else:
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   supplier_id=supplier_id,
                                   form=form,
                                   **document_info)