Exemple #1
0
def update_role(request,role_id,template_name="oa/role_form.html"):
    """更新角色"""
    ctx = {}
    schools = helpers.get_schools(request.user)
    role = get_object_or_404(Role, id=role_id,school__in=schools)
    agency_pks = [r.id for r in role.accesses.filter(level=0)]
    school_pks = [r.id for r in role.accesses.filter(level=1)]
    agency_par_accesses = Access.objects.filter(parent__pk=0,level=0).exclude(pk=5)
    school_par_accesses = Access.objects.filter(parent__pk=0,level=1)
    agency_sub_accesses = Access.objects.filter(level=0).exclude(parent__pk=0).exclude(parent__pk=5)
    school_sub_accesses = Access.objects.filter(level=1).exclude(parent__pk=0)

    if request.method == 'POST':
        form = RoleForm(request.POST,instance=role)
        if form.is_valid():
            access_pks = request.POST.getlist('user')
            form.save()
            
            access_list = [a for a in Access.objects.filter(pk__in=access_pks)]
            if access_list:
                role.accesses = access_list
                
            messages.success(request, u"角色 %s 已成功创建" % role.name)
            return redirect("oa_permission_role_list")
    else:
        form = RoleForm(instance=role)
        
    ctx.update({'agency_par_accesses':agency_par_accesses,'school_par_accesses':school_par_accesses,\
                'agency_sub_accesses':agency_sub_accesses,'school_sub_accesses':school_sub_accesses,\
                'form':form,'agency_pks':agency_pks,'school_pks':school_pks,'role':role})
    return render(request, template_name, ctx)
Exemple #2
0
def create_role(request,template_name="oa/role_form.html"):
    """创建角色"""
    school = helpers.get_schools(request.user)[0]
    ctx = {}
    agency_par_accesses = Access.objects.filter(parent__pk=0,level=0).exclude(pk=5)
    agency_sub_accesses = Access.objects.filter(level=0).exclude(parent__pk=0).exclude(parent__pk=5)

    if request.method == 'POST':
        form = RoleForm(request.POST)
        if form.is_valid():
            access_pks = request.POST.getlist('user')
            role = form.save(commit=False)
            role.school = school
            role.save()
            
            access_list = [a for a in Access.objects.filter(pk__in=access_pks)]
            if access_list:
                role.accesses = access_list

            messages.success(request, u"角色 %s 已成功创建" % role.name)
            return redirect("oa_permission_role_list")
    else:
        form = RoleForm()
        
    ctx.update({'agency_par_accesses':agency_par_accesses,'agency_sub_accesses':agency_sub_accesses,'form':form})
    return render(request, template_name, ctx)