Ejemplo n.º 1
0
def add_resource(request, resource_pid=None):
    if request.method == 'POST':
        ds = DataSerialize()
        form = AddResourceForm(model=Menu, data=request.POST)
        if form.is_valid():
            new_resource = form.save()
            r_parent = new_resource.parent
            if r_parent is not None:
                new_resource.level = new_resource.parent.level - 1
            elif r_parent is None:
                new_resource.level = 0
            new_resource.save()

            sort_subnodes(parent_ins=r_parent)
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        if resource_pid is None or resource_pid == '' or int(
                resource_pid) == 0:
            form = AddResourceForm(model=Menu, )
        else:
            form = AddResourceForm(model=Menu,
                                   instance=Menu(parent_id=resource_pid))

    app = app_info()
    app['name'] = "permission"
    app['location'] = 'add'
    return render(request, 'add_data2_not_nav.html', {
        'form': form,
        'app': app
    })
Ejemplo n.º 2
0
def add_user(request):
    if request.method == 'POST':
        ds = DataSerialize()
        form = ManageAddUserForm(model=User, data=request.POST)
        if form.is_valid():
            new_user = form.save()
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        form = ManageAddUserForm(model=User)

    m2m_fs = User._meta.many_to_many
    m2m_list = []
    for m2m_f in m2m_fs:
        if m2m_f.name in form.fields.keys():
            m2m_list.append(m2m_f.name)
    app = app_info()
    app['name'] = "m_account"
    app['location'] = 'add'
    return render(request, 'add_data2_not_nav.html', {
        'form': form,
        'app': app,
        'm2m_list': m2m_list
    })
