rbac组件之角色操作

Posted shenjianping

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rbac组件之角色操作相关的知识,希望对你有一定的参考价值。

为了与stark组件分离,形成独立的模块,所以rbac数据表的操作需要单独进行操作,对角色表的操作。

urls.py

urlpatterns = [
    re_path(r^roles/list/$, RoleView.as_view(),name=roles_list),
    re_path(r^roles/add/$, RoleAddView.as_view(), name=roles_add),
    re_path(r^roles/edit/(?P<rid>\\d+)/$, RoleEditView.as_view(), name=roles_edit),
    re_path(r^roles/dell/(?P<rid>\\d+)/$, RoleDelView.as_view(), name=roles_del),
]

views.py

from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from rbac.models import *
from rbac.forms.roles import RoleModelForm
from django.urls import reverse

# Create your views here.

class RoleView(View):
    def get(self,request):
        role_queryset=Role.objects.all()
        return render(request,rbac/role_list.html,role_queryset:role_queryset)

class RoleAddView(View):
    def get(self,request):
        form = RoleModelForm()
        return render(request,rbac/role_add.html,form:form)

    def post(self,request):
        form=RoleModelForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse(rbac:roles_list))
        return render(request,rbac/role_add.html,form:form)

class RoleEditView(View):

    def get(self,request,rid):
        role_obj=Role.objects.filter(id=rid).first()
        if not role_obj:
            return HttpResponse(该角色不存在)
        form=RoleModelForm(instance=role_obj)
        return render(request,rbac/role_edit.html,form:form)

    def post(self,request,rid):
        role_obj=Role.objects.filter(id=rid).first()
        form=RoleModelForm(data=request.POST,instance=role_obj)
        if form.is_valid():
            form.save()
            return redirect(reverse(rbac:roles_list))
        return render(request, rbac/role_edit.html, form: form)

class RoleDelView(View):

    def  get(self,request,rid):
        Role.objects.filter(id=rid).first().delete()
        return redirect(reverse(rbac:roles_list))

role_list.html

技术图片
% extends ‘layout.html‘ %
% block css %


% endblock %
% block content %
    <div class="role_list">
    <div class="container">
        <a href="% url ‘rbac:roles_add‘ %" class="btn btn-primary add_role">添加角色</a>
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <td>角色ID</td>
                <td>角色名称</td>
                <td>权限个数</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            % for row in role_queryset %
            <tr>
                <td> row.id </td>
                <td> row.title </td>
                <td> row.permissions.count </td>
                <td>
                    <a href="% url ‘rbac:roles_edit‘ rid=row.id %"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                     <span style="padding: 2px 2px;display: inline-block">|</span>
                    <a href="% url ‘rbac:roles_del‘ rid=row.id %"><i class="fa fa-trash" aria-hidden="true"></i></a>
                </td>
            </tr>
            % endfor %
            </tbody>
        </table>
    </div>
    </div>
% endblock %
View Code

role_add.html

技术图片
% extends ‘layout.html‘ %

% block content %
    <div style="margin-top: 20px">
    <form  method="post" class="form-horizontal" novalidate>
    % csrf_token %
    % for field in form %
     <div class="row">
    <div class="form-group">
    <label class="col-sm-2 control-label"> field.label </label>
    <div class="col-sm-6">
         field <span style="color: red;display: inline-block"> field.errors.0 </span>
    </div>
  </div>
     </div>
     % endfor %
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-primary">提交</button>
    </div>
  </div>
    </form>
     </div>
% endblock %
View Code

role_edit.html

技术图片
% extends ‘layout.html‘ %

% block content %
    <div style="margin-top: 20px">
    <form method="post" class="form-horizontal" novalidate>
    % csrf_token %
     <div class="row">
    <div class="form-group">
    <label class="col-sm-2 control-label">角色名称</label>
    <div class="col-sm-6">
         form.title <span style="color: red;display: inline-block"> form.title.errors.0 </span>
    </div>
  </div>
     </div>
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-primary">提交</button>
    </div>
  </div>



    </form>
     </div>
% endblock %
View Code

 

操作页面

技术图片

 

以上是关于rbac组件之角色操作的主要内容,如果未能解决你的问题,请参考以下文章

权限组件之rbac

rbac组件之数据库设计

Django-CRM项目学习-权限(rbac)

Django框架之RBAC+ContentType

Django之权限管理

django之权限组件