コード例 #1
0
def permission_add(request, second_menu_id):
    """
    三级菜单增加视图功能函数(即添加权限)
    :param request:
    :return:
    """
    # menu_obj = models.Menu.objects.filter(id=menu_id).first()
    if request.method == "GET":
        form = PermissionModelForm()
        return render(request, 'rbac/change.html', {'form': form})

    # 数据校验相关,校验成功则直接保存到数据库
    form = PermissionModelForm(data=request.POST)
    if form.is_valid():
        # 查询传过来的二级菜单是否存在,不存在则返回错误页面
        second_menu_objects = models.Permission.objects.filter(
            pid=second_menu_id).first()
        if not second_menu_objects:
            return render(request, 'rbac/menu_error.html')
        # form.instance 中包含了用户提交的所有值
        form.instance.ppid = second_menu_objects
        form.save()
        url = memory_reverse(request, 'menu_list')
        return redirect(url)
    return render(request, 'rbac/change.html', {'form': form})
コード例 #2
0
def second_menu_del(request, id):
    """
    删除二级菜单
    :param request:
    :return:
    """
    url = memory_reverse(request, 'menu_list')
    if request.method == "GET":
        return render(request, 'rbac/delete.html', {'cancel': url})
    models.Permission.objects.filter(pid=id).delete()  # 对数据库进行删除操作

    return redirect(url)
コード例 #3
0
def multi_permission_del(request, pid):
    """
    批量页面的权限删除功能视图函数
    :param request:
    :param pid:
    :return:
    """
    url = memory_reverse(request, 'multi_permission')
    if request.method == "GET":
        return render(request, 'rbac/delete.html', {'cancel': url})
    models.Permission.objects.filter(pid=pid).delete()  # 对数据库进行删除操作

    return redirect(url)
コード例 #4
0
def menu_del(request, id):
    """
    删除一级菜单视图功能函数
    :param request:
    :param id:  为当前删除内容的 id
    :return:
    """
    # origin_url = reverse('menu_del')
    url = memory_reverse(request, 'menu_list')
    if request.method == "GET":
        return render(request, 'rbac/delete.html', {'cancel': url})
    models.Menu.objects.filter(id=id).delete()  # 对数据库进行删除操作

    return redirect(url)
コード例 #5
0
def menu_add(request):
    """
    新建菜单视图功能函数
    :param request:
    :return:
    """
    if request.method == "GET":
        form = MenuModelForm()
        return render(request, 'rbac/change.html', {'form': form})
    form = MenuModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        url = memory_reverse(request, 'menu_list')
        return redirect(url)
    return render(request, 'rbac/change.html', {'form': form})
コード例 #6
0
def permission_edit(request, id):
    """
    编辑权限功能视图函数
    :param request:
    :param id: 要编辑的权限 id
    :return:
    """
    permission_obj = models.Permission.objects.filter(pid=id).first()
    if request.method == "GET":
        form = PermissionModelForm(instance=permission_obj)
        return render(request, 'rbac/change.html', {'form': form})
    form = PermissionModelForm(data=request.POST, instance=permission_obj)
    if form.is_valid():
        form.save()
        url = memory_reverse(request, 'menu_list')
        return redirect(url)
    return render(request, 'rbac/change.html', {'form': form})
コード例 #7
0
def menu_edit(request, id):
    """
    编辑一级菜单视图功能函数
    :param request:
    :param id: 为当前编辑内容的 id
    :return:
    """
    obj = models.Menu.objects.filter(id=id).first()
    if not obj:
        return render(request, 'rbac/menu_error.html')
    if request.method == 'GET':
        form = MenuModelForm(instance=obj)  # instance= 表示给模板传默认值
        return render(request, 'rbac/change.html', {'form': form})
    form = MenuModelForm(instance=obj, data=request.POST)
    if form.is_valid():
        form.save()
        url = memory_reverse(request, 'menu_list')
        return redirect(url)
    return render(request, 'rbac/change.html', {'form': form})
コード例 #8
0
def second_menu_add(request, menu_id):
    """
    新增二级菜单
    :param request:
    :param menu_id: 当前二级菜单的一级菜单 id (用于设置默认值)
    :return:
    """
    menu_obj = models.Menu.objects.filter(id=menu_id).first()
    if request.method == "GET":
        # initial 表示传过去的数据为选择框或 input 标签的默认值,
        # 传参格式为字典形式,字典的 key 则为 models 中创建数据库表时定义的字段
        form = SecondMenuModelForm(initial={'menu_p': menu_obj})
        return render(request, 'rbac/change.html', {'form': form})
    form = SecondMenuModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        url = memory_reverse(request, 'menu_list')
        return redirect(url)
    return render(request, 'rbac/change.html', {'form': form})