Ejemplo n.º 3
0
def edit_user(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    # print 'is_active=%s' % user.is_active
    if request.method == 'POST':
        ds = DataSerialize()
        form = ManageEditUserForm(model=User, instance=user, data=request.POST)
        if form.is_valid():
            new_user = form.save()
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        form = ManageEditUserForm(
            model=User,
            instance=user,
        )

    m2m_fs = User._meta.many_to_many
    m2m_list = []
    for m2m_f in m2m_fs:
        if m2m_f.name in form.fields.keys():
            m2m_list.append(m2m_f.name)
    app = app_info()
    app['name'] = "m_account"
    app['location'] = 'edit'
    return render(request, 'edit_data2_not_nav.html', {
        'form': form,
        'app': app,
        'm2m_list': m2m_list
    })
Ejemplo n.º 4
0
def update_resource_position(request):
    '''
    更新资源节点的位置、排序、层级信息
    :param request:
    :return:
    '''
    if request.method == "POST":
        ds = DataSerialize()
        node_data = request.POST.get('node_data')
        node_data = ds.deserialize(node_data)
        # co.DD(node_data)
        node = node_data['curr_node']
        pre_node = node_data['pre_node']
        next_node = node_data['next_node']
        n_ins = get_object_or_404(Menu, pk=node['id'])
        old_parent_ins = n_ins.parent
        init_max_order_num = 2.0
        init_min_order_num = 1.0

        is_update_pid = False
        if n_ins.parent is not None:  # 非根节点
            if node['pId'] is None:  # 非根节点 --> 根节点
                n_ins.parent = None
            elif n_ins.parent.id != node['pId']:  # 非根节点 --> 非根节点(父节点有变化)
                is_update_pid = True
        elif node['pId'] is not None and n_ins.parent is None:  # 根节点 --> 非根节点
            is_update_pid = True
        else:
            is_update_pid = False
        if is_update_pid:
            n_ins.parent = get_object_or_404(Menu, pk=node['pId'])
            # 更新节点的level
            if n_ins.parent is None:
                n_ins.level = 0
            else:
                n_ins.level = n_ins.parent.level + 1
        else:
            n_ins.level = node['level']
        if pre_node is not None and next_node is not None:  # 夹在中间位
            t_order = round((pre_node['order'] + next_node['order']) / 2, 5)
        elif pre_node is None and next_node is None:  # 第一位或最后一位(有且只有一个元素)
            t_order = init_min_order_num
        elif pre_node is None and next_node is not None:  # 第一位
            t_order = round(next_node['order'] / 2, 5)
        elif pre_node is not None and next_node is None:  # 最后一位
            t_order = round((pre_node['order'] + 1), 5)
        n_ins.order = t_order
        n_ins.save()

        # 重新整理同级所有节点排序,使得序号范围在init_min_order_num -- init_max_order_num之间,
        # 同级的序号间距也重新规整为:round(init_max_order_num-init_min_order_num/subnodes_count, 5)
        new_parent_ins = n_ins.parent
        if new_parent_ins != old_parent_ins:
            sort_subnodes(parent_ins=new_parent_ins)
        sort_subnodes(parent_ins=old_parent_ins)

        rs_data = {'timestamp': ds.get_create_date()}
        html = json.dumps(rs_data)
        return HttpResponse(html, content_type="application/json")
Ejemplo n.º 5
0
def save_resource_tree(request):
    '''
    保存所有节点的位置、排序、层级信息
    :param request:
    :return:
    '''
    if request.method == "POST":
        ds = DataSerialize()
        tree_data = request.POST.get('tree_data')
        tree_data = ds.deserialize(tree_data)
        sort_nodes(nodes=tree_data)
        rs_data = {'timestamp': ds.get_create_date()}
        html = json.dumps(rs_data)
        return HttpResponse(html, content_type="application/json")
Ejemplo n.º 6
0
def del_resource(request, resource_id):
    ds = DataSerialize()
    if request.method == "POST":
        new_resource = get_object_or_404(Menu, pk=resource_id)
        r_parent = new_resource.parent

        sub_menus = Menu.objects.filter(parent=new_resource)
        for sm in sub_menus:
            sm.parent = r_parent
            sm.save()
        del_res = del_model_data(model=Menu, id=resource_id)
        sort_subnodes(parent_ins=r_parent)
        rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
        html = json.dumps(rs_data)
        return HttpResponse(html, content_type="application/json")
Ejemplo n.º 7
0
def save_perm_tree(request, role_id):
    if request.method == "POST":
        ds = DataSerialize()
        tree_data = request.POST.get('tree_data')
        tree_data = ds.deserialize(tree_data)
        role_id = request.POST.get('role_id')
        rs_data = {'timestamp': ds.get_create_date()}
        role_ins = get_object_or_404(Role, pk=role_id)
        for td in tree_data:
            if td['checked'] is True and td['checkedOld'] is False:
                role_ins.permissions.add(Menu.objects.get(pk=td['id']))
            elif td['checked'] is False and td['checkedOld'] is True:
                role_ins.permissions.remove(Menu.objects.get(pk=td['id']))
        role_ins.save()
        html = json.dumps(rs_data)
        return HttpResponse(html, content_type="application/json")
Ejemplo n.º 8
0
def add_role(request):
    if request.method == 'POST':
        ds = DataSerialize()
        form = AddRoleForm(model=Role, data=request.POST)
        if form.is_valid():
            new_role = form.save()
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        form = AddRoleForm(model=Role)

    app = app_info()
    app['name'] = "m_role"
    app['location'] = 'add'
    return render(request, 'add_data2_not_nav.html', {
        'form': form,
        'app': app
    })
Ejemplo n.º 9
0
def edit_role(request, role_id):
    role = get_object_or_404(Role, pk=role_id)
    # print 'is_active=%s' % role.is_active
    if request.method == 'POST':
        ds = DataSerialize()
        form = AddRoleForm(model=Role, instance=role, data=request.POST)
        if form.is_valid():
            new_role = form.save()
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        form = AddRoleForm(model=Role, instance=role)

    app = app_info()
    app['name'] = "m_role"
    app['location'] = 'edit'
    return render(request, 'edit_data2_not_nav.html', {
        'form': form,
        'app': app
    })
Ejemplo n.º 10
0
def edit_resource(request, resource_id):
    resource = get_object_or_404(Menu, pk=resource_id)
    ds = DataSerialize()
    if request.method == 'POST':
        form = AddResourceForm(model=Menu,
                               instance=resource,
                               data=request.POST)
        if form.is_valid():
            new_resource = form.save()
            r_parent = new_resource.parent
            sort_subnodes(parent_ins=r_parent)
            rs_data = {'timestamp': ds.get_create_date(), 'result': 'success'}
            html = json.dumps(rs_data)
            return HttpResponse(html, content_type="application/json")
    else:
        form = AddResourceForm(model=Menu, instance=resource)

    app = app_info()
    app['name'] = "m_account"
    app['location'] = 'edit'
    return render(request, 'edit_data2_not_nav.html', {
        'form': form,
        'app': app
    